- We’re using MarkLogic on the backend – our data is stored in xml. Our server-side code is XQuery.
- Some of these strings are needed on the server side as well, so storing them all on the client in .js files wasn’t going to cut it.
- We wanted to get all of the strings into the same place to make the list more manageable to update and maintain.
So, now there are two parts to getting internationalized strings available in the front-end code: The data transform on the backend and the strings made available on the front-end.
The Backend Transform
The resource bundle data looks like this:
1 2 3 4 5 6
The transform a la:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Now we have our xml transformed into a sequence of xs:string, each a json object where the English text is the key and the language text is the value, like this:
1 2 3 4 5 6 7 8 9
Note that if you provide a pre-known set of i18n string $keys to the get-lang-dict() function, it will only add those resources to the dictionary. Otherwsie, all the resources in your bundle are added.
The lang object that stores the dictionary of translated strings is like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
And when you want to grab a string from the language dictionary, just call:
Note that the lang object implementation also allows for storage of a plural form of the translated word. In practice, it was decided not to store our resource bundles in a parallel format. Still, you provided data to the lang.dict() populating function that looked like this:
And then got the translation like this:
If it had a plural form, it would use it, falling back to the singular (1st) form.
So, what are the pros and cons here? The main design issue that I felt created a bit of give and take was the keying of the strings by the English word. The pro: If the string doesn’t exist, the intelligible English string is shown in its place. The con: It’s less reliable and more brittle because you have to be able to control and escape properly the text that appears in the key. What alternate solutions might you create?
this article on i18n in RequireJs <http://rockycode.com/blog/i18n-strings-requirejs/>_.