formatjs icon indicating copy to clipboard operation
formatjs copied to clipboard

Examples with more than just english, and translation files

Open kosz opened this issue 10 years ago • 45 comments

I was looking for an example that sets up i18n for anything other than english and shows how to create and setup translation files, but it looks like every example in the repo only sets up english and there are no translation files of any kind.

Am I missing something ?

kosz avatar May 13 '16 12:05 kosz

You can check mine https://github.com/este/este dev stack, where react-intl is used in universal manner.

steida avatar May 14 '16 22:05 steida

@kosz I only know English :) wanna help by translate the apps in the /examples/ dir?

ericf avatar May 14 '16 23:05 ericf

Thanks for your answers guys. I'll see if I can set some time aside for contributing as suggested :)

The most confusing thing for me was that there were no translation files. Perhaps splitting these words into their own translation files may look a bit more intuitive. At the end of the day, French doesn't have to be French for the example to be understood, normally we do things like

fr-translation-file.js

{
  hello: '[FR] Hello',
  world: '[FR] world'
}

And this file is now ready for a non technical translator to work with easily

kosz avatar May 18 '16 14:05 kosz

Yes this is indeed the biggest disadvantage when trying to adopt this technology. It took me more than a day to figure out that:

  • I need the react-intl package
  • and the babel-plugin-react-intl package
  • which has to be added in the webpack (dev) config as a babel config
  • and the react-intl-translations-manager package
  • which requires the babel-cli package
  • and a script entry in the package.json
  • which now requires the --preset es2015 option
  • and an additional translationRunner.js script
  • and that I should add the desired languages via addLocalData from react-intl/locale-data
  • but when providing the locale and messages to IntlProvider it is my responsibility to choose the one and only correct message object

All of this is needed to correctly print "Hello World" or "Hallo Welt", which is the most trivial use case for everyone outside the us or uk.

Documenting this is indeed the first step (thats why I wrote the above bullet list), but I see some additional issues too:

  • the functionality of the babel-plugin-react-intl and react-intl-translations-manager packages should be combined into the babel-plugin-react-intl package without requiring babel-cli
  • instead of outputting a json file output an js file and add the description and additional information as comments

Hopefully this comment helps until the docs are updated. I think that the approach choose with v2 where we can embed the default language into the code and make anything else and additional process is superior. Thanks Ognian

Ognian avatar May 20 '16 12:05 Ognian

@Ognian Instead of such a long whining you should send PR. Omg, what's wrong with those people? They can have awesome things for free, and instead of being happy they only cry that life is hard. Shame.

steida avatar May 20 '16 13:05 steida

@steida looks like you are a fan of the programming first and thinking thereafter approach. I prefer to discuss issues first. Answering a comment by begging for a PR or shaming people suggests a bad style.

Ognian avatar May 20 '16 14:05 Ognian

addLocaleData is for low-level plural and relative-time formatting that's passed along to the lower-level libs React Intl is built on. The reason it's split out from the library is because loading the locale data for all languages would be expensive and wasteful since you only are running one language at a time. This stuff is documented: https://github.com/yahoo/react-intl/wiki#loading-locale-data

babel-plugin-react-intl is optional, you don't have to use it you use an external files to hold your default messages and translations. If you want to co-locate messages in your components, then you should use it to extract these strings into external files.

The reason you pass messages into IntlProvider is to setup the i18n context, this allows your app to be rendered as a function of props and state. And this is again, documented: https://github.com/yahoo/react-intl/wiki#creating-an-i18n-context

Yes it is going to be your responsibility to get your messages translated and pass them into the library. React Intl doesn't do translations, similarly React Intl doesn't negotiate the locale, you need to provide this as well. You could serialize the messages in the HTML page, or load them as a separate script, it's up to you for how ever it best integrates with your app.

The translations manager package is third-party and not something that I work on. So I can't speak about it other than it's not required to use React Intl.

ericf avatar May 21 '16 18:05 ericf

Thanks @ericf for the explanation. From a technical point of view I understand why babel-plugin-react-intl is optional, but from a framework user perspective when I'm searching for a i18n framework the translation workflow is the very first thing I'm looking at and everything else adds more and more value to the framework. Since I see that we don't have this yet lets try to build it, therefore my question would you accept a PR for babel-plugin-react-intl where functionality changes in that way that it generates not only the module files for a list of languages but also allows changes in those files to be preserved between runs? Or how would you proceed?

Ognian avatar May 23 '16 16:05 Ognian

@Ognian Check messages-extract.js and messages-check.js in https://github.com/este/este/tree/master/gulp

steida avatar May 23 '16 17:05 steida

therefore my question would you accept a PR for babel-plugin-react-intl where functionality changes in that way that it generates not only the module files for a list of languages but also allows changes in those files to be preserved between runs? Or how would you proceed?

@Ognian I don't fully understand what you're proposing here. The mental model is that babel-plugin-react-intl creates build artifacts based on the source, and what you do with those and how you integrate it into your translation pipeline is up to you. Many translation systems work differently, the idea is that the Babel plugin will provide you with the raw default strings, which then you can process and prep for translation.

ericf avatar May 23 '16 17:05 ericf

OK, the "workflow" I suggest is the following:

  • Write your code in english using FormattedMessage. Lets assume we have only one Message in a js Module named TableOptions.js wich is in the containerssubdirectory of src
 <FormattedMessage
                                        id="newItem"
                                        description="the text on the plus button for adding an item"
                                        defaultMessage="Add item"
                                    />
  • use the babel-plugin (i.e. react-intl). This plugin extracts the messages and prepares all the language files. Lets say we want to have german and french, then we would add the language option languages:[de fr]and an option for an directory where to put the language files. This directory is treaded like a source code directory. Lets name this dir `ì18n. So for the above example we get 2 filesTableOptions.de.jsonandTableOptions.fr.jsonini18n/containers``and the content of this files is for the first time running the plugin nearly the same:
[
  {
    "// id": "newItem",
    "// description": "the text on the plus button for adding an item",
    "// defaultMessage": "Add item",
    "newItem":"[DE] Add item"
  }
]
[
  {
    "// id": "newItem",
    "// description": "the text on the plus button for adding an item",
    "// defaultMessage": "Add item",
    "newItem":"[FR] Add item"
  }
]
  • Now just using an editor you can edit and translate the files:
[
  {
    "// id": "newItem",
    "// description": "the text on the plus button for adding an item",
    "// defaultMessage": "Add item",
    "newItem":"Neuer Eintrag"
  }
]
[
  {
    "// id": "newItem",
    "// description": "the text on the plus button for adding an item",
    "// defaultMessage": "Add item",
    "newItem":"Nouvel élément"
  }
]
  • When we rerun the babel plugin it first reads this language files. If a new id is found it is added as in step 2, if an id was deleted it is deleted too, and if an id was already in the file the translation is preserved and the description and the default message are updated. An important part is that the options for the babel plugin are NOT options of the plugin but options in the package.json of the app.
  • We need a webpack plugin which collects all language files not only from our app but also from the modules in node_modules and generates the "optimal" (whatever this means) flatten file to be used as input to messages in IntlProvider. In the above example I showed a .json file where fields starting with //should be treaded as comments and discarded by the webpack plugin. As I previously suggested we could use .js files instead i.e.:
lang=[
  {
    // id: newItem
    // description: the text on the plus button for adding an item
    // defaultMessage: Add item
    "newItem":"Neuer Eintrag"
  }
]

which I personally think is looking better (but this should be discussed)

The reason for changing the workflow is that most of the applications are written AND translated by the developer(s) who normally knows at least 2 languages: his/her own and "bad english". "translation systems" can still be used and transform the `ì18n``directory but my experience showed that a text editor is the best translation tool.

This workflow would also allow that modules added via npm bring their own translations.

Ognian avatar May 24 '16 09:05 Ognian

@Ognian sounds like a specific workflow for your app, you should build that out. You have the tools to do so and nothing you described here couldn't be done by you writing a little bit of code to implement your desired workflow.

This is not the same workflow that we could use at Yahoo, and I'm sure many others have a different workflow than what you described. Therefore I think babel-plugin-react-intl should remain low level and generic so others can use it as a building block in their translation workflow.

ericf avatar May 24 '16 18:05 ericf

After playing another day with memsource.com a web based CAT (Computer Aided Translation) Tool my eyes opend:

  • generate a JSON File in the build directory (it can be discarded since it has NOT to preserve any previous translations). This JSON File has to contain an array of objects to translate. These objects can have as much keys as you want since they will be preserved in the translated output JSON File. It is only important that in one key we have the whole message to translate and in another key we can have a description wich is showed when translating this message.
  • When we translate with the CAT Tool, a thing called Translation Memory TM is created which contains all the messages we translate and even more. This is the file which we save in the source directory together with the translated files and this is the file which enables the CAT Tool to "immediately" fill an newly generated file with the previously translated messages.

So how can we do this workflow:

  • use babel-plugin-react-intl as it is and put the output into the build directory
  • use a webpack plugin (is there already something like this?) to generate the one and only defaultMessages JSON File
  • translate this file with a CAT Tool and get the translated result files(s) and the TM file which shall be saved somewhere in the source

What I'm still missing is:

  • uniqueness of the id: actually only the uniqueness in one module can be assured. Maybe prepending the id with the path and the module name (name in package.json) would be a good idea, since actually we will have to load not only translated messages from our module but also some from node_modules too
  • do you know of any simple and free CAT Tools? memsource.com looks good and they have a free option too. I also saw that okapi has such tools but haven't tried them out.

Ognian avatar May 26 '16 14:05 Ognian

@Ognian You can write a simple node script (or create a Grunt, Gulp, etc. task) to aggregate the JSON files produced by babel-plugin-react-intl and apply your business logic; e.g., error on duplicate message ids.

I wrote code to do this for one of the example apps in this repo: https://github.com/yahoo/react-intl/blob/master/examples/translations/scripts/translate.js#L9-L26

ericf avatar May 26 '16 16:05 ericf

I wrote https://github.com/Ognian/reactIntlJson-loader a webpack loader to directly import the output of the translation:

import deMessages from 'reactIntlJson!./intl/TableOptions-en-de_at-C.json';

let messages = {};
messages.de = deMessages;
const currentLang = navigator.language
...
<IntlProvider locale={currentLang} messages={messages[currentLang]}>
...

and found https://github.com/danilobuerger/react-intl-loader/blob/master/index.js another webpack loader which focuses on the conditional load so maybe we could even chain both loaders (@danilobuerger)

@ericf thanks for the input, but I still have a problem with the react-intl babel plugin: it writes directly to the fs, which makes it write to the build directory even if webpack is in dev mode and actually uses a memory fs. I searched a lot to find an example of a webpack loader or plugin to find a pattern how to do this without dumping to a directory but haven't been very successful.

Ognian avatar May 27 '16 15:05 Ognian

@Ognian most likely you can. If you have any trouble, open an issue in that repos and i will try to help out.

danilobuerger avatar May 27 '16 16:05 danilobuerger

@Ognian please read through the babel plugin completely, because it does support providing the messages in memory and only writes to disk when the messagesDir option is provided. https://github.com/yahoo/babel-plugin-react-intl#via-node-api

ericf avatar May 30 '16 15:05 ericf

@ericf yes I saw it, thanks, I'm preparing a webpack plugin and a PR to babel-loader to pass thru metadata... Need just some more time to work on this... Will post back here

Ognian avatar May 30 '16 15:05 Ognian

@ericf the babel-loader pr is https://github.com/babel/babel-loader/pull/254 and the plugin https://github.com/Ognian/ReactIntlPlugin. I need some sleep before finishing and publishing the plugin ...

Ognian avatar May 30 '16 19:05 Ognian

Both are published now and should work

  • https://github.com/Ognian/reactIntlJson-loader
  • https://github.com/Ognian/react-intl-webpack-plugin

Ognian avatar May 31 '16 12:05 Ognian