Rails 3 force localization language for specific text - ruby-on-rails

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

Related

How to keep the query params when changing locale in Rails 6

I am trying to preserve query params when changing locale in Rails 6, I only found the default_url_options method & if I try to pass the params to that it doesn't work throwing unpermitted params passed exception
For example:
A URL having http://localhost:3000/some_path?token=ABcasdjlaweQWd
Should be changed to http://localhost:3000/de/some_path?token=ABcasdjlaweQWd
But currently, it only becomes: http://localhost:3000/some_path & I lose the query params
Keeping things secure is important, so I don't want to hack my way disabling the params filter, but it should be possible to pass the params during language change without big hacks.
Update:
I am changing local by navbar link_to btn (for different locale), for example: link_to url_for( :locale => 'fr' ) that will change locale part of the current url to the french locale for example
What is the correct way to maintain the query params on language change?
Without overriding the default_url_options, a better way is to make the language change button behave differently, so that it won't remove the query params
So instead of this:
= link_to url_for( locale: 'en' )
It should be changed to this, which will keep the params:
= link_to request.params.merge( locale: 'en' )

Can I Set Up Routes That Include The Locale & Default the Locale In The Route When It Is Not Provided?

I currently have routing that requires the locale (i.e. /en, /fr, etc.). At some point I have used all of the statements below in routes.rb:
scope "/:locale", locale: /#{I18n.available_locales.join("|")}/ do
scope "/:locale", defaults: { :locale => "en" } do
scope "/:locale" do
I know that if I do the following if the route does not include the locale that it will point to the English version of the website. However it does not set the locale like I want once the page is displayed. If I go to the French version by clicking my locale logic the first link will display the French version of the page with /fr in the link. However if I click another link on the French page the locale goes back to English with the locale excluded from the link.
scope "(/:locale)", defaults: { :locale => "en" } do
Here is the code for my locale links in my application where a user can click on a flag image or text to change the locale:
<%= link_to_unless_current image_tag("english.jpg", alt: "#{t :english}"), locale: "en" %> <%= link_to_unless_current "#{t :english}", locale: "en" %>
<%= link_to_unless_current image_tag("french.jpg", alt: "#{t :french}"), locale: "fr" %> <%= link_to_unless_current "#{t :french}", locale: "fr" %>
What I would like to do is to prevent a 500 system error if by chance someone has an link saved before the website was localized. For example if they have http://mywebsite.com/video it would display the English version of the website and set the locale to "en".
Here is the code I have in application_controller.rb.
before_filter :set_locale
def default_url_options(options={})
{ :locale => I18n.locale }
end
private
def set_locale
I18n.locale = (params[:locale] if params[:locale].present?) || cookies[:locale] || 'en'
cookies[:locale] = I18n.locale if cookies[:locale] != I18n.locale.to_s
end
I'm not finding anything on this particular issue other than to use the routing-filter gem. I was using the gem but until there is a production version of the gem for Rails 4 I have no option but to figure this routing issue out.
Any help would be appreciated.
i find your question rather confusing... so my answer will refer to some parts of you code. maybe that gives you enough context to fix your problems.
do not use 3 routes for locale
one route is enough, please read the guides for internationlization.
i think that you will have to go with the optional approach as you want to support legacy urls:
scope "(/:locale)" {}
use the config.default_locale option
in the configuration you can configure fallbacks for localization.
don't mix default_url_options and cookies
if you are using cookies to keep track of your locale, you can can skip the default_url_options, you will have to keep the unlocalized versions anyways for backward compatibility.
if you MUST have urls like domain.com/en/something do it the other way around. avoid using cookies, use the URL everywhere and redirect people coming in from a legacy url.

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 problem using I18n in views but not in console

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'

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