-
Notifications
You must be signed in to change notification settings - Fork 178
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
FbtTranslations: add support for modifying registered translations #208
FbtTranslations: add support for modifying registered translations #208
Conversation
I'm actually not opposed to exposing this for other purposes, but for what you're trying to solve, you'll still want some "merging" logic to ensure existing translations in the same locale aren't lost. In that implementation, you'd perform function mergeTranslations(newTranslations) {
Object.keys(newTranslations).forEach(locale => {
Object.assign(this.translatedFbts[locale], newTranslations[locale]);
});
} Thoughts? |
@jrwats Absolutely agree. This was actually my second proposal in my head (but I was thinking |
Yes. That'd be fine. |
@jrwats I updated the PR. Could you please have a look? |
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.
Minor nit, but LG! Thanks for the PR!
runtime/nonfb/FbtTranslations.js
Outdated
if (translatedFbts[locale] === undefined) { | ||
translatedFbts[locale] = {}; | ||
} | ||
Object.assign(translatedFbts[locale], newTranslations[locale]); |
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.
minor nit:
translatedFbts[locale] = Object.assign(
translatedFbts[locale] ?? {},
newTranslations[locale],
);
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.
That's unfortunately not possible because it would not be merging the translations into translatedFbts
but rather into an empty unrelated object. Tests are covering this case, you can try it.
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.
But Object.assign(...)
will return its result and we're setting the entry in my suggestion above? Just tested in the imported diff — seems to work.
PASS fbt-runtime packages/fbt/lib/__tests__/FbtTranslations-test.js
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.
🙈 You are right! I completely missed the part that you are suggesting to assign the value back via translatedFbts[locale] = …
My apologies. I updated the PR with your suggestion.
@@ -14,7 +14,7 @@ import type {FbtRuntimeCallInput, FbtTranslatedInput} from 'FbtHooks'; | |||
|
|||
const FbtHooks = require('FbtHooks'); | |||
|
|||
let translatedFbts = null; | |||
let translatedFbts: TranslationDict = {}; |
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.
✓
@jrwats has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
@mrtnzlml has updated the pull request. You must reimport the pull request before landing. |
This change adds a new function `getRegisteredTranslations` into `FbtTranslations` which allows us to retrieve registered translations. For convenience, it also adds a new `mergeTranslations` function that allows us to append the translations. Use-case: I have an application with FBT and a design system with its own FBT as well. Unfortunately, registering translations overwrites any translations previously registered so the design system (or the application) cannot register their own translations without conflicting with the other part. One solution could be to get already registered translations and re-register them modified. Another solution is to merge these two translations together. There are many other possible solutions so I am open to suggestions how to make it better.
@mrtnzlml has updated the pull request. You must reimport the pull request before landing. |
This change adds a new function
getRegisteredTranslations
intoFbtTranslations
which allows us to retrieve registered translations. For convenience, it also adds a newmergeTranslations
function that allows us to append the translations.Use-case: I have an application with FBT and a design system with its own FBT as well. Unfortunately, registering translations overwrites any translations previously registered so the design system (or the application) cannot register their own translations without conflicting with the other part. One solution could be to get already registered translations and re-register them modified. Another solution is to merge these two translations together.
There are many other possible solutions so I am open to suggestions how to make it better.