Error I18n::InvalidLocaleData - ruby-on-rails

I want use I18n and I already follow on how to do that from railscast but the thing gone wild and I don't know where the mistake, had tried check the format, tried several ways but still can't be done.
my en.yml
en:
category:
index:
title: "Listing Categories"
name: "Name"
is_active: "Is Active"
my view
<%= t 'category.index.title' %>
But return I18n::InvalidLocaleData in Categories#index and can not load translations from /home/lenovo/cost_control/config/locales/en.yml, expected it to return a hash, but does not
I had try on my en.yml just:
en:
title: "Listing Categories"
and can work perfectly, but when I adding more line, just return me those error. I'm sorry I just not so advanced yet in rails, thank you for the help you guys :D really.

It seems your YAML is broken. Didn't you use tabs instead of spaces?
You can use YAMLlint to check YAML for validity.

The validator Ilya recommends seemed pretty basic and not so great
Found this one which is better https://codebeautify.org/yaml-validator

Related

No translation for devise flashes

I have ru.yml and en.yml files, the section I need looks like this:
en:
devise:
failure:
already_authenticated: You are already signed in.
ru:
devise:
failure:
already_authenticated: Вы уже вошли в систему.
The thing is, that no matter what language the user chooses, this (and several others) alert messages will be in russian.
My controllers have around_action :localize_request, which should determine the locale. And it works just fine for any other funcionality on the site, except for this particular case.
I think it could be a possible bug from devise. Take a look at this issue they discussed something similar and created a PR to fix it that I think is the reason why you can't translate your message.
As a workaround, maybe you can try to make this change to test if it works:
ru:
devise:
failure:
user: # <<<
already_authenticated: Вы уже вошли в систему.
Looks like it's a bug in the Devise gem -- https://github.com/heartcombo/devise/issues/5247

Trouble on translating views after renaming statements from 'translate' to 'I18n.t' and from 'localize' to 'I18n.l'

I am using Ruby on Rails 3.2.2 and I have a strange problem. In my view files I use the following code:
# app/views/articles/show.html.erb
I18n.t('.page_title')
When I render the above view code in the browser I get the translation missing: en.page_title message. However, as you can see from the "missing" message, the translation doesn't refer to the articles.show.page_title YML statement... but it should do that! It seems that the “Lazy” Lookup doesn't work as expected.
Why that happens? Have you some idea about the issue and how to solve it?
Note: I just made a code refactoring renaming statements from translate to I18n.t and from localize to I18n.l...
Try simply: t('.page_title') Then you should see another statement. But in fact, I18n searches in several places. You can add the translation and see for yourself:
en:
page-title:
Or:
en:
articles:
show:
page-title:

Hash inside YAML file?

I want to include a hash and list inside a YAML file that I'm parsing with the following command:
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")
My YAML file looks like this:
feeds: [{:url => 'http://www.google.com', :label => 'default'}]
But this doesn't seem to work.
How would I go about achieving such a thing?
Thanks,
Yuval
EDIT: Sorry, guys. I'm still unclear about how to do this and I suspect it is in part due to my somewhat vague phrasing. I asked a better-phrased, more broad question here. Thank you!
You can mark it up like this
feeds:
-
url: 'http://www.google.com'
label: 'default'
Note the spacing is important here. "-" must be indented by a single space (not a tab), and followed by a single space. And url & label must be indented by two spaces (not tabs either).
Additionally this might be helpful: http://www.yaml.org/YAML_for_ruby.html
Ceilingfish's answer is maybe technically correct, but he recommends to use a white space at the end of a line. This is prone to errors and is not a good practice!
This is how I would do it:
Create a settings.yaml file with the following contents:
---
feeds:
:url: 'http://www.google.com'
:label: 'default'
This will create the following hash after the YAML file has been loaded:
irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> YAML.load_file('settings.yaml')
=> {"feeds"=>{:url=>"http://www.google.com", :label=>"default"}}
irb(main):003:0>
In this example, I also use symbols since this seems to be the preferred way of storing Ruby keys in Ruby.
Old question, but since I was in a similar spot... Like Jasper pointed out, Ceilingfish's answer is correct. But you can also do
feeds:
- url: 'http://www.google.com'
label: 'default'
to avoid having to rely on trailing whitespace after the dash.

Missing en-UK.yml file for Rails app

I'm trying to switch one of my websites into en-UK so that I get the correct date and currency formats etc...
I have found this yaml file:
http://github.com/mattetti/globalite/blob/master/lang/rails/en-UK.yml
Any ideas if there is a better one to use?
I also checked here but could not see it:
http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
Thanks,
Nick
I have found a better solution than to keep a duplicate version of en.yml and simply change the $ to £.
There is a plugin that lets you only override the values you require:
http://github.com/javan/rails-i18n-translation-inheritance-helper
config/environment.rb
config.i18n.default_locale = 'en-UK'
and then create:
config/locales/en-UK.yml - for special cases
en-UK:
number:
currency:
format:
unit: '£'
format: '%u%n'
config/locales/en.yml - for all English translations
en:
btn_submit: Submit
This works a treat and will also mean that I don't need to maintain the file apart from any special cases like above.
View
=t 'btn_submit' #Submit
=h number_to_currency(#price, :precision => 0) #£1,000
Hope this helps others as it took a while to find a solution.
I think you're looking for en-GB :)
https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en-GB.yml

How to use Rails I18n.t to translate an ActiveRecord attribute?

Trying to use my active record translations in config/locales/de.yml also in my views. I thought I am clever using this:
de:
activerecord:
attributes:
user:
login: "Benutzerkennung"
comment: "Bemerkungen"
And in my view this:
<%= label_tag :login, t('activerecord.attributes.user.login') %>
But instead of the translation value ("Benutzerkennung") I am getting the infamous
"translation missing: de, activerecord, attributes, user, login"
Has anybody got this working (not using the label translation plugin (I am wary of potential side effects), or User.humanize_attribute_name)? What am I missing? (it does work when I use "activerecord1" or something else than activerecord, so my setup seems to be fine)
Thanks!
Ok, my bad, it does work just fine. I fell in the YML formatting trap :(
To help you debug along the way, use "script/console" and the following statements:
- I18n.locale --> should output the locale you want to examine
- I18n.t('activerecord.attributes') --> should give you all the key/value pairs for your translation, if not, you made a formatting error in your YML file or it could not be found
And btw - the plugin works quite well http://github.com/iain/i18n_label/
if you don't like the result of ".human_name" (which the plugin uses), just fall back to I18n.t('your key')
Another method:
<%= label_tag :login, User.human_attribute_name(:login) %>
You should upgrade Rails gem to v2.3.11 (I tried to use 2.3.9, but now days it is not available so I suggest you 2.3.11)!
gem install -v=2.3.11 rails
You can find this issues documented here: Rails 2.3.9 Release notes

Resources