gwalshington
gwalshington

Reputation: 1495

i18n-js translations not updating with additional translations in .yml

I have been reading about this for days, and nothing seems to be working. I have seen a lot of documentation of this issue, but none of the work arounds are working for me. I have :

  Rails 5.0.1
  * sprockets (3.7.1)
  * sprockets-rails (3.2.0)
  * i18n (0.7.0)
  * i18n-js (3.0.0.rc15)

config/i18n-js.yml

translations:
- file: "app/assets/javascripts/application/i18n/translations.js"
  only: '*.js*'

config/application.rb

    config.middleware.use I18n::JS::Middleware

When I add new translations to the corresponding yml file, the i18n/translations.js does not update to include the new .yml translations.

For example, in en.yml:

en:
 form_error:
    tos_check: "You must agree to Lexody's Terms of Use to continue."
    choose_city: "Please select a city from the menu."
    cancel_reason: "Please provide a reason for cancelling."

$('.prompt').html('<p style="color:#e57373">' + I18n.t('form_error.cancel_reason') +'</p>');

returns: [missing "en.form_error.cancel_reason" translation]

I have tried:

Deleting translations.js and run rake i18n:js:export

rake tmp:cache:clear

rake assets:precompile

Does anyone have another solution I can try? Thanks!!

Upvotes: 4

Views: 4816

Answers (4)

wjordan
wjordan

Reputation: 20390

Update

After looking at the additional configuration files, this config/i18n-js.yml seems suspect:

translations:
- file: "app/assets/javascripts/application/i18n/translations.js"
  only: '*.js*'

According to the export configuration docs, the only key refers to the translation keys to be exported, not the filenames. So '*.js*' will match nothing, causing no translations to be exported.

Change this file to read:

translations:
- file: "app/assets/javascripts/application/i18n/translations.js"
  only: '*'

(Original answer below)

Working example

Here's a minimal, working example that produces expected behavior with the i18n-js gem:

#!/bin/bash
rails _5.0.1_ new .
echo "gem 'i18n-js', '3.0.0.rc15'" >> Gemfile
echo "  NEW_KEY: NEW_VALUE" >> config/locales/en.yml
bundle install
bundle exec rake i18n:js:export
grep -o '"NEW_KEY"' public/javascripts/translations.js

For me, running the above script outputs "NEW_KEY" on the last line, as expected (demonstrating that NEW_KEY is correctly added to public/javascripts/translations.js after running i18n:js:export in a fresh Rails installation), which means something else is going on in your local project.

Further investigation

In order to know what else exactly is going on, you'll have to further investigate exactly what configuration you've changed locally when compared to a fresh Rails installation.

(Note that the easiest way to do this is to provide a Minimal, Complete and Verifiable example, such as a link to a GitHub repo that produces your issue exactly.)

  • you say "the i18n/translations.js does not update to include the new .yml translations.", but the default path that gets updated is public/javascripts/translations.js. If you're using a non-standard path for your translations.js file, do you have additional configuration for this in config/i18n-js.yml? (If so, please share the entire contents of this file).
  • Have you confirmed that the new translation doesn't show up in translations.js at all (e.g., using a grep command like the one above)? Or is it possible that the issue is related to the asset pipeline configuration instead?

Upvotes: 3

Jim DeLaHunt
Jim DeLaHunt

Reputation: 11395

In the .yml file, check for a colon after the language code.

Your example reads:

en
 form_error:
    cancel_reason: "Please provide a reason for cancelling."

Try:

en:
 form_error:
    cancel_reason: "Please provide a reason for cancelling."

I haven't tried exercising an example with this. However, the .yml files in the projects I have copies of all have colon after the language name. And it's just the sort of infuriating typo that might be invisible to someone close to it. Grr!

Upvotes: 0

Lucas Ferronato
Lucas Ferronato

Reputation: 119

It seems that you have the English translation but is trying to access french locale instead.

Upvotes: -1

user2545521
user2545521

Reputation: 63

Not sure if you mean dev or prod env. I had a similar problem in my dev env and I solved it by adding config.middleware.use(I18n::JS::Middleware) to config/application.rb. You can check it here. Hope it helps.

Upvotes: 0

Related Questions