Skip to content

Instantly share code, notes, and snippets.

@fongfan999
Forked from bazzel/README.md
Created May 28, 2020 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fongfan999/3e156acdbd5a0ca073575de2b8d467ac to your computer and use it in GitHub Desktop.
Save fongfan999/3e156acdbd5a0ca073575de2b8d467ac 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment