Replies: 4 comments
-
So, the answer depends on which version you're using. I'm assuming you want the latest one. There are two repos:
You can do that by using the
If you want to use translations on your views, the traditional approach of using
Just make sure you call
Rake tasks are a thing from previous versions and have been superseded by the CLI.
You are. The |
Beta Was this translation helpful? Give feedback.
-
Thank you very much fnando for your quick response. With your helpful explanations plus much experimenting I have reached my goal. I did not end up using the i18n-js npm package, but instead templated my own I18n.t() function into a file called app/assets/javascripts/i18n.js and included that in my app/assets/javascripts/application.js file. This means that any JavaScript code can now call I18n.t(key) to get a translation. That approach has been useful for me as I've been able to include custom business logic and also avoid creating any new npm package dependencies, as the application currently only has one. This is my i18n.yml file
I only want output from the export_files plugin, not the json produced by the main i18n export command. I found by specifying the same name twice the export plugin over wrote the json output. I'd prefer to find a way to just specify the export_files output file and skip the json file rendering. For instance at the moment my solution would break if the Thanks again. |
Beta Was this translation helpful? Give feedback.
-
Hi CarriSisomphouqo |
Beta Was this translation helpful? Give feedback.
-
Hi,
I'm looking for help getting setup properly.
I have a Ruby on Rails application. It has some client side JavaScript code. I want to add some translation strings to that code. I'd like the translations to be included in the application.js bundle that is precompiled and provided to the browser when the application loads, and then reference it from other JS client side code.
I've tried to follow the i18n-js and i18n repo readme files, but embarrassingly they just don't hold my hand enough to get it all straight. I am in the process of preparing a PR to offer some changes to the readme's that would have helped me, and hopefully others. I won't push that up until I have it all working.
Here's an outline of my understanding of the steps I need to perform
i18n export
to create .json files, and store them in app/javascript/translations. I'd prefer to find a way to export them as .js files that can be embedded in the application.js bundle that Rails will provide to the client browser.I've seen other suggestions about adding a controller to the Ruby on Rails app that will serve translations when requested by the client side JavaScript. I don't really want to take that approach. I only need to update translation strings on fresh application deployments. My preference is to have some way to ensure the build pipeline generates and precompiles the translation hash that will be provided in the application.js and then made available for other client side Javascript to reference.
The client side code will be served a locale attribute in the document body so the client side JavaScript knows which language to render.
I've seen mention of
rake i18n:js:export output_file=public/javascripts/translations.js
but I don't have that rake task despite installing thei18n-js
gem v4.I'm not sure if I'm also supposed to include the
i18n-js
npm module into the application.js bundle. I've tried. I did thenpm install i18n-js
and have the package in app/node_modules, but adding//= require i18n-js
to application.js causes errors in the browser console, and the JavaScript package fails. Ultimately I'm not sure I need the i18n-js in the client side code, if I can generate a globally accessible data structure that the JavaScript client side code can reference directly then that'll be good enough for me.Thank you for reading this far. I'd very much appreciate any help with this. I've looked for examples in github, but failed.
All the best,
Cheers,
Mark
Beta Was this translation helpful? Give feedback.
All reactions