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

How to deal with defaultLanguage #76

Open
mgtitimoli opened this issue Jan 13, 2017 · 7 comments
Open

How to deal with defaultLanguage #76

mgtitimoli opened this issue Jan 13, 2017 · 7 comments

Comments

@mgtitimoli
Copy link

Hi @GertjanReynaert, before going forward I wanted to say thank you for your work, great job!

I opened this issue since I'm a bit confused about how to deal with the defaultLanguage.

Let's say you wrote all the message descriptors in your components and then used babel-plugin-react-intl to extract them to a directory, you google a while about how to join them and found this package, so you read the docs and create a script like this:

import manageTranslations from 'react-intl-translations-manager';

const defaultLanguage = 'en-US';

manageTranslations({
  messagesDirectory: 'src/translations/extractedMessages',
  translationsDirectory: 'src/translations/locales/',
  languages: [defaultLanguage], // any language you need
});

you run the script and voila, everything worked as expected, but there is a file that grabs your attention: whitelist_en-US.json, so you read the docs again and found this:

languages (optional, default: [])
What languages the translation manager needs to maintain. Specifying no languages actually doesn't make sense, but won't break the translationManager either.
example: for ['nl', 'fr'] the translation manager will maintain a nl.json, fr.json, whitelist_nl.json and a whitelist_fr.json file

You think: "I can have more control with this feature, great!"

And then you modify some defaultMessage, run the script again, and you get:

image

So you are stuck, you was expecting the generated json to update with the latest change you've introduced but instead you got all this...

So the question would be:

Shouldn't it work like I'm expecting or if not, why?

Thanks!

@GertjanReynaert
Copy link
Owner

First: thx for using this package!

Second: sorry for the confusion around the default language!

I haven't found a good way to deal with the defaultLanguage yet, but ATM, it's not really recommended to include the defaultLanguage in your languages array. At the start of this package I approached it like this: since your defaultMessages are in your defaultLanguage, you don't need to manage them, so you don't need to include the language in the array of languages to be managed.

That being said, we should come up with an approach to help people out (either a way to include the default language, or better docs) since this is a frequently returning question.

@vilvadot
Copy link

First of all thanks for the tool @GertjanReynaert Im using it and its amzazing. Im also having questions regarding this feature.

I have it included because I want it to generate the 'en.json' file from my messages, but I don't like to get that output.

Maybe we should be able to define a defaultLanguage, the manager will then generate the defaultMessages .json with that name but ignore it when comparing files?

That way we get the file generation and nice translations management. just an idea!

@jkomusin
Copy link

I wrote a small workaround for this very issue today, it's not very clean since it re-reads the messages, and it prevents checking for untranslated strings in other languages, but it works with the current API and may be a start. In the original example:

import manageTranslations, {
  readMessageFiles,
  getDefaultMessages
} from 'react-intl-translations-manager';

const defaultLanguage = 'en-US';
const messagesDirectory = 'src/translations/extractedMessages';

manageTranslations({
  messagesDirectory,
  translationsDirectory: 'src/translations/locales/',
  languages: [defaultLanguage], // any language you need
  overrideCoreMethods: {
    provideWhitelistFile: (lang) => {
      // Avoid reporting untranslated stuff in defaultLanguage
      if (lang === defaultLanguage) {
        const messageFiles = readMessageFiles(messagesDirectory);
        const messages = getDefaultMessages(messageFiles).messages;
        return Object.keys(messages);
      } else {
        // This is no good, unfortunately :(
        // You could do your own whitelist retrieval here..
        return [];
      }
    }
  }
});

I wonder if provideWhitelistFile (and perhaps provideTranslationsFile as well to stay consistent) should also be passed the set of default messages? The core runner does have them at this point, would it be odd to pass them along for use in overridden hooks like this?

I've also opened #84 which might be of interest. If implemented it would allow for the else clause I wrote to continue normally.

@hudovisk
Copy link

It looks like this package is not maintained anymore, so I forked it and made a little change to suppress the warnings about untranslated messages with a defaultLanguage option in config.

You can check it here: https://github.com/hudovisk/react-intl-translations-manager

@GertjanReynaert
Copy link
Owner

@hudovisk I couldn't really find how to configure your fork with the suppression though. Since I'm not using this package on a day to day basis my attention to the package has slacked a bit. However it's in a pretty solid state right now, so it doesn' require frequent updates anymore, but this lack of updates might have given an unmaintained impression.

Long story short: If you'd like to create a pr, that's always welcome!

@hudovisk
Copy link

hudovisk commented Mar 31, 2018

Hi @GertjanReynaert, sorry if I misunderstood the state of the package, would be nice to have your opinion on my patch #113 , I use it with the config:

...
languages: ['en-US', 'pt-BR'],
defaultLanguage: 'en-US',
....

this way I can extract all de defaultMessages in a simple json file without having the untranslated warnings.

@sunknudsen
Copy link

Great work @GertjanReynaert. Thanks, really. What’s the status for the #113 PR? Looks promising! I believe having the default locale in translations/locales makes things more coherent for integrating with translation apps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants