Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use i18n-js with webpacker in rails 6?

I just switched to Rails 6 (6.0.0.rc1) which uses the Webpacker gem. I want to use i18n-js in some of my modules for translation purpose. How can configured i18n in my application with webpacker ?

like image 322
code_aks Avatar asked Oct 16 '22 08:10

code_aks


1 Answers

Sorry for the late answering but I have already resolved this issue as below :-

yarn add i18n-js

After this I have changed in below files

## javascript/packs/application.js

require("@rails/ujs").start()
require("@rails/activestorage").start()
require("channels")

window.jQuery = window.$ = require('jquery')

var Turbolinks = require("turbolinks");
Turbolinks.start();

import I18n from 'i18n-js'
window.I18n = I18n

And then to load all translations :-

## application.html.erb

<script>
   var language = "<%= I18n.locale %>";
   I18n.translations = <%== I18n::JS.filtered_translations.to_json %>;
</script>

Now translations are ready to use in js files:-

I18n.t('a.hello')
like image 198
code_aks Avatar answered Oct 18 '22 13:10

code_aks