How can I make i18n for javascript with Middleman?

Is there something already in the technology stack used by MM that will handle i18n for JS? For instance. something that takes the javascript section from the en.yml and es.yml and makes it available as a translation.js file - or something like that (I only know how it is sometimes done on rails).

Fwiw, https://github.com/fnando/i18n-js seems like a popular rail solution. I assume parts of JS will work fine. I'm just not sure how to integrate the build process into the MM stack, so that all the files are generated and get to the right place.

@tdreyno suggests: Currently not, but it looks like we could connect Asterisks (JS) and I18n.

Has anyone done this, or do you have suggestions on how to proceed (for those who work in Ruby / Rails but haven’t yet dug in the path to resource 3.1, but are not afraid to try).

+4
source share
2 answers

I am just thinking about solving the same problem. So here is my solution:

In source/layouts/layout.html.slim :

 javascript: var I18n = {}; I18n.locale = 'fr'; I18n.translations = {}; I18n.translations['fr'] = #{I18n.backend.send(:translations)[:fr].to_json}; 

Then access to translations in JS is as simple as:

 I18n.translations.fr['menu']['glossaire'] #=> "Glossaire" 
+1
source
Decision

caedes is simple and functional, but in my case I want to use some of the i18n-js more complex functions, such as string interpolation, rather than just a set of nested dictionaries.

Fortunately, i18n-js can read from a dictionary . I installed i18n-js with a browser, imported it globally using window.I18n = require('i18n-js'); and added a partial _i18n_js.slim :

 javascript: I18n.translations = #{I18n.backend.send(:translations).to_json}; 

and then you can use all the features of the excellent i18n-js .

0
source

Source: https://habr.com/ru/post/1397156/


All Articles