Rails problem using I18n in views but not in console - ruby-on-rails

I have to show a translation in a view in a locale different from the current one. I use this code to force the locale for one translation :
I18n.t :what_ever, :locale => 'es'
It works in the rails console but not in the view! I have try many things but I cannot find a solution. The view tell this error :
translation missing: es.what_ever
So, I was thinking it was a trouble from the YAML but exactly the same code works well in rails console....
Any Ideas ?

This seems like answered, but I will give you the solution anyway:
Instead of doing:
I18n.t :what_ever, :locale => 'es'
Do this:
I18n.t 'what_ever', :locale => 'es'

Related

Fast_Gettext Not Displaying Translations in Rails 3.2.13 Application

I have PO files (en.po & fr.po) that I want to use to localize my Rails application into French. I submitted the question https://stackoverflow.com/questions/17203622/translating-a-rails-application-3-2-13-using-po-gettext-files recently to see if I could get any help. I mentioned that I had read some information about Fast-Gettext and another gem. I decided to look at the Fast-Gettext gem since it allows the use of PO files without using a database.
I added the latest versions of fast_gettext and gettext_i18n_rails in my Gemfile. I installed the latter gem to get rid of an undefined method "_" error message even though I have no plans at this point to use the database feature.
I added the following code in config/application.rb.
# add FastGettext configuration
FastGettext.add_text_domain 'my_app', :path => 'config/locales', :type => :po, :ignore_fuzzy => true, :report_warning => false
FastGettext.default_text_domain = 'my_app' # set the default textdomain
FastGettext.default_available_locales = ["en","fr"] # set available locales # (note: the first one is used as a fallback if you try to set an unavailable locale)
FastGettext.default_locale = 'en'
Here is my setup in application_controller.rb to allow the locale to be set and saved using a cookie.
include FastGettext::Translation
before_filter :set_users_locale
def set_users_locale
I18n.locale = FastGettext.set_locale(params[:locale] || cookies[:locale] ||
request.env['HTTP_ACCEPT_LANGUAGE'] || 'en')
cookies[:locale] = I18n.locale if cookies[:locale] != I18n.locale.to_s
end
I added logic where the user can click a flag and set the value of :locale.
<%= link_to_unless_current image_tag("flag_us_30px.jpg", :alt => "Set Language to English"), locale: "en" %>
<%= link_to_unless_current image_tag("flag_fr_30px.jpg", :alt => "Set Language to French"), locale: "fr" %>
When a person clicks the flag it sets the value of :locale correctly. I have my routes formatted as domain.com/:locale/link. Right now the root will include the locale until I add logic to override it.
Here are two statements in my views that I am testing with:
<%= _("Language") %>
<%= _("Note: If you do not understand the text on the icons, use the text links at the bottom of the page.") %>
When I click the French flag to change the value of :locale to "fr" the link changes properly but the code for both strings remains in English. The PO file has the French translation for both of these terms. I would think that if it had not found the PO files that I should be seeing error messages stating it did not find them.
I first attempted to use the configuration code in config/initializers/fast_gettext.rb but did not get any results so I decided to put it in config/application.rb to see if I could get it to work. I also removed ':ignore_fuzzy => true, :report_warning => false' to see if that may change things. However I get the same results.
I am using fast_gettext because back in 2011 #svenfuchs recommended it for using Gettext. I may try and contact him on Twitter since it looks like that is the only place I can find where he is active these days.
Any help would be appreciated.
Why are you using PO files instead of common Rails translations?
Or the globalize3 if you prefer to save the translations on database and add model translations.
There's also a gem to help you converting your existing translations: i18n-translators-tools.

Rails: i18n, variable gets printed if translation is not there

I am facing a weird problem. In my rails 3 app I am supporting internationalization with english and french. Here in my template I wrote something like this
<%= t "Hi %{person}!", :person => "Simpson" %>
When I set locale as french everything works fine, since it has got translation for this but when I set locale as english then it gives output as
Hi %{person}!
in my browser. When I add translation to en.yml, it works fine. I don't understand why there is need to add translation in en.yml for this. Moreover I don't want this to happen, is there any work around for this?
Thanks
The first argument givent to the t method should be a key, so your view should have something like this :
<%= t :greetings, :person => "Simpson" %>
Your config/locales/en.yml would look like this :
en:
greetings: Hi %{person}
and your config/locales/fr.yml something like this :
fr:
greetings: Bonjour %{person}

Rails 3 force localization language for specific text

Any way to force the translation to a specific language only for some text. Something like:
t("my.text",:fr)
The reason is that i want the user to be able to change the language of their content but not the site interface. Thanks!
It could be done this way:
I18n.t :foo, :locale => :fr
equivalent too:
t("my.text", :locale => :fr)
t("my.text", locale: :fr)
More options here

Rails 3 - translating of routes with I18n

I want simple thing. Translate routes with I18n like this
get I18n.t('routes.login') => "devise/sessions#new", :as => :new_user_session
I made file initilizers/locale.rb with
I18n.default_locale = :cz
It works perfectly when i run 'rake routes', but when i run server it ignore default locale and throw 'translation missing message'
Any suggestion what is happening and why ?
I believe, that normally routes are loaded once, even before locale are set, so you simply can't use the logic you have here. You need more advanced stuff to get it to work. Take a look at i18n_routing: http://github.com/kwi/i18n_routing

Rails routing with requirements

with the following routes I try to achive the goal, that I can present static resources like terms of use, imprint and so on in different languages using different urls.
I defined two example routes for my imprint like that:
map.imprint ':lang/impressum', :controller => "statics", :action => "imprint", :requirements => {:lang => /de/}
map.imprint ':lang/imprint', :controller => "statics", :action => "imprint", :requirements => {:lang => /en/}
Now in my view I try to use the path/url helper like that:
<%= link_to(t(statics.imprint.linkname), imprint_url(:lang => session[language])) %>
where there session[:language] is "de" or "en".
Thats results in a working link for the de route. But the english one fails. If I change the order of the routes, it's vice versa, and the english one works, while the german one fails.
The error always reads like that:
imprint_url failed to generate from {:controller=>"statics", :lang=>"de", :action=>"imprint"}, expected: {:controller=>"statics", :action=>"imprint"}, diff: {:lang=>"de"}
Can anyone help out with this?
Thanks.
Jason
As far as I know, you cannot map two routes to the same name like that.
You would need to rename one of them, ie
map.impressum
map.imprint
When Rails looks up the route, it will stop at the first one that it finds, that's why your 'de' links are working.

Resources