I have a Rails 3 app in which I'm using I18n.available_locales to display a list of available languages for user accounts. In my config/locales directory I only have en and nl yml files, however, I18n.available_locales returns a variety of other locale keys which I'm assuming are present due to translations being present in the gems that I'm using. Is there a common/standard way to get the method to only return the translations present in the app itself?
I would suggest, you use this gem : rails-i18n , it provides basic translations for every languages, and then, restrict available locales with
config.i18n.available_locales = ['es-CO', :de]
Of course, rails-i18n is not mandatory here, you could simply define available_locales in your config/application.rb file
And as said in the comments, you should definitely accept some answers to improve your accept_rate
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.
I have a rails app the uses the standard i18n translations gem. It's a multi-tenant app and relies on parsing the current tenant from the subdomain.
en:
foo:
bar: "Welcome to %{subdomain}"
I have a few keys (e.g. subdomain above) that I'm using very often in my translations. In my view I have to keep passing in the same key in various places
t("foo.bar", subdomain: current_subdomain)
Is there a way I can specify a default set of keys (like subdomain) that are always passed to the translations by default so I don't have to type it out all the time?
Thanks!
I'm using gem route_translator for some solutions.
While using Faker with rails, it generates sentences in some other language. I want them to be generated in English. What kind of settings do I need to change to do it?
Docs say
Just set Faker::Config.locale to the locale you want, and Faker will take care of the rest.
https://github.com/stympy/faker/blob/master/README.md
https://github.com/stympy/faker/tree/master/lib/locales
Faker::Config.locale = :en
Hoping some learned Rails developers here can recommend an existing Ruby on Rails plugin or gem that allows you to continue using the Simple I18n backend whilst allowing you to optionally specify translations in the database.
Here's why:
I have one Rails app used for many websites. For the example I'll just use 2 websites:
Website 1: Leprechauns R Us
Website 2: Unicorns R Us
Most translations are the same for both websites, but occassionally I want to override a translation. For example, in my en-US.yml file I have the following translation:
view_all: View all
And for most websites this translation is fine, including for website 1 (Leprechauns) where I'm happy to use "View all".
However, for website 2, I'd like to use "View all Unicorns" as the view_all translation and I'd like to specify this in the database. For maintenance reasons I don't want to specify this override in a YAML file.
Many thanks,
Eliot
In the end I opted to take advantage of Rails' I18n::Backend::Simple's ability to process both .yml files and .rb files as locale dictionaries.
Artifacts created:
DB migration to create a translations table with columns: locale, key, text
Translation model to map to the translations table
Class method to_locale_hash on Translation model that returns a locale keyed hash as required by I18n::Backend::Simple.load_rb
A single-line file located at config/translations.rb with the line 'Translation.to_locale_hash'
For the source code see the Spree extension (sorry not in a Rails plugin structure, it will be easy to move over to a plugin should you require it) here:
http://github.com/eliotsykes/spree-i18n-db/tree/master
When using Warbler, what line(s) do I need to add to config/warble.rb to keep it from including Active Record in the bundled gems. I already have excluded Active Record in config/environment.rb as shown below.
config.frameworks -= [ :active_record ]
I tried the same thing only using config.gems in config/warble.rb, but to no avail.
I haven't been able to try either of these ideas, but looking at Nick Sieger's examples :-
Does the gem name have to be a String rather than a Symbol?
It looks like activerecord maybe being included implicitly because config.gems includes rails and config.gem_dependencies = true. Maybe you need to change config.gem_dependencies to false and explicitly include rails, actioncontroller, etc in config.gems.
It might be instructive to print out or log the value of config.gems from within the warble.rb file.