Skip to content

Commit

Permalink
STRF-9433: Update doc on cascade/flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
mattolson committed Jan 29, 2022
1 parent 717e60e commit c267e05
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/translator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function Translator(acceptLanguage, allTranslations, logger = console) {

/**
* @private
* @type {Object.<string, string>}
* @type {Object.<string, string|Object>}
*
* Looks like this:
* {
Expand Down
56 changes: 26 additions & 30 deletions lib/translator/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,25 @@
*
* The return value looks like this, assuming preferredLocales of ['fr-CA', 'en']:
* {
* 'fr-CA': {
* locale: 'fr-CA',
* locales: {
* 'salutations.welcome': 'en',
* 'salutations.hello': 'fr-CA',
* 'salutations.bye': 'fr',
* items: 'en',
* },
* translations: {
* 'salutations.welcome': 'Welcome',
* 'salutations.hello': 'Salut {name}',
* 'salutations.bye': 'au revoir',
* items: '{count, plural, one{1 Item} other{# Items}}',
* }
* locale: 'fr-CA',
* locales: {
* 'salutations.welcome': 'en',
* 'salutations.hello': 'fr-CA',
* 'salutations.bye': 'fr',
* items: 'en',
* },
* translations: {
* 'salutations.welcome': 'Welcome',
* 'salutations.hello': 'Salut {name}',
* 'salutations.bye': 'au revoir',
* items: '{count, plural, one{1 Item} other{# Items}}',
* }
* }
*
* @param {Object.<string, Object>} allTranslations
* @param {string[]} preferredLocales
* @param {Object} logger
* @returns {Object.<string, Object>} Transformed translations
* @returns {Object.<string, string|Object>} Transformed translations
*/
function transform(allTranslations, preferredLocales, logger = console) {
const flattened = flatten(allTranslations, preferredLocales, logger);
Expand Down Expand Up @@ -158,26 +156,24 @@ function flatten(translations, preferredLocales, logger = console) {
*
* The return value looks like this, assuming preferredLocales of ['fr-CA', 'en']:
* {
* 'fr-CA': {
* locale: 'fr-CA',
* locales: {
* 'salutations.welcome': 'en',
* 'salutations.hello': 'fr-CA',
* 'salutations.bye': 'fr',
* items: 'en',
* },
* translations: {
* 'salutations.welcome': 'Welcome',
* 'salutations.hello': 'Salut {name}',
* 'salutations.bye': 'au revoir',
* items: '{count, plural, one{1 Item} other{# Items}}',
* }
* locale: 'fr-CA',
* locales: {
* 'salutations.welcome': 'en',
* 'salutations.hello': 'fr-CA',
* 'salutations.bye': 'fr',
* items: 'en',
* },
* translations: {
* 'salutations.welcome': 'Welcome',
* 'salutations.hello': 'Salut {name}',
* 'salutations.bye': 'au revoir',
* items: '{count, plural, one{1 Item} other{# Items}}',
* }
* }
*
* @param {Object.<string, Object>} translations Flattened translations
* @param {string[]} preferredLocales Ordered list of preferred locales
* @returns {Object.<string, Object>} Cascaded translations spec
* @returns {Object.<string, string|Object>} Cascaded translations spec
*/
function cascade(translations, preferredLocales) {
const result = { locale: preferredLocales[0], locales: {}, translations: {} };
Expand Down

0 comments on commit c267e05

Please sign in to comment.