Localization (i18n)
Avo leverages Rails' powerful I18n
translations module.
Localizing Avo
When you run bin/rails avo:install
, Rails will not generate for you the avo.en.yml
translation file. This file is already loaded will automatically be injected into the I18n translations module.
Localizing resources
Let's say you want to localize a resource. All you need to do is add a self.translation_key
class attribute in the Resource
file. That will tell Avo to use that translation key to localize this resource. That will change the labels of that resource everywhere in Avo.
# app/avo/resources/user.rb
class Avo::Resources::User < Avo::BaseResource
self.title = :name
self.translation_key = 'avo.resource_translations.user'
end
# avo.es.yml
es:
avo:
dashboard: 'Dashboard'
# ... other translation keys
resource_translations:
user:
zero: 'usuarios'
one: 'usuario'
other: 'usuarios'
Localizing fields
Similarly, you can even localize fields. All you need to do is add a translation_key:
option on the field declaration.
# app/avo/resources/project.rb
class Avo::Resources::Project < Avo::BaseResource
self.title = :name
def fields
field :id, as: :id
# ... other fields
field :files, as: :files, translation_key: 'avo.field_translations.file'
end
end
# avo.es.yml
es:
avo:
dashboard: 'Dashboard'
# ... other translation keys
field_translations:
file:
zero: 'archivos'
one: 'archivo'
other: 'archivos'
Localizing buttons label
BetaThe avo.save
configuration applies to all save buttons. If you wish to customize the localization for a specific resource, such as Avo::Resources::Product
, you can achieve this by:
---
en:
avo:
resource_translations:
product:
save: "Save the product!"
Setting the locale
Setting the locale for Avo is pretty simple. Just use the config.locale = :en
config attribute. Default is nil
and will fall back to whatever you have configured in as config.i18n.default_locale
in application.rb
.
Avo.configure do |config|
config.locale = :en # default is nil
end
That will change the locale only for Avo requests. The rest of your app will still use your locale set in application.rb
. If you wish to change the locale for Avo, you can use the set_locale=pt-BR
param. That will set the default locale for Avo until you restart your server.
Suppose you wish to change the locale only for one request using the force_locale=pt-BR
param. That will set the locale for that request and keep the force_locale
param in all links while you navigate Avo. Remove that param when you want to go back to your configured default_locale
.
Related:
- Check out our guide for multilingual records.
Customize the locale
If there's anything in the locale files that you would like to change, run bin/rails generate avo:locales
to generate the locale files.
These provide a guide for you for when you want to add more languages.
If you do translate Avo in a new language please consider contributing it to the main repo. Thank you
FAQ
If you try to localize your resources and fields and it doesn't seem to work, please be aware of the following.
The I18n.t method defaults to the name of that field/resource
Internally the localization works like so I18n.t(translation_key, count: 1, default: default)
where the default
is the computed field/resource name. So check the structure of your translation keys.
# config/locales/avo.pt-BR.yml
pt-BR:
avo:
field_translations:
file:
zero: 'arquivos'
one: 'arquivo'
other: 'arquivos'
resource_translations:
user:
zero: 'usuários'
one: 'usuário'
other: 'usuários'