Rails admin views' titles & views' links - ruby-on-rails

I'm working with a RESTful Rails App since long time ago.
But this week I received a requirement to change the titles for a Rails App. I think it could be easy. But I'm reading the views (in hamlet)
And found the titles are not written and come from other place. I found they use things like:
label: t("common.email")
f.input :password_confirmation,
ep.check_box :followed_issues
link_to t('common.profile'), csn_profile_path
I wrote a lote of code in rails' controllers but this is the first time I'm trying to understand rails views. Is this standard? Where can I find all this information? is this the way to do?

I think what you are looking for is Rails internationalization.
The internationalization allows use the same views but with different languages. You can find the translations in config/local/, there you will find one yml file per language.
So, when you use t("common.email") it will search the translation in config/local/[language_abbreviation].yml
For example en.yml should look like:
common:
welcome: "Welcome to my page"
email: "Email"
bye: "Bye bye"
And, at the same time es.yml:
common:
welcome: "Bienvenido a mi pagina"
email: "Correo electronico"
bye: "Adios"
Thus, you only need to find the translation and change it for each language and it will change the titles in the views.

Related

Rails own config/initializers updated and doesn't display changes

I work on a web app that was already coded, and I have to bring some new features to improve it. I have to add 2 field in the rails admin, and there are some file type that I have never work with like .yml files.
I have a file called : simple_form.fr.yml, that permit to use on the view : human_attribute_name on a class. but I made a modification on a line (change "hello" to "hello world" for example) and I can't see the modification on the view ...
Should I did a command line to make modifications available ?
Thank ! (don't hesitate to ask if you need more precisions / code, I'll update my question)
EDIT
The yml document :
fr:
simple_form:
labels:
school:
code_online_access: "Accès code en ligne"
the line that I change is code_online_access, I juste change the content of the string and in the view I have this line :
<%= School.human_attribute_name("code_online_access") %>
The problem is that you're changing a label for simple_form, but expect the change to show up in human_attribute_name. This method does not look in simple_form's section.
Take a look at this section of I18n rails guide to learn correct place to put your translations to.
en:
activerecord:
models:
user: Dude
attributes:
user:
login: "Handle"
# will translate User attribute "login" as "Handle"
then
User.human_attribute_name("login") # => 'Handle'

I18n to call different Model Attribute depending on Locale?

So I am building an on-line shop and I want two language options, English and Spanish.
I am using I18n as you would normally do for all my static text and headings ect.
But, I have a products Model that can have new Products created for listing on the site. This has fields like :name_en and :name_es, :description_en and :description_es ect.
When the admin uploads a new product they obviously need to add the English and the Spanish text.
Because I have only 2 locales what I would like to do i think is call something like
<%= Product.name_"#{I18n.locale.downcase}" %>
But obviously this does not work. How can i, or just can I, interpolate a method or Attribute?
Have I missed something obvious here and just going about it the wrong way or is there a way to do this along the lines of my thinking?
Any Help massively appreciated.
Thanks
You can use send method. Try something like:
<%= Product.send("name_#{I18n.locale.downcase}") %>
Just a word of explanation, the following are equal:
string = "Hello"
string.upcase
# => "HELLO"
string.send("upcase")
# => "HELLO"
Hope that puts you in proper direction!
Good luck!

rails i18n adjectives

I'm currently stuck with a small but not so uncommon problem in rails i18n ( hopefully I just haven't used the wrong search terms...)
In a standard rails crud app, you often have models like "contract", "group" etc..
So far, if you want a button for "new group" oder "new contract", this is simple in the english language, since it's basically always something like "new" + model_name.
Unfortunately, languages like german are not so computer-friendly :)
"new contract" translates to "neuer vertrag" while "new group" translates to "neue gruppe" (note the adjective change).
So, no problem with pluralisation this time but with adjective changes.
Is there any rails i18n support for such cases?
Don't want to use ugly i18n concatenation :)
thanks a lot!
just to understand your question correctly, you don't want to translate 'new' to german?
What is wrong with:
en:
new_contract: New contract
new_group: New group
de:
new_contract: Neuer Vertrag
new_group: Neue Gruppe
This is the standard way to do it and I don't see any advantage of DRYing up the 'new'. You remove something from the context there. Btw. you have this problem in a lot of languages. It is not possible to translate an adjective just once and then reuse, since it belongs to another word (cannot stand in its own context).
I would definitely go with the approach above.

Keeping track of changes using rails - "changed?"

I am building a multi lingual website, using ruby on rails, where part of the content is supposed to be user generated and they are supposed to be able to create different versions of it for all languages. The language support is handled by i18n gem.
Part of their content is created using Markdown through http://daringfireball.net/projects/markdown/basics .
In my database I save: object.content_markdown_en, object.content_html_en, object.content_markdown_sv, object.content_html_sv and so on for the different locales.
Now if a user changes the content, new html is supposed to be generated. But it seems unnecessary to regenerate the html for all locales if he only made changes in one of the languages.
I thought there might be some way to use something like
if object.content_markdown_[locale]_changed?
generate_new_html
end
that can be run in a loop for all possible locales. But I can't find any nice ways of doing this.
How about:
[:en, :sv].each do |locale|
if object.send("content_markdown_#{ locale }_changed?".to_sym)
send("generate_new_#{ locale }_html".to_sym)
end
end
You can use send to call methods by name:
object.send("content_markdown_#{locale}_changed?".to_sym)
Your loop would look like this:
%w(en sv).each { |locale|
if object.send("content_markdown_#{locale}_changed?".to_sym)
generate_new_html
end
}
However, using a separate translation table might be a better approach.

Hook into Rails' I18n

I've written a little library to hold translated content in model attributes. All you have to do is add the following to a model:
class Page < ActiveRecord::Base
i18n_attributes :title, :content
end
By convention, the data is written to the real attributes i18n_title and i18n_content as a hash (or hstore hash for Postgres). And a number of getters and setters give you access to "localized virtual attributes":
page = Page.new
page.title_en = 'Hello'
page.title_es = 'Hola'
page.i18n_title # => { en: "Hello", es: "Hola" }
I18n.locale = :es
page.title # => "Hola"
page.title_en # => "Hello"
You can use these virtual attributes in forms as well, but there's a downside: The form builder uses I18n to get the label and translate attribute validation errors. And it does of course look for keys such as activerecord.attributes.page.title_en if you use title_enin the form.
It would be very cumbersome to replicate the same translation for every available_locale in the locales/en.yml etc files:
activerecord:
attributes:
page:
title_en: "Title"
title_es: "Title"
...
What I'd like to do is execute some code after Rails has loaded all locales in the boot process and then clone translations for these keys. Is there a way to do this? Maybe a hook which gets called after the translations have been loaded from the YAML files? (The translations are not yet loaded when my lib loads.)
Or do you see another way to tackle this problem? (I've tried to alias I18n.translate, but I'm afraid this might cause major headache in the future.)
Thanks for your hints!
Although you dropped this approach, please let me share my thoughts:
I don't think it is incredible useful to add other locale strings into a translation file for a specific localization. Since a config/locales/$locale.yml usually starts (at least in my case) with
$locale:
...
there is no need for activerecord.attributes.page.title_es in an English localization file. I'd just put it in es.yml as activerecord.attributes.page.title.
I mean: isn't that the whole purpose of separate localization files? (Or from the developer/translator point of view: In which file should I search for .title_es, in en.yml, es.yml or both?)

Resources