I have locales that come in the form of en-GB, en-US, de-DE, de-AT, de-CH etc..
Now the issue is that de-DE, de-AT, de-CH all share the same translations.
So having to define them multiple times is annoying and not very DRY.
Before anyone suggests just using :de as locale, I can't. That's how I did it before and due to some business logic I can't change the new en-GB format was forced onto me.
Help would be very much appreciated.
If you're using YAML for you localization then you already have necessary tools. YAML allows you to write something like mixins:
en: &english
hello: Hello, %{username}!
en-GB:
<<: *english
en-US:
<<: *english
I hope you got the idea of DRYing here.
Still DRY still but more importantly your translations aren't duplicated in memory :
Create an i18n initializer that includes fallbacks :
require "i18n/backend/fallbacks"
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
And move your translations into de: and en: locales.
A call like this
I18n.t :foo, :locale => 'de-AT'
will first look for de-AT.foo and providing there is no match, will look for de.foo.
More info on the i18n wiki.
Related
Is there way to get time_ago_in_words in different languages, or write locales?
Here’s my investigation:
time_ago_in_words calls distance_of_time_in_words (https://apidock.com/rails/ActionView/Helpers/DateHelper/distance_of_time_in_words)
The api docs for that method mention translations in the scope of datetime.distance_in_words.
If you look for distance_in_words in a default locale, such as this en.yml (https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml) you’ll see all the available translations in that space. You can redefine any of these in your locale file.
If, however, you’re just trying to change the language it uses, it seems I18n contains a great many default locales, you need to set the locale either as a default within your app, or from the locale defined in the browser.
To set the default for the app, you’ll need a line like this in config/application.rb
config.i18n.default_locale = :de
You can see the list of default locales here (use the part before .yml) https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
You can look at the rails i18 locale file en.yml https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml and can use their predefined translations. If you want to change the translations you can also override the en.yml file in your repository.
Here is in our yml file:
Or make up unique one like login#a.com : 'test'
But here is the error on view page for I18n.t("Or make up unique one like login#a.com"):
translation missing: zh-CN.Or make up unique one like login#a.com
What causes the missing translation of t()?
UPDATE:
Just verified that the problem was caused by # sign. Now the question becomes how to use # in translation key.
I highly recommend the Rails i18n guide for beginners.
It will help you understand how to structure your files, how to setup routes to support a locale param, how to set the locale, and other useful tips.
The t helper is not intended to take a string of text to translate. It takes a yaml key and outputs the translated text from a locale file.
You want to structure your yaml file as such:
config/locales/en.yml
en:
some_key: hello world
config/locales/cn.yml
cn:
some_key: hello in Chinese
Then using it in your views
<%= t :some_key %>
Based on the I18n.locale setting, the t helper will look up :some_key in the corresponding locale file and output the translated text.
I generated some CRUD interface to manage my tables. I put validations in the models. But the returns error messages in English. I would like to return this message in Portuguese because my client is in Brazil. Is there any way I can do that?
Within your config/application.rb probably in line #34 you have commented out
# config.i18n.default_locale = :de
You should have this:
config.i18n.default_locale = :pt # if pt means Portugese, I'm not sure now...
You would probably need to put some translations in config/locales/pt.yml. More about translating your Rails app you will find in Rails Guides i18n section
Please, take a look in the internationalization guide: http://guides.rubyonrails.org/i18n.html
I'm using the standard Rails I18n API to localise some of our views. This is working really well, but we now have a few use cases for regional changes to the en locale.
The API guide mentions that this isn't supported directly, and other plugins should be used. However, I'm wondering whether there's a simpler way to do this.
I already have en.yml, so in theory I could just create en-AU.yml and en-US.yml which are effectively clones of en.yml but with a few regional changes applied. I could then add additional English - American and English - Australian options to our configuration which would map to the new region-specific locales and allow users to use a region-specific locale.
The only problem I can think of with this is that it isn't DRY -- I would have duplicate translations for all common English words. I can't see a way around this.
Are there any other disadvantages to this approach, or should I just bite the bullet and dive into one of the plug-ins such as Globalize2 instead?
The rails-i18n-translation-inheritance-helper is getting a bit old now, so here's my approach for a Rails 3.2 project.
If you keep both en-US and en-AU in the same en.yml file you can use the yml repeated node to have a super en section:
For example:
en: &en
errors:
messages:
expired: "has expired, please request a new one"
not_found: "not found"
en-US:
<<: *en
en-AU:
<<: *en
errors:
messages:
not_found: "tis not found"
I'm using translation inheritance helper plugin for this.
In newer versions of Rails/i18n, they've added a fallback feature. Works similar to the outdated translation inheritance helper gem
see this answer for more info: Fall back to default language if translation missing
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