Skip to content

Instantly share code, notes, and snippets.

@bazzel
Last active July 27, 2024 16:48
Show Gist options
  • Save bazzel/ecdff4718962e57c2d5569cf01d332fe to your computer and use it in GitHub Desktop.
Save bazzel/ecdff4718962e57c2d5569cf01d332fe to your computer and use it in GitHub Desktop.
Webpacker and I18n
$ rails new my-i8n --webpack

Gemfile

gem 'i18n-js'
$ bundle --binstubs
$ yarn add i18n-js
$ bin/rails webpacker:install:erb

app/javascript/i18n-js/index.js.erb

import I18n from "i18n-js"
I18n.translations = <%= I18n::JS.filtered_translations.to_json %>;
export default I18n

app/javascript/packs/application.js

import I18n from 'i18n-js/index.js.erb'
console.log(I18n.t('hello'))
$ bin/rails g controller welcome index

config/routes.rb

root 'welcome#index'

app/views/layouts/application.html.erb

<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
$ bin/rails s
@OtherCroissant
Copy link

How can I make sure webpacker invalidates the cached translations and regenerate the i18n-js/index.js.erb?
When I change the translations that are used in my JS, the translations are not updated in I18n.translations.

@strickland84
Copy link

Ditto @cunknown, @bazzel is there a way to auto-update these without reloading the server when making changes in dev? Thanks for the gist, very helpful

@bazzel
Copy link
Author

bazzel commented Sep 10, 2019

@cunknown @strickland84 don't have an answer for that, I'm afraid. To be honest: I didn't get that far when writing this up, so I think this needs some more investigation.

@kfiltschew
Copy link

kfiltschew commented Sep 18, 2019

If you want to use the rails set locale, you can take this solution:

app/layouts/applcation.html.erb - save default locale in a global variable (LOCALE) you can access everywhere.

...
<script type="text/javascript">
  var LOCALE = "<%= I18n.locale %>";
</script>
...

app/javascript/i18n-js/index.js.erb - use the global variable LOCALE

import I18n from "i18n-js"

I18n.translations = <%= I18n::JS.filtered_translations.to_json %>;
I18n.locale = LOCALE;

export default I18n;

Other points should match the example.

@hadees
Copy link

hadees commented Nov 21, 2019

can you do this without using erb? I'm having other problems with erb and webpacker.

@dev-alihaider
Copy link

Great

@fongfan999
Copy link

Awesome

@Uepsilon
Copy link

👍 thanks man, that saved me at least a few hours.

@nathan-ch
Copy link

nathan-ch commented Sep 29, 2020

Hi, we are changing our front end to use only React as a Single Page Application inside the Rails app with Webpacker.

I wonder if i can use i18n-js to translate html generated from React.
With this piece of code, i am able to receive translations in React code but the js import of I18n only gives defaultLocale, not the currentLocale.
I can write code to get currentLocal from localStorage or cookies easily but is there a way to handle url without rewriting the detection functions in js ?

@t3k4y
Copy link

t3k4y commented Nov 30, 2020

Anyone managed to combine this with jest tests?

@m-andreas
Copy link

m-andreas commented Jul 19, 2021

Thanks for the Hint @kfiltschew! So you can just use:

import I18n from "i18n-js"
I18n.translations = <%= I18n::JS.filtered_translations.to_json %>;
I18n.locale = "<%= I18n.locale %>";
export default I18n

in i18n-js/index.js.erb

@huzaifa-malik
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment