-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Improve translations - flatten translation objects #25846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mountiny
merged 19 commits into
Expensify:main
from
BeeMargarida:feat/25466-flatten_translation_objects
Sep 13, 2023
Merged
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e3b5852
test: unit test for translation flatten object function
BeeMargarida 82919b7
feat: flatten object function to flatten translation
BeeMargarida 463daff
feat: remove lodashGet from translate
BeeMargarida 62d4e91
feat: migrate use of languages translate to new solution
BeeMargarida 370d679
feat: migrate use of pronouns translate to new solution
BeeMargarida 9f84e42
feat: migrate use of countries and states translate to new solution
BeeMargarida 6548241
style: prettier
BeeMargarida f38a0bf
fix: small error
BeeMargarida 2fef893
refactor: move unit test to translate unit suite
BeeMargarida 9d8b224
fix: remove unused usage
BeeMargarida ad78499
feat: rename const
BeeMargarida 0088b39
feat: switch us states const to an array of ISOs
BeeMargarida e86a966
Merge branch 'main' into feat/25466-flatten_translation_objects
BeeMargarida c6bf5df
Merge branch 'main' into feat/25466-flatten_translation_objects
BeeMargarida 8db1cda
Merge branch 'main' into feat/25466-flatten_translation_objects
BeeMargarida 1ac2cde
Merge branch 'main' into feat/25466-flatten_translation_objects
BeeMargarida be487e7
feat: improve translation types
BeeMargarida 2931a45
refactor: rename generics
BeeMargarida 8f8420a
style: prettier
BeeMargarida File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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, TranslationBaseValue, 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: TranslationFlatObject = {}; | ||
|
||
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 as TranslationBaseValue; | ||
} 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] = {} as TranslationBaseValue; | ||
} | ||
} | ||
}; | ||
|
||
recursive(obj, ''); | ||
return result; | ||
} | ||
|
||
export default { | ||
en, | ||
es, | ||
en: flattenObject(en), | ||
es: flattenObject(es), | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
'es-ES': esES, | ||
}; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TranslationFlatObject
is way too generic (along withTranslation
&TranslationBaseValue
types). The problem is that we lose type safety usingflattenObject
function:This is a big problem as we should infer what keys are available to stay type safe when using Localize inside of components.
What we should do is to flatten the object in Typescript. Something like this (link):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However this will be quite irritating to use inside of components as the value is not inferred and it's too generic:
string | string[] | Function
. So we would have to always check the types inside of components 😒There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I had a similar problem in my side project, something like this could work here but the type is quite complicated ngl 😅 (this would have to be tested as I used it a little bit differentl in my project)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also cc @fabioh8010 as this is quite important to get right 😅