forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Expensify#25846 from BeeMargarida/feat/25466-flatt…
…en_translation_objects Improve translations - flatten translation objects
- Loading branch information
Showing
16 changed files
with
268 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,50 @@ | ||
import en from './en'; | ||
import es from './es'; | ||
import esES from './es-ES'; | ||
import type {Translation, TranslationFlatObject} from './types'; | ||
|
||
/** | ||
* Converts an object to it's flattened version. | ||
* | ||
* Ex: | ||
* Input: { common: { yes: "Yes", no: "No" }} | ||
* Output: { "common.yes": "Yes", "common.no": "No" } | ||
*/ | ||
// Necessary to export so that it is accessible to the unit tests | ||
// eslint-disable-next-line rulesdir/no-inline-named-export | ||
export function flattenObject(obj: Translation): TranslationFlatObject { | ||
const result: Record<string, unknown> = {}; | ||
|
||
const recursive = (data: Translation, key: string): void => { | ||
// If the data is a function or not a object (eg. a string or array), | ||
// it's the final value for the key being built and there is no need | ||
// for more recursion | ||
if (typeof data === 'function' || Array.isArray(data) || !(typeof data === 'object' && !!data)) { | ||
result[key] = data; | ||
} else { | ||
let isEmpty = true; | ||
|
||
// Recursive call to the keys and connect to the respective data | ||
Object.keys(data).forEach((k) => { | ||
isEmpty = false; | ||
recursive(data[k] as Translation, key ? `${key}.${k}` : k); | ||
}); | ||
|
||
// Check for when the object is empty but a key exists, so that | ||
// it defaults to an empty object | ||
if (isEmpty && key) { | ||
result[key] = ''; | ||
} | ||
} | ||
}; | ||
|
||
recursive(obj, ''); | ||
return result as TranslationFlatObject; | ||
} | ||
|
||
export default { | ||
en, | ||
es, | ||
en: flattenObject(en), | ||
es: flattenObject(es), | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
'es-ES': esES, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.