rails-i18n
Repository for collecting Locale data for Ruby on Rails I18n as well as other interesting, Rails related I18n stuff
I am making my website multilanguage using this railscast
But at the very beginning I get an error:
I18n::InvalidLocaleData in Users#index
Showing .../app/views/users/index.html.erb where line #1 raised:
can not load translations from .../config/locales/en.yml, expected it to return a hash, but does not
index.html.erb:
<% provide(:title, t('users.index.title.site_title')) %>
<h1><%= t 'users.index.title.head' %></h1>
<%= form_tag users_path, :method => 'get' do %>
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag (t 'users.index.search_form.search'), :name => nil %>
</p>
<% end %>
config/locales/en.yml
en:
users:
index:
title:
site_title: "Users"
head: "Users"
search_form:
search: "Search"
Source: (StackOverflow)
I have a nested attribute on which I perform presence validation. I am trying without success to provide translations for the attribute name that is returned in the full error message text.
The model is called Identity
and contains an attribute called identity
The model is nested within another with a has_many
relationship.
A typical error message is currently returned like this
Identities identity can't be blank
I want to translate the attribute (by default Identities identity
) into something else.
I have
en:
activerecord:
models:
identity:
identity: "whatever"
If I do this, I get an error
I18n::InvalidPluralizationData (translation data {:identity=>"whatever"} can not be used with :count => 1):
I have tried to add pluralisation data to this by altering the above to
en:
activerecord:
models:
identity:
identity:
one: "one"
other: "other"
This changes the error to
I18n::InvalidPluralizationData (translation data {:identity=>{:one=>"one", :other=>"other"}} can not be used with :count => 1):
I have also tried many
instead of other
without difference.
I have spent a few hours trying to make this work, having read other questions on Stack Overflow and elsewhere, without success. What is the correct way to write translations for attribute names ?
Source: (StackOverflow)
I want I18n.translate()
or I18n.t()
to use a specific locale, but not I18n.locale.
I don't want to use I18n.t(:my_key, locale: :my_locale)
every time, so it would be great if I could override the function itself.
I tried to put it in a new helper:
# my_helper.rb
module MyHelper
def translate(key, options = {})
options[:locale] = MY_LOCALE
I18n.translate key, options
end
alias :t :translate
end
This works fine for "hard keys" like t('word')
, but doesn't find the right path for "dynamic keys" like t('.title')
, which should use the path of my partial, i.e. de.users.form.title
.
Thanks for any help!
Source: (StackOverflow)
What is the recommended approach to maintaining multi-language values within an ActiveRecord model.
I am looking into upgrading our database schema and Object Models to allow for widespread internationalisation of many of the values, and I am weighing up various ways to do this.
The standard rails-i18n system is largely silent on this, although it offers powerful tools for internationalising field and model names, in addition to the text within views.
The R18n gem allows you to overload your database with columns that store the localised strings, and which present the correct value depending on the locale. This presents a couple of problems.
Say we are talking about a model Sport
— database table sports
. We need to be able to search for Sport.where(name: 'soccer')
even though in the UK they call it 'football', so the query becomes scope :with_name ->(n){ where("name_en_GB = ? OR name_en_AU = ?", n, n) }
.
If we want to add another locale we need to both update the schema, and update any such queries on that schema. A rather brittle solution.
Another solution I've seen is to maintain a separate SportLocale
model and associated sport_locales
table, that holds the name and locale.
Assuming
class Sport < ActiveRecord::Base
has_many :locales
end
class SportLocale < ActiveRecord::Base
belongs_to :sport
end
Then to find the right sport you'd do something like
class Sport < ActiveRecord::Base
has_many :locales, class_name: "SportLocale"
self.with_name(n)
SportLocale.where(name: n, locale: I18n.locale).first.try(:sport)
end
end
This is fine if Sport
is your only localised model but when you start adding all the other models it becomes a bit crazy, with each of them needing an associated *Locale model. Not the DRY
est of solutions either.
I'd like a solution that allows
class Sport < ActiveRecord::Base
include Localised
localised_field :name
end
and magically Sport.where(name: 'football')
will find the right sport.
Is there any such system out there already, or would I have to build it myself?
How are other projects dealing with this sort of problem?
Source: (StackOverflow)
on default, the subject for invitation mail is
mailer:
invitation_instructions:
subject: 'Invitation instructions'
I'd like to change it to
subject: '%{invited_by} has invited you!'
but this requires to have invited_by variable accessible to the translate method for i18n.
How can I have this variable accessible/declared without changing default behavior too much?
Source: (StackOverflow)
I'm guessing that rails stores all the parsed translations yml files in a sort of array/hash.
Is there a way to access this?
For example, if I've a file:
en:
test_string: "testing this"
warning: "This is just an example
Could I do something like, I18n.translations_store[:en][:test_string] ?
I could parse the yml file with YAML::load, but in my case I've splitted the yml files in subfolders for organization, and I'm pretty sure that rails already parsed them all.
Source: (StackOverflow)
This might be I18n-ception but lets say I have an en.yml
file as below
en:
my_var: Foo
my_message: "This is a message where I'd like to interpolate I18n's %{my_var}"
Is there a way to indicate to I18n that %{my_var}
should be the my_var
key in en.yml
?
I know I could accomplish it by doing something like
I18n.t 'my_message', :my_var => I18n.t('my_var')
but I was hoping I18n has a way to self reference keys.
Source: (StackOverflow)
Using shorter i18n keys (e.g. t '.submit_button'
) in Rails views makes them easier to type, but is it actually good? When later you decide to refactor your views and partials you have to remember to update the respective localization entries. Wouldn't it be more robust to name them by their business meaning and always specify the full key-name?
Source: (StackOverflow)
In Rails i18n, how to get all values for a certain key using the following:
translations = I18n.backend.send(:translations)
get all the keys
I need to be able to get a certain section for example only return everything under "home"
en:
home:
test: test
Source: (StackOverflow)
Working on a rails 3 app where I want to check if a translation exists before outputting it, and if it doesn't exist fall back to some static text. I could do something like:
if I18n.t("some_translation.key").to_s.index("translation missing")
But I feel like there should be a better way then that. What if rails in the future changes the "translation missing" to "translation not found". Or what if for some weird reason the text contains "translation missing". Any ideas?
Source: (StackOverflow)
I am trying to use internationalization in rails. Here i've found that for the command
<%= t :hello_world %>
I know that Ineed to define :hello_world in the file config/locales/en.yml like this
# config/locales/en.yml
en:
hello_world: Hello world!
What i want to know is as in django it generates translation files using makemessages, is there any way to do it in rails? It becomes a tedious task to find and write whole translations.
Thanks
Source: (StackOverflow)
I am using I18n with Redis store, and have a strange behavior after updating to Rails 3.2.13
[6] pry(main)> I18n.t("my_website_field")
=> "M"
[7] pry(main)> $redis.get("en.my_website_field")
=> "\"My website\""
I am getting only the first letters of the translations
Source: (StackOverflow)
How can I add parameters to my parametrized and internationalized error message? Say, in my controller there's:
flash[:error] = t(:error)[:my_error_message]
And in en.yml:
error:
my_error_message: "This is the problem XXX already."
Source: (StackOverflow)
I have a model called User
with an attribute called current_sign_in_at
. In my en.yml file I have the display name as such…
en-GB:
activerecord:
attributes:
user:
current_sign_in_at: "Last sign-in"
…which allows me to display the desired form label ("Last sign-in") using = f.label :current_sign_in_at
.
But how can I use this same translation for a table header, i.e. not in a form?
%th= :current_sign_in_at
Source: (StackOverflow)