Answer the question
In order to leave comments, you need to log in
Localizing a .js file with RoR code?
There is a regular .js file, I need localized text in it. I came up with the following:
app.js
import { locale } from './locale.js.erb';
const locale = locale( );
translated_word = locale.translated_word;
function locale() {
return {
translated_word: "<%= t('words.translated_word') %>"
}
}
export { locale }
{ translated_word: "<%= t('words.translated_word') %>" }
Answer the question
In order to leave comments, you need to log in
It cannot work like that. JS is compiled once during deployment, t(...) is calculated per request. Either everything related to localization should be duplicated in js code, or translations should be pulled through ajax. You can also pervert and convert yml to json at the build stage, forward them to routes and pull up the entire set of translations depending on the locale.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question