diff options
author | Xavier Noria <fxn@hashref.com> | 2010-04-14 01:00:03 -0700 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-04-14 01:00:03 -0700 |
commit | 9a4fdff0af0ba05ad77096ce835aeb4bf343a53f (patch) | |
tree | d4657c9e64fdc050695dd58cf419241ace32e7a6 /railties/guides/source/i18n.textile | |
parent | 2ff5f38abb4a44ed5356c34b40d30d446fb63408 (diff) | |
parent | e090898c5e98e853828208343b3911a229563b62 (diff) | |
download | rails-9a4fdff0af0ba05ad77096ce835aeb4bf343a53f.tar.gz rails-9a4fdff0af0ba05ad77096ce835aeb4bf343a53f.tar.bz2 rails-9a4fdff0af0ba05ad77096ce835aeb4bf343a53f.zip |
Merge commit 'docrails/master'
Diffstat (limited to 'railties/guides/source/i18n.textile')
-rw-r--r-- | railties/guides/source/i18n.textile | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/railties/guides/source/i18n.textile b/railties/guides/source/i18n.textile index ec69f770ee..01e3380ee4 100644 --- a/railties/guides/source/i18n.textile +++ b/railties/guides/source/i18n.textile @@ -97,19 +97,17 @@ The *translations load path* (+I18n.load_path+) is just a Ruby Array of paths to NOTE: The backend will lazy-load these translations when a translation is looked up for the first time. This makes it possible to just swap the backend with something else even after translations have already been announced. -The default +environment.rb+ files has instructions on how to add locales from another directory and how to set a different default locale. Just uncomment and edit the specific lines. +The default +application.rb+ files has instructions on how to add locales from another directory and how to set a different default locale. Just uncomment and edit the specific lines. <ruby> -# The internationalization framework can be changed -# to have another default locale (standard is :en) or more load paths. -# All files from config/locales/*.rb,yml are added automatically. -# config.i18n.load_path << Dir[File.join(RAILS_ROOT, 'my', 'locales', '*.{rb,yml}')] +# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. +# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] # config.i18n.default_locale = :de </ruby> h4. Optional: Custom I18n Configuration Setup -For the sake of completeness, let's mention that if you do not want to use the +environment.rb+ file for some reason, you can always wire up things manually, too. +For the sake of completeness, let's mention that if you do not want to use the +application.rb+ file for some reason, you can always wire up things manually, too. To tell the I18n library where it can find your custom translation files you can specify the load path anywhere in your application - just make sure it gets run before any translations are actually looked up. You might also want to change the default locale. The simplest thing possible is to put the following into an initializer: @@ -117,8 +115,7 @@ To tell the I18n library where it can find your custom translation files you can # in config/initializers/locale.rb # tell the I18n library where to find your translations -I18n.load_path << Dir[ File.join(RAILS_ROOT, 'lib', 'locale', - '*.{rb,yml}') ] +I18n.load_path += Dir[Rails.root.join('lib', 'locale', '*.{rb,yml}')] # set default locale to something other than :en I18n.default_locale = :pt @@ -126,7 +123,7 @@ I18n.default_locale = :pt h4. Setting and Passing the Locale -If you want to translate your Rails application to a *single language other than English* (the default locale), you can set I18n.default_locale to your locale in +environment.rb+ or an initializer as shown above, and it will persist through the requests. +If you want to translate your Rails application to a *single language other than English* (the default locale), you can set I18n.default_locale to your locale in +application.rb+ or an initializer as shown above, and it will persist through the requests. However, you would probably like to *provide support for more locales* in your application. In such case, you need to set and pass the locale between requests. @@ -227,21 +224,23 @@ You probably want URLs to look like this: +www.example.com/en/books+ (which load <ruby> # config/routes.rb -map.resources :books, :path_prefix => '/:locale' +scope "/:locale" do + resources :books +end </ruby> Now, when you call the +books_path+ method you should get +"/en/books"+ (for the default locale). An URL like +http://localhost:3001/nl/books+ should load the Netherlands locale, then, and following calls to +books_path+ should return +"/nl/books"+ (because the locale changed). -Of course, you need to take special care of the root URL (usually "homepage" or "dashboard") of your application. An URL like +http://localhost:3001/nl+ will not work automatically, because the +map.root :controller => "dashboard"+ declaration in your +routes.rb+ doesn't take locale into account. (And rightly so: there's only one "root" URL.) +Of course, you need to take special care of the root URL (usually "homepage" or "dashboard") of your application. An URL like +http://localhost:3001/nl+ will not work automatically, because the +root :to => "books#index"+ declaration in your +routes.rb+ doesn't take locale into account. (And rightly so: there's only one "root" URL.) You would probably need to map URLs like these: <ruby> # config/routes.rb -map.dashboard '/:locale', :controller => "dashboard" +match '/:locale' => 'dashboard#index' </ruby> -Do take special care about the *order of your routes*, so this route declaration does not "eat" other ones. (You may want to add it directly before the +map.root+ declaration.) +Do take special care about the *order of your routes*, so this route declaration does not "eat" other ones. (You may want to add it directly before the +root :to+ declaration.) IMPORTANT: This solution has currently one rather big *downside*. Due to the _default_url_options_ implementation, you have to pass the +:id+ option explicitly, like this: +link_to 'Show', book_url(:id => book)+ and not depend on Rails' magic in code like +link_to 'Show', book+. If this should be a problem, have a look at two plugins which simplify work with routes in this way: Sven Fuchs's "routing_filter":http://github.com/svenfuchs/routing-filter/tree/master and Raul Murciano's "translate_routes":http://github.com/raul/translate_routes/tree/master. See also the page "How to encode the current locale in the URL":http://rails-i18n.org/wiki/wikipages/how-to-encode-the-current-locale-in-the-url in the Rails i18n Wiki. @@ -288,8 +287,8 @@ You most probably have something like this in one of your applications: <ruby> # config/routes.rb -ActionController::Routing::Routes.draw do |map| - map.root :controller => 'home', :action => 'index' +Yourapp::Application.routes.draw do |map| + root :to => "home#index" end # app/controllers/home_controller.rb @@ -422,8 +421,9 @@ This way, you can separate model and model attribute names from text inside view NOTE: The default locale loading mechanism in Rails does not load locale files in nested dictionaries, like we have here. So, for this to work, we must explicitly tell Rails to look further: <ruby> - # config/environment.rb - config.i18n.load_path += Dir[File.join(RAILS_ROOT, 'config', 'locales', '**', '*.{rb,yml}')] + # config/application.rb + config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] + </ruby> Do check the "Rails i18n Wiki":http://rails-i18n.org/wiki for list of tools available for managing translations. |