#138 I18n (revised)
Mar 29, 2012 | 10 minutes |
Views
Translate a site into other languages through internationalization (I18n). Here I show how to store a locale in the URL and manage translated text through YAML files.
- Download:
- source codeProject Files in Zip (85.2 KB)
- mp4Full Size H.264 Video (27.9 MB)
- m4vSmaller H.264 Video (12.1 MB)
- webmFull Size VP8 Video (14.3 MB)
- ogvFull Size Theora Video (28.3 MB)
Great screencast on basic I18n use in ruby!
Another unmentioned tool for managing your YAML files is Locale, check it out.
(Disclaimer : I'm a Locale developer)
Any idea, why localizing nested attributes is unbelievable painful? Consider this setup, where you need a lot of repetition to create correctly localized error messages.
Am I doing it wrong?
Maybe we can have an episode about globalize3 next?
+1 (x10^99999)
I've just seen the old I18n episode and discovered on of the globalize3 authors (svenfuchs) is even an rails-i18n author. It would be cool an episode about globalize3 (I wonder if some way to build translation interface on it exists...)
Thank you so much for this. I did not know we can use Model attributes in a more simpler than i used to do it all the time.
excellent revised episode!
Using the t() method, it can sometimes be confusing which key in the i18n file Rails is trying to look up.. there doesn't seem to be a consistent naming convention in Rails.
e.g it uses different locations for form labels, buttons, helpers, titles, strings in controllers or models, etc..
The following page provides a table with examples, as well as a tip on how to monkey-patch the i18n Gem in development mode, so Rails will tell you the i18n-keys it tries to look up.
http://www.unixgods.org/~tilo/Rails/where_is_Rails_trying_to_lookup_L10N_strings.html
I hope you'll find it useful.
The screencasts are so perfectly made, I am trying to do the same at my http://happycasts.net (One Linux Tip per Week), to make sth useful and beautiful at the same time.
Railscasts Rocks!!!
Managing i18n and l10n can be very painful, especially in large projects with multiple persons working on translations.
That is why we built a product to make things a lot easier: phrase
If you have a rails/ruby project that uses the i18n gem, this could be the right thing for you to use.
And since we are on Railscasts, we have a screencast, too ;-)
Nice one, for dealing with locales in the URLs Routing Filter Unless API might be useful.
you may want to update
to be something along the lines of
If you want to maintain the request format sent in /posts.json, also it should be noted that a 302 redirect will not resubmit post data, possibly a 307. This method of redirection you need to pay close attention that any urls placed in ajax calls have the locale otherwise there may be some unforeseen bugs, especially with POST requests
Has anyone tried tr8n?
http://github.com/berk/tr8n
http://wiki.tr8n.org/slides
No need for YML files, full support for language context rules and language cases. Uses crowd-sourced or professional translator provided translations.
Here is what some of the translations would look like. t method is replaced with tr.
<%= tr("Hello World!") %>
<%= tr("Name:", "User name label") %>
<%= tr("You have {count|| message} in your mailbox.", nil, {count: 5}) %>
<%= tr("{actor} sent {target::dat} a message.", nil, {actor:user1, target:user2}) %>
<%= tr("{actor} sent {target::dat} [strong: {count|| message}].", nil, {actor:user1, target:user2, count:5}) %>
Michael sent Anna 5 messages.
In Russian this would need 9 translations based on the gender of actor (male, female, unknown) as well as value of count: 3 cases as well. It can translate names as well, if you so desire.
Михаил послал Анне 5 сообщений.
The technology is very powerful and has been used by Geni and Yammer and a few other companies and is now open sourced.
(Disclaimer: I am a tr8n developer)
For some strange reason. It sends me to an redirect loop
http://0.0.0.0:3000/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/assets
Am using twitter bootstrap
Not sure what could be going wrong
Sorry about this issue. I will add a note and update the code to explain this.
Hi, have you found any solution to this issue?
I managed to get rid of the redirect loop mentioned, and make the routing a bit more robust and friendly-forwarding-ish, by doing the following (I used this example when I attempted to internationalize the Rails Tutorial:
config/routes.rb
Since there are two
root_path
s, I renamed the one inside the:locale
scope so there would be no conflicts in the app and tests. I tested the routes using RSpec as follows:spec/routing/routing_spec.rb
I know that redirects are technically requests and their tests should really be under the spec/requests/ directory, but I figured they'd be more appropriate under routing since they're really just tests for exceptions and catch-alls. Anyway, hope this helps or is of interest to someone.
It's still breaking thing with Rails 4.0.x.
When using
%{path}
in the redirect, Rails will escape all the slashes in thepath
, so that the route will no more be recognized.I found this to be a valid fix:
Thanks that worked for me too. (Rails 4.1.x)
I want to set the locale by the clients browserlocale
request.env['HTTP_ACCEPT_LANGUAGE']
and by the url.If the url has no
params[:locale]
it should use the browserlocale set the url.Here is my code:
The Problem is that the routesfile gets executed first so the controller-actions won't grab anymore.
Does anyone has a solution for that?
Thank you, Ryan!
+1
This is awesome, works great on development but when I pushed to production I get this error in the log...
URI::InvalidComponentError (bad component(expected host component):
Thanks in advance for any help out there
I have followed all instructions and even copy/paste-d to make sure I made no mistakes. I'm at 6:16, right after adding default_url_options in the application controller and keep getting the same error when restarting the server.
whyyy?
Nevermind, simple mistake. Just had to change
get "dashboard/index"
toget "dashboard/index" => 'dashboard#index'
. I would like to delete my comment but I can't access a delete button.Hey Leonel, thanks for not deleting your comment - it helped me after a couple of hours scratching my head. Seems the 'get' calls need to explicitly point to the controller#action when doing things this way. Thanks dude!
Hi, does anyone knows how to accomplish that routing process in Rails 2.3.X ?
Thank you.
Hi, anybody knows why when i type long urls it redirected to an escaped url?
http://localhost:3000/users/login becomes http://localhost:3000/id/users%2Flogin
no worries, i was able to fix my problem by changing last two line of code on the route
Thanks for this, man! Really helped.
I should mention it's now preferred to add
via: :all
if usingmatch
to address what Keil mentions below.+1
Hi,
The routes seetings affects the assets path in production.
Ain't working as is.
nice approach rayn ,,I didnot test it in production yet ,,but it seems that its not working with with Endless page approach you showed before .
It works like a charm :) Thank you very much !
Just a question, how can I use I18n in javascript confirmation, for example to delete an item
<%= link_to raw(''), user, confirm: 'Are you sure?', method: :delete %>
Thanks :)
I did it like this
confirm: t("sure")
but in fact my question is more for links like the one below :
You should not use the
match
method in your router without specifying an HTTP method.Match is now depreciated with rails 4. I changed "match" to "get". I figured it makes sense to only support get in these routes. Who would be posting or patching in an unknown language?
Make sure that if you are using devise with a locale field like so:
It important to check if current_user is nil before setting the locale. If you're using devise, placing this code in your application controller without checking will cause not-logged in user's pages to crash.
Hi i use rails 4.
I have a loop in my url when i put bad locales : example : site.com/zz/
It's look like : site.com/en/en/en/en/en/zz/
In my config/routes.rb i have :
Does anyone have the same issue ?