$ 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
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 ?