Skip to content
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

Closed
wants to merge 1 commit into from
Closed

FbtTranslations: add support for modifying registered translations #208

wants to merge 1 commit into from

Conversation

mrtnzlml
Copy link
Contributor

@mrtnzlml mrtnzlml commented Apr 12, 2021

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.

@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Apr 12, 2021
@jrwats
Copy link
Contributor

jrwats commented Apr 13, 2021

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?

@mrtnzlml
Copy link
Contributor Author

@jrwats Absolutely agree. This was actually my second proposal in my head (but I was thinking getRegisteredTranslations will open more possibilities). Would you accept a PR with both functions?

@jrwats
Copy link
Contributor

jrwats commented Apr 13, 2021

Yes. That'd be fine.

@mrtnzlml mrtnzlml changed the title FbtTranslations: add support for getting registered translations FbtTranslations: add support for modifying registered translations Apr 13, 2021
@mrtnzlml
Copy link
Contributor Author

@jrwats I updated the PR. Could you please have a look?

Copy link
Contributor

@jrwats jrwats left a 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!

Comment on lines 59 to 62
if (translatedFbts[locale] === undefined) {
translatedFbts[locale] = {};
}
Object.assign(translatedFbts[locale], newTranslations[locale]);
Copy link
Contributor

@jrwats jrwats Apr 13, 2021

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],
      );

Copy link
Contributor Author

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.

Copy link
Contributor

@jrwats jrwats Apr 13, 2021

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

Copy link
Contributor Author

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 = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@facebook-github-bot
Copy link
Contributor

@jrwats has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@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.
@facebook-github-bot
Copy link
Contributor

@mrtnzlml has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Contributor

@jrwats merged this pull request in d05d44f.

@mrtnzlml mrtnzlml deleted the FbtTranslations_getRegisteredTranslations branch April 15, 2021 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Do not delete this pull request or issue due to inactivity. Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants