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}
Related
I'm using jquery autocomplete on several places of my app, including in a form with url :
http://www.fonsiuris.net/fr/administration/actes/nouveau/122
app/views/administration/actes form has this field:
<%= lieux.text_area :place, :value => params[:acte] ? #place["place"] : "", class:"field_places", data: {autocomplete_source: lieux_enum_path} %>
in routes.rb:
get 'lieux/enum' => 'lieux#enum'
This is the very first url of my routes.rb
However, when I type letters in the text area, such as 'Dou' that has the autocomplete function, it points to this url :
http://www.fonsiuris.net/fr/biblios/trouver?utf8=✓&ch_bib[aut_titre_cherche]=Douai&commit.x=6&commit.y=2&term=Dou
and when I type in more than one term, that term gets added to the url like this :
/fr/biblios/trouver?utf8=%E2%9C%93&ch_bib%5Baut_titre_cherche%5D=Douai&commit.x=5&commit.y=11&term=Paris
(both Douai and Paris are added as search terms)
It should point to this url:
http://www.fonsiuris.net/lieux/enum?term=Dou
Which gives the right result. I'm using the same code, with the same javascript in several places of my application and it's working fine on all other places !
The output of rails routes | grep enum is :
lieux_enum GET /lieux/enum(.:format) lieux#enum
motclefs_enum GET /motclefs/enum(.:format) motclefs#enum
actes_enum_acte_mot GET /actes/enum_acte_mot(.:format) actes#enum_acte_mot
biblios_enum_rec GET /biblios/enum_rec(.:format) biblios#enum_rec
administration_archives_enum GET /administration/archives/enum(.:format) administration/archives#enum_archive
administration_biblios_enum GET (/:locale)/administration/biblios/enum(.:format) administration/biblios#enum_titres {:locale=>/fr|en|nl|it/}
administration_auteurs_enum GET (/:locale)/administration/auteurs/enum(.:format) administration/auteurs#enum_noms {:locale=>/fr|en|nl|it/}
What I have tried so far :
Reoganising routes.rb
rewriting the form.html.erb so it contains exactly the same code as the other places of my app where this does work
use the same javascript everywhere
reinstall jquery and jquery-ui : now using cdn instead of the gem - doesn't make any
difference.
I don't know where to start looking to resolve this.
I'm going to provide a slightly more articulate answer based on what I garnered from #thiebo.
<%= lieux.text_area :place, :value => params[:acte] ? #place["place"] : "",
class:"field_places", data: {autocomplete_source: lieux_enum_path} %>
This field appears twice with the same field name. Delete the field without the autocomplete member on the data attribute.
For the sake of posterity, the problem had nothing to do with rails, routes or jquery. I had put elsewhere in the form another field with the same class name as the field on which I put the autocomplete.
So I have a link_to method that requires the body argument to be populated by translated material.
For example:
link_to('translated material', '/dashboard')
I currently have it set up so in my en.yml (for example) has:
en:
dashboard: 'Dashboard'
and in a typical situation where I need translation I would just do, for example, this: <%= t :dashboard %>
This works great. However, how do I place that into the link_to() method like so:
link_to(*insert :dashboard translated text here*, '/dashboard')
I'm sure it's simple formatting, still learning all the nuances of ROR!
Just put everything together:
link_to(t('dashboard'), dashboard_path)
btw. it is preferred to use the named routes. Do not use strings as urls.
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.
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'
I'm using the default I18n module for Rails to translate my strings in views.
<%= t("registration.heading") %>
Now, when I'm in the registration-view, all my strings start with registration. I always have to write
<%= t("registration.heading.notice") %>
// or
<%= t(:heading, :scope => :registration) %>
It would be nice to define a default scope for that file (maybe even in the controller), so a call to t automatically adds the defined scope
// controller
set_i18n_default_scope :registration
// view
<%= t(:heading) %>
// --> looks in "registration.heading"
Is this possible?
If you organize your translations adding a view name, as in:
en:
registration:
index:
heading: "Registration heading"
then you may use this:
<%= t(".heading") %>
Notice that the first character is a dot.
You may read about it in Rails Internationalization (I18n) API Guide
If you have texts which are shared amongst numerous views, and you don't want to copy the same translation in each section for each view, you may use YAML references. They are nicely described on wikipedia: http://en.wikipedia.org/wiki/YAML#Repeated_nodes
It is possible. Check section 4.1.4 of the Rails i18n API
4.1.4 “Lazy” Lookup
Rails 2.3 implements a convenient way
to look up the locale inside views.
When you have the following
dictionary:
es: books:
index:
title: "Título"
you can look up the books.index.title value inside
app/views/books/index.html.erb
template like this (note the dot):
<%= t '.title' %>
Regarding Lazy Lookups:
Here's the general solution for this kind of problem
Common Problem: Where is Rails trying to look-up L10N / I18N Strings? - e.g. when doing Lazy Lookups
It's not easy to guess, because it's different for Views, Controllers, Models, Labels, Helpers, or Validations... etc... but...
It's easy to find out directly, using this:
http://www.unixgods.org/Rails/where_is_Rails_trying_to_lookup_L10N_strings.html
this helps figuring out what Rails thinks the current scope is (e.g. when using ".heading")
3 Simple Steps:
create a file ./config/initializers/i18n.rb , as described in the article above
put t('.heading') in your view
start "rails server" and look in the console output where Rails thinks the default location is for '.heading' for this view... e.g. what's the I18N-key
(4. then add the I18N string into the location identified by the key)
Works like a charm :-)
If you want to print out keys that I18n gem's lazy mode is looking for, you can add that in a i18n.rb file in your initializers folder:
module I18n
module Backend
class Simple
module Implementation
alias_method :lookup_orig, :lookup
# Give ability to check I18n looked up keys in lazy mode by setting env var I18N_DEBUG to true
#
def lookup(locale, key, scope = [], options = {})
puts "I18N keys: #{I18n.normalize_keys(locale, key, scope, options[:separator])}" if ENV['I18N_DEBUG']
lookup_orig(locale, key, scope, options)
end
end
end
end
end
(Gist: https://gist.github.com/capripot/6e6cf778ad2db0443280)
And then start your server like for instance:
I18N_DEBUG=true bundle exec rails server