railties 3.0.11 → 3.0.12.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. data/lib/rails/version.rb +2 -2
  2. metadata +305 -461
  3. data/guides/output/2_2_release_notes.html +0 -565
  4. data/guides/output/2_3_release_notes.html +0 -713
  5. data/guides/output/3_0_release_notes.html +0 -652
  6. data/guides/output/3_1_release_notes.html +0 -670
  7. data/guides/output/action_controller_overview.html +0 -925
  8. data/guides/output/action_mailer_basics.html +0 -658
  9. data/guides/output/action_view_overview.html +0 -1471
  10. data/guides/output/active_model_basics.html +0 -349
  11. data/guides/output/active_record_basics.html +0 -364
  12. data/guides/output/active_record_querying.html +0 -1272
  13. data/guides/output/active_record_validations_callbacks.html +0 -1292
  14. data/guides/output/active_resource_basics.html +0 -252
  15. data/guides/output/active_support_core_extensions.html +0 -3374
  16. data/guides/output/ajax_on_rails.html +0 -412
  17. data/guides/output/api_documentation_guidelines.html +0 -317
  18. data/guides/output/asset_pipeline.html +0 -691
  19. data/guides/output/association_basics.html +0 -1742
  20. data/guides/output/caching_with_rails.html +0 -533
  21. data/guides/output/command_line.html +0 -662
  22. data/guides/output/configuring.html +0 -811
  23. data/guides/output/contribute.html +0 -216
  24. data/guides/output/contributing_to_ruby_on_rails.html +0 -465
  25. data/guides/output/credits.html +0 -210
  26. data/guides/output/debugging_rails_applications.html +0 -791
  27. data/guides/output/engines.html +0 -673
  28. data/guides/output/form_helpers.html +0 -850
  29. data/guides/output/generators.html +0 -725
  30. data/guides/output/getting_started.html +0 -1980
  31. data/guides/output/i18n.html +0 -1054
  32. data/guides/output/images/belongs_to.png +0 -0
  33. data/guides/output/images/book_icon.gif +0 -0
  34. data/guides/output/images/bullet.gif +0 -0
  35. data/guides/output/images/challenge.png +0 -0
  36. data/guides/output/images/chapters_icon.gif +0 -0
  37. data/guides/output/images/check_bullet.gif +0 -0
  38. data/guides/output/images/credits_pic_blank.gif +0 -0
  39. data/guides/output/images/csrf.png +0 -0
  40. data/guides/output/images/customized_error_messages.png +0 -0
  41. data/guides/output/images/edge_badge.png +0 -0
  42. data/guides/output/images/error_messages.png +0 -0
  43. data/guides/output/images/feature_tile.gif +0 -0
  44. data/guides/output/images/footer_tile.gif +0 -0
  45. data/guides/output/images/fxn.png +0 -0
  46. data/guides/output/images/grey_bullet.gif +0 -0
  47. data/guides/output/images/habtm.png +0 -0
  48. data/guides/output/images/has_many.png +0 -0
  49. data/guides/output/images/has_many_through.png +0 -0
  50. data/guides/output/images/has_one.png +0 -0
  51. data/guides/output/images/has_one_through.png +0 -0
  52. data/guides/output/images/header_backdrop.png +0 -0
  53. data/guides/output/images/header_tile.gif +0 -0
  54. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  55. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  56. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  57. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  58. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  59. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  60. data/guides/output/images/icons/README +0 -5
  61. data/guides/output/images/icons/callouts/1.png +0 -0
  62. data/guides/output/images/icons/callouts/10.png +0 -0
  63. data/guides/output/images/icons/callouts/11.png +0 -0
  64. data/guides/output/images/icons/callouts/12.png +0 -0
  65. data/guides/output/images/icons/callouts/13.png +0 -0
  66. data/guides/output/images/icons/callouts/14.png +0 -0
  67. data/guides/output/images/icons/callouts/15.png +0 -0
  68. data/guides/output/images/icons/callouts/2.png +0 -0
  69. data/guides/output/images/icons/callouts/3.png +0 -0
  70. data/guides/output/images/icons/callouts/4.png +0 -0
  71. data/guides/output/images/icons/callouts/5.png +0 -0
  72. data/guides/output/images/icons/callouts/6.png +0 -0
  73. data/guides/output/images/icons/callouts/7.png +0 -0
  74. data/guides/output/images/icons/callouts/8.png +0 -0
  75. data/guides/output/images/icons/callouts/9.png +0 -0
  76. data/guides/output/images/icons/caution.png +0 -0
  77. data/guides/output/images/icons/example.png +0 -0
  78. data/guides/output/images/icons/home.png +0 -0
  79. data/guides/output/images/icons/important.png +0 -0
  80. data/guides/output/images/icons/next.png +0 -0
  81. data/guides/output/images/icons/note.png +0 -0
  82. data/guides/output/images/icons/prev.png +0 -0
  83. data/guides/output/images/icons/tip.png +0 -0
  84. data/guides/output/images/icons/up.png +0 -0
  85. data/guides/output/images/icons/warning.png +0 -0
  86. data/guides/output/images/jaimeiniesta.jpg +0 -0
  87. data/guides/output/images/nav_arrow.gif +0 -0
  88. data/guides/output/images/polymorphic.png +0 -0
  89. data/guides/output/images/posts_index.png +0 -0
  90. data/guides/output/images/radar.png +0 -0
  91. data/guides/output/images/rails_guides_logo.gif +0 -0
  92. data/guides/output/images/rails_logo_remix.gif +0 -0
  93. data/guides/output/images/rails_welcome.png +0 -0
  94. data/guides/output/images/session_fixation.png +0 -0
  95. data/guides/output/images/tab_grey.gif +0 -0
  96. data/guides/output/images/tab_info.gif +0 -0
  97. data/guides/output/images/tab_note.gif +0 -0
  98. data/guides/output/images/tab_red.gif +0 -0
  99. data/guides/output/images/tab_yellow.gif +0 -0
  100. data/guides/output/images/tab_yellow.png +0 -0
  101. data/guides/output/images/validation_error_messages.png +0 -0
  102. data/guides/output/images/vijaydev.jpg +0 -0
  103. data/guides/output/index.html +0 -300
  104. data/guides/output/initialization.html +0 -1087
  105. data/guides/output/javascripts/guides.js +0 -7
  106. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  107. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  108. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  109. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  110. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  111. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  112. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  113. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  114. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  115. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  116. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  117. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  118. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  119. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  120. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  121. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  122. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  123. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  124. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  125. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  126. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  127. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  128. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  129. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  130. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  131. data/guides/output/javascripts/syntaxhighlighter/shCore.js +0 -17
  132. data/guides/output/layout.html +0 -312
  133. data/guides/output/layouts_and_rendering.html +0 -1257
  134. data/guides/output/migrations.html +0 -751
  135. data/guides/output/nested_model_forms.html +0 -350
  136. data/guides/output/performance_testing.html +0 -858
  137. data/guides/output/plugins.html +0 -590
  138. data/guides/output/rails_application_templates.html +0 -368
  139. data/guides/output/rails_on_rack.html +0 -408
  140. data/guides/output/routing.html +0 -1246
  141. data/guides/output/ruby_on_rails_guides_guidelines.html +0 -218
  142. data/guides/output/security.html +0 -968
  143. data/guides/output/stylesheets/fixes.css +0 -16
  144. data/guides/output/stylesheets/main.css +0 -445
  145. data/guides/output/stylesheets/print.css +0 -52
  146. data/guides/output/stylesheets/reset.css +0 -43
  147. data/guides/output/stylesheets/style.css +0 -13
  148. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +0 -226
  149. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  150. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  151. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  152. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  153. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  154. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  155. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  156. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  157. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  158. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  159. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  160. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  161. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  162. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  163. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  164. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  165. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  166. data/guides/output/testing.html +0 -1182
@@ -1,1054 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
-
4
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
- <head>
6
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
-
8
- <title>Ruby on Rails Guides: Rails Internationalization (I18n) API</title>
9
-
10
- <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
11
- <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
12
-
13
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
14
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
15
-
16
- <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
17
- </head>
18
- <body class="guide">
19
- <div id="topNav">
20
- <div class="wrapper">
21
- <strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
22
- <a href="http://rubyonrails.org/">Overview</a> |
23
- <a href="http://rubyonrails.org/download">Download</a> |
24
- <a href="http://rubyonrails.org/deploy">Deploy</a> |
25
- <a href="https://github.com/rails/rails">Code</a> |
26
- <a href="http://rubyonrails.org/screencasts">Screencasts</a> |
27
- <a href="http://rubyonrails.org/documentation">Documentation</a> |
28
- <a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
29
- <a href="http://rubyonrails.org/community">Community</a> |
30
- <a href="http://weblog.rubyonrails.org/">Blog</a>
31
- </div>
32
- </div>
33
- <div id="header">
34
- <div class="wrapper clearfix">
35
- <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
36
- <p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
37
- <ul class="nav">
38
- <li><a href="index.html">Home</a></li>
39
- <li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
40
- <div id="guides" class="clearfix" style="display: none;">
41
- <hr />
42
- <dl class="L">
43
- <dt>Start Here</dt>
44
- <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
45
- <dt>Models</dt>
46
- <dd><a href="migrations.html">Rails Database Migrations</a></dd>
47
- <dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
48
- <dd><a href="association_basics.html">Active Record Associations</a></dd>
49
- <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
50
- <dt>Views</dt>
51
- <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
52
- <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
53
- <dt>Controllers</dt>
54
- <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
55
- <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
56
- </dl>
57
- <dl class="R">
58
- <dt>Digging Deeper</dt>
59
- <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
60
- <dd><a href="i18n.html">Rails Internationalization API</a></dd>
61
- <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
62
- <dd><a href="testing.html">Testing Rails Applications</a></dd>
63
- <dd><a href="security.html">Securing Rails Applications</a></dd>
64
- <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
65
- <dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
66
- <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
67
- <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
68
- <dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
69
- <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
70
-
71
- <dt>Extending Rails</dt>
72
- <dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
73
- <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
74
- <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
75
-
76
- <dt>Contributing to Ruby on Rails</dt>
77
- <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
78
- <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
79
- <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
80
-
81
- <dt>Release Notes</dt>
82
- <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
83
- <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
84
- <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
85
- <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
86
- </dl>
87
- </div>
88
- </li>
89
- <li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
90
- <li><a href="credits.html">Credits</a></li>
91
- </ul>
92
- </div>
93
- </div>
94
- <hr class="hide" />
95
-
96
- <div id="feature">
97
- <div class="wrapper">
98
- <h2>Rails Internationalization (I18n) <span class="caps">API</span></h2>
99
- <p>The Ruby I18n (shorthand for <em>internationalization</em>) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for <strong>translating your application to a single custom language</strong> other than English or for <strong>providing multi-language support</strong> in your application.</p>
100
- <p>The process of &#8220;internationalization&#8221; usually means to abstract all strings and other locale specific bits (such as date or currency formats) out of your application. The process of &#8220;localization&#8221; means to provide translations and localized formats for these bits. <sup class="footnote" id="fnr1"><a href="#fn1">1</a></sup></p>
101
- <p>So, in the process of <em>internationalizing</em> your Rails application you have to:</p>
102
- <ul>
103
- <li>Ensure you have support for i18n</li>
104
- <li>Tell Rails where to find locale dictionaries</li>
105
- <li>Tell Rails how to set, preserve and switch locales</li>
106
- </ul>
107
- <p>In the process of <em>localizing</em> your application you&#8217;ll probably want to do the following three things:</p>
108
- <ul>
109
- <li>Replace or supplement Rails&#8217; default locale &#8212; e.g. date and time formats, month names, Active Record model names, etc.</li>
110
- <li>Abstract strings in your application into keyed dictionaries &#8212; e.g. flash messages, static text in your views, etc.</li>
111
- <li>Store the resulting dictionaries somewhere</li>
112
- </ul>
113
- <p>This guide will walk you through the I18n <span class="caps">API</span> and contains a tutorial on how to internationalize a Rails application from the start.</p>
114
-
115
- <div id="subCol">
116
- <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
117
- <ol class="chapters">
118
- <li><a href="#how-i18n-in-ruby-on-rails-works">How I18n in Ruby on Rails Works</a><ul><li><a href="#the-overall-architecture-of-the-library">The Overall Architecture of the Library</a></li> <li><a href="#the-public-i18n-api">The Public I18n <span class="caps">API</span></a></li></ul></li><li><a href="#setup-the-rails-application-for-internationalization">Setup the Rails Application for Internationalization</a><ul><li><a href="#configure-the-i18n-module">Configure the I18n Module</a></li> <li><a href="#optional-custom-i18n-configuration-setup">Optional: Custom I18n Configuration Setup</a></li> <li><a href="#setting-and-passing-the-locale">Setting and Passing the Locale</a></li> <li><a href="#setting-the-locale-from-the-domain-name">Setting the Locale from the Domain Name</a></li> <li><a href="#setting-the-locale-from-the-url-params">Setting the Locale from the <span class="caps">URL</span> Params</a></li> <li><a href="#setting-the-locale-from-the-client-supplied-information">Setting the Locale from the Client Supplied Information</a></li></ul></li><li><a href="#internationalizing-your-application">Internationalizing your Application</a><ul><li><a href="#adding-translations">Adding Translations</a></li> <li><a href="#passing-variables-to-translations">Passing variables to translations</a></li> <li><a href="#adding-date-time-formats">Adding Date/Time Formats</a></li> <li><a href="#localized-views">Localized Views</a></li> <li><a href="#organization-of-locale-files">Organization of Locale Files</a></li></ul></li><li><a href="#overview-of-the-i18n-api-features">Overview of the I18n <span class="caps">API</span> Features</a><ul><li><a href="#looking-up-translations">Looking up Translations</a></li> <li><a href="#interpolation">Interpolation</a></li> <li><a href="#pluralization">Pluralization</a></li> <li><a href="#setting-and-passing-a-locale">Setting and Passing a Locale</a></li> <li><a href="#using-safe-html-translations">Using Safe <span class="caps">HTML</span> Translations</a></li></ul></li><li><a href="#how-to-store-your-custom-translations">How to Store your Custom Translations</a><ul><li><a href="#translations-for-active-record-models">Translations for Active Record Models</a></li> <li><a href="#overview-of-other-built-in-methods-that-provide-i18n-support">Overview of Other Built-In Methods that Provide I18n Support</a></li></ul></li><li><a href="#customize-your-i18n-setup">Customize your I18n Setup</a><ul><li><a href="#using-different-backends">Using Different Backends</a></li> <li><a href="#using-different-exception-handlers">Using Different Exception Handlers</a></li></ul></li><li><a href="#conclusion">Conclusion</a></li><li><a href="#contributing-to-rails-i18n">Contributing to Rails I18n</a></li><li><a href="#resources">Resources</a></li><li><a href="#authors">Authors</a></li><li><a href="#footnotes">Footnotes</a></li></ol></div>
119
- </div>
120
- </div>
121
-
122
- <div id="container">
123
- <div class="wrapper">
124
- <div id="mainCol">
125
- <div class="note"><p>The Ruby I18n framework provides you with all necessary means for internationalization/localization of your Rails application. You may, however, use any of various plugins and extensions available, which add additional functionality or features. See the Rails <a href="http://rails-i18n.org/wiki">I18n Wiki</a> for more information.</p></div>
126
- <h3 id="how-i18n-in-ruby-on-rails-works">1 How I18n in Ruby on Rails Works</h3>
127
- <p>Internationalization is a complex problem. Natural languages differ in so many ways (e.g. in pluralization rules) that it is hard to provide tools for solving all problems at once. For that reason the Rails I18n <span class="caps">API</span> focuses on:</p>
128
- <ul>
129
- <li>providing support for English and similar languages out of the box</li>
130
- <li>making it easy to customize and extend everything for other languages</li>
131
- </ul>
132
- <p>As part of this solution, <strong>every static string in the Rails framework</strong> &#8212; e.g. Active Record validation messages, time and date formats &#8212; <strong>has been internationalized</strong>, so <em>localization</em> of a Rails application means &#8220;over-riding&#8221; these defaults.</p>
133
- <h4 id="the-overall-architecture-of-the-library">1.1 The Overall Architecture of the Library</h4>
134
- <p>Thus, the Ruby I18n gem is split into two parts:</p>
135
- <ul>
136
- <li>The public <span class="caps">API</span> of the i18n framework &#8212; a Ruby module with public methods that define how the library works</li>
137
- <li>A default backend (which is intentionally named <em>Simple</em> backend) that implements these methods</li>
138
- </ul>
139
- <p>As a user you should always only access the public methods on the I18n module, but it is useful to know about the capabilities of the backend.</p>
140
- <div class="note"><p>It is possible (or even desirable) to swap the shipped Simple backend with a more powerful one, which would store translation data in a relational database, GetText dictionary, or similar. See section <a href="#using-different-backends">Using different backends</a> below.</p></div>
141
- <h4 id="the-public-i18n-api">1.2 The Public I18n <span class="caps">API</span></h4>
142
- <p>The most important methods of the I18n <span class="caps">API</span> are:</p>
143
- <div class="code_container">
144
- <pre class="brush: ruby; gutter: false; toolbar: false">
145
- translate # Lookup text translations
146
- localize # Localize Date and Time objects to local formats
147
- </pre>
148
- </div>
149
- <p>These have the aliases #t and #l so you can use them like this:</p>
150
- <div class="code_container">
151
- <pre class="brush: ruby; gutter: false; toolbar: false">
152
- I18n.t 'store.title'
153
- I18n.l Time.now
154
- </pre>
155
- </div>
156
- <p>There are also attribute readers and writers for the following attributes:</p>
157
- <div class="code_container">
158
- <pre class="brush: ruby; gutter: false; toolbar: false">
159
- load_path # Announce your custom translation files
160
- locale # Get and set the current locale
161
- default_locale # Get and set the default locale
162
- exception_handler # Use a different exception_handler
163
- backend # Use a different backend
164
- </pre>
165
- </div>
166
- <p>So, let&#8217;s internationalize a simple Rails application from the ground up in the next chapters!</p>
167
- <h3 id="setup-the-rails-application-for-internationalization">2 Setup the Rails Application for Internationalization</h3>
168
- <p>There are just a few simple steps to get up and running with I18n support for your application.</p>
169
- <h4 id="configure-the-i18n-module">2.1 Configure the I18n Module</h4>
170
- <p>Following the <em>convention over configuration</em> philosophy, Rails will set up your application with reasonable defaults. If you need different settings, you can overwrite them easily.</p>
171
- <p>Rails adds all <tt>.rb</tt> and <tt>.yml</tt> files from the <tt>config/locales</tt> directory to your <strong>translations load path</strong>, automatically.</p>
172
- <p>The default <tt>en.yml</tt> locale in this directory contains a sample pair of translation strings:</p>
173
- <div class="code_container">
174
- <pre class="brush: ruby; gutter: false; toolbar: false">
175
- en:
176
- hello: &quot;Hello world&quot;
177
- </pre>
178
- </div>
179
- <p>This means, that in the <tt>:en</tt> locale, the key <em>hello</em> will map to the <em>Hello world</em> string. Every string inside Rails is internationalized in this way, see for instance Active Record validation messages in the <a href="https://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml"><tt>activerecord/lib/active_record/locale/en.yml</tt></a> file or time and date formats in the <a href="https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml"><tt>activesupport/lib/active_support/locale/en.yml</tt></a> file. You can use <span class="caps">YAML</span> or standard Ruby Hashes to store translations in the default (Simple) backend.</p>
180
- <p>The I18n library will use <strong>English</strong> as a <strong>default locale</strong>, i.e. if you don&#8217;t set a different locale, <tt>:en</tt> will be used for looking up translations.</p>
181
- <div class="note"><p>The i18n library takes a <strong>pragmatic approach</strong> to locale keys (after <a href="http://groups.google.com/group/rails-i18n/browse_thread/thread/14dede2c7dbe9470/80eec34395f64f3c?hl=en">some discussion</a>), including only the <em>locale</em> (&#8220;language&#8221;) part, like <tt>:en</tt>, <tt>:pl</tt>, not the <em>region</em> part, like <tt>:en-US</tt> or <tt>:en-GB</tt>, which are traditionally used for separating &#8220;languages&#8221; and &#8220;regional setting&#8221; or &#8220;dialects&#8221;. Many international applications use only the &#8220;language&#8221; element of a locale such as <tt>:cs</tt>, <tt>:th</tt> or <tt>:es</tt> (for Czech, Thai and Spanish). However, there are also regional differences within different language groups that may be important. For instance, in the <tt>:en-US</tt> locale you would have $ as a currency symbol, while in <tt>:en-GB</tt>, you would have £. Nothing stops you from separating regional and other settings in this way: you just have to provide full &#8220;English &#8211; United Kingdom&#8221; locale in a <tt>:en-GB</tt> dictionary. Various <a href="http://rails-i18n.org/wiki">Rails I18n plugins</a> such as <a href="https://github.com/joshmh/globalize2/tree/master">Globalize2</a> may help you implement it.</p></div>
182
- <p>The <strong>translations load path</strong> (<tt>I18n.load_path</tt>) is just a Ruby Array of paths to your translation files that will be loaded automatically and available in your application. You can pick whatever directory and translation file naming scheme makes sense for you.</p>
183
- <div class="note"><p>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.</p></div>
184
- <p>The default <tt>application.rb</tt> 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.</p>
185
- <div class="code_container">
186
- <pre class="brush: ruby; gutter: false; toolbar: false">
187
- # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
188
- # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
189
- # config.i18n.default_locale = :de
190
- </pre>
191
- </div>
192
- <h4 id="optional-custom-i18n-configuration-setup">2.2 Optional: Custom I18n Configuration Setup</h4>
193
- <p>For the sake of completeness, let&#8217;s mention that if you do not want to use the <tt>application.rb</tt> file for some reason, you can always wire up things manually, too.</p>
194
- <p>To tell the I18n library where it can find your custom translation files you can specify the load path anywhere in your application &#8211; 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:</p>
195
- <div class="code_container">
196
- <pre class="brush: ruby; gutter: false; toolbar: false">
197
- # in config/initializers/locale.rb
198
-
199
- # tell the I18n library where to find your translations
200
- I18n.load_path += Dir[Rails.root.join('lib', 'locale', '*.{rb,yml}')]
201
-
202
- # set default locale to something other than :en
203
- I18n.default_locale = :pt
204
- </pre>
205
- </div>
206
- <h4 id="setting-and-passing-the-locale">2.3 Setting and Passing the Locale</h4>
207
- <p>If you want to translate your Rails application to a <strong>single language other than English</strong> (the default locale), you can set I18n.default_locale to your locale in <tt>application.rb</tt> or an initializer as shown above, and it will persist through the requests.</p>
208
- <p>However, you would probably like to <strong>provide support for more locales</strong> in your application. In such case, you need to set and pass the locale between requests.</p>
209
- <div class="warning"><p>You may be tempted to store the chosen locale in a <em>session</em> or a <em>cookie</em>. <strong>Do not do so</strong>. The locale should be transparent and a part of the <span class="caps">URL</span>. This way you don&#8217;t break people&#8217;s basic assumptions about the web itself: if you send a <span class="caps">URL</span> of some page to a friend, she should see the same page, same content. A fancy word for this would be that you&#8217;re being <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer"><em>RESTful</em></a>. Read more about the RESTful approach in <a href="http://www.infoq.com/articles/rest-introduction">Stefan Tilkov&#8217;s articles</a>. There may be some exceptions to this rule, which are discussed below.</p></div>
210
- <p>The <em>setting part</em> is easy. You can set the locale in a <tt>before_filter</tt> in the <tt>ApplicationController</tt> like this:</p>
211
- <div class="code_container">
212
- <pre class="brush: ruby; gutter: false; toolbar: false">
213
- before_filter :set_locale
214
-
215
- def set_locale
216
- I18n.locale = params[:locale] || I18n.default_locale
217
- end
218
- </pre>
219
- </div>
220
- <p>This requires you to pass the locale as a <span class="caps">URL</span> query parameter as in <tt>http://example.com/books?locale=pt</tt>. (This is, for example, Google&#8217;s approach.) So <tt>http://localhost:3000?locale=pt</tt> will load the Portuguese localization, whereas <tt>http://localhost:3000?locale=de</tt> would load the German localization, and so on. You may skip the next section and head over to the <strong>Internationalize your application</strong> section, if you want to try things out by manually placing the locale in the <span class="caps">URL</span> and reloading the page.</p>
221
- <p>Of course, you probably don&#8217;t want to manually include the locale in every <span class="caps">URL</span> all over your application, or want the URLs look differently, e.g. the usual <tt>http://example.com/pt/books</tt> versus <tt>http://example.com/en/books</tt>. Let&#8217;s discuss the different options you have.</p>
222
- <h4 id="setting-the-locale-from-the-domain-name">2.4 Setting the Locale from the Domain Name</h4>
223
- <p>One option you have is to set the locale from the domain name where your application runs. For example, we want <tt>www.example.com</tt> to load the English (or default) locale, and <tt>www.example.es</tt> to load the Spanish locale. Thus the <em>top-level domain name</em> is used for locale setting. This has several advantages:</p>
224
- <ul>
225
- <li>The locale is an <em>obvious</em> part of the <span class="caps">URL</span>.</li>
226
- <li>People intuitively grasp in which language the content will be displayed.</li>
227
- <li>It is very trivial to implement in Rails.</li>
228
- <li>Search engines seem to like that content in different languages lives at different, inter-linked domains.</li>
229
- </ul>
230
- <p>You can implement it like this in your <tt>ApplicationController</tt>:</p>
231
- <div class="code_container">
232
- <pre class="brush: ruby; gutter: false; toolbar: false">
233
- before_filter :set_locale
234
-
235
- def set_locale
236
- I18n.locale = extract_locale_from_tld || I18n.default_locale
237
- end
238
-
239
- # Get locale from top-level domain or return nil if such locale is not available
240
- # You have to put something like:
241
- # 127.0.0.1 application.com
242
- # 127.0.0.1 application.it
243
- # 127.0.0.1 application.pl
244
- # in your /etc/hosts file to try this out locally
245
- def extract_locale_from_tld
246
- parsed_locale = request.host.split('.').last
247
- I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil
248
- end
249
- </pre>
250
- </div>
251
- <p>We can also set the locale from the <em>subdomain</em> in a very similar way:</p>
252
- <div class="code_container">
253
- <pre class="brush: ruby; gutter: false; toolbar: false">
254
- # Get locale code from request subdomain (like http://it.application.local:3000)
255
- # You have to put something like:
256
- # 127.0.0.1 gr.application.local
257
- # in your /etc/hosts file to try this out locally
258
- def extract_locale_from_subdomain
259
- parsed_locale = request.subdomains.first
260
- I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil
261
- end
262
- </pre>
263
- </div>
264
- <p>If your application includes a locale switching menu, you would then have something like this in it:</p>
265
- <div class="code_container">
266
- <pre class="brush: ruby; gutter: false; toolbar: false">
267
- link_to(&quot;Deutsch&quot;, &quot;#{APP_CONFIG[:deutsch_website_url]}#{request.env['REQUEST_URI']}&quot;)
268
- </pre>
269
- </div>
270
- <p>assuming you would set <tt>APP_CONFIG[:deutsch_website_url]</tt> to some value like <tt>http://www.application.de</tt>.</p>
271
- <p>This solution has aforementioned advantages, however, you may not be able or may not want to provide different localizations (&#8220;language versions&#8221;) on different domains. The most obvious solution would be to include locale code in the <span class="caps">URL</span> params (or request path).</p>
272
- <h4 id="setting-the-locale-from-the-url-params">2.5 Setting the Locale from the <span class="caps">URL</span> Params</h4>
273
- <p>The most usual way of setting (and passing) the locale would be to include it in <span class="caps">URL</span> params, as we did in the <tt>I18n.locale = params[:locale]</tt> <em>before_filter</em> in the first example. We would like to have URLs like <tt>www.example.com/books?locale=ja</tt> or <tt>www.example.com/ja/books</tt> in this case.</p>
274
- <p>This approach has almost the same set of advantages as setting the locale from the domain name: namely that it&#8217;s RESTful and in accord with the rest of the World Wide Web. It does require a little bit more work to implement, though.</p>
275
- <p>Getting the locale from <tt>params</tt> and setting it accordingly is not hard; including it in every <span class="caps">URL</span> and thus <strong>passing it through the requests</strong> is. To include an explicit option in every <span class="caps">URL</span> (e.g. <tt>link_to( books_url(:locale => I18n.locale))</tt>) would be tedious and probably impossible, of course.</p>
276
- <p>Rails contains infrastructure for &#8220;centralizing dynamic decisions about the URLs&#8221; in its <a href="http://api.rubyonrails.org/classes/ActionController/Base.html#M000515"><tt>ApplicationController#default_url_options</tt></a>, which is useful precisely in this scenario: it enables us to set &#8220;defaults&#8221; for <a href="http://api.rubyonrails.org/classes/ActionController/Base.html#M000503"><tt>url_for</tt></a> and helper methods dependent on it (by implementing/overriding this method).</p>
277
- <p>We can include something like this in our <tt>ApplicationController</tt> then:</p>
278
- <div class="code_container">
279
- <pre class="brush: ruby; gutter: false; toolbar: false">
280
- # app/controllers/application_controller.rb
281
- def default_url_options(options={})
282
- logger.debug &quot;default_url_options is passed options: #{options.inspect}\n&quot;
283
- { :locale =&gt; I18n.locale }
284
- end
285
- </pre>
286
- </div>
287
- <p>Every helper method dependent on <tt>url_for</tt> (e.g. helpers for named routes like <tt>root_path</tt> or <tt>root_url</tt>, resource routes like <tt>books_path</tt> or <tt>books_url</tt>, etc.) will now <strong>automatically include the locale in the query string</strong>, like this: <tt>http://localhost:3001/?locale=ja</tt>.</p>
288
- <p>You may be satisfied with this. It does impact the readability of URLs, though, when the locale &#8220;hangs&#8221; at the end of every <span class="caps">URL</span> in your application. Moreover, from the architectural standpoint, locale is usually hierarchically above the other parts of the application domain: and URLs should reflect this.</p>
289
- <p>You probably want URLs to look like this: <tt>www.example.com/en/books</tt> (which loads the English locale) and <tt>www.example.com/nl/books</tt> (which loads the Netherlands locale). This is achievable with the &#8220;over-riding <tt>default_url_options</tt>&#8221; strategy from above: you just have to set up your routes with <a href="http://api.rubyonrails.org/classes/ActionController/Resources.html#M000354"><tt>path_prefix</tt></a> option in this way:</p>
290
- <div class="code_container">
291
- <pre class="brush: ruby; gutter: false; toolbar: false">
292
- # config/routes.rb
293
- scope &quot;/:locale&quot; do
294
- resources :books
295
- end
296
- </pre>
297
- </div>
298
- <p>Now, when you call the <tt>books_path</tt> method you should get <tt>"/en/books"</tt> (for the default locale). An <span class="caps">URL</span> like <tt>http://localhost:3001/nl/books</tt> should load the Netherlands locale, then, and following calls to <tt>books_path</tt> should return <tt>"/nl/books"</tt> (because the locale changed).</p>
299
- <p>If you don&#8217;t want to force the use of a locale in your routes you can use an optional path scope (donated by the use brackets) like so:</p>
300
- <div class="code_container">
301
- <pre class="brush: ruby; gutter: false; toolbar: false">
302
- # config/routes.rb
303
- scope &quot;(:locale)&quot;, :locale =&gt; /en|nl/ do
304
- resources :books
305
- end
306
- </pre>
307
- </div>
308
- <p>With this approach you will not get a <tt>Routing Error</tt> when accessing your resources such as <tt>http://localhost:3001/books</tt> without a locale. This is useful for when you want to use the default locale when one is not specified.</p>
309
- <p>Of course, you need to take special care of the root <span class="caps">URL</span> (usually &#8220;homepage&#8221; or &#8220;dashboard&#8221;) of your application. An <span class="caps">URL</span> like <tt>http://localhost:3001/nl</tt> will not work automatically, because the <tt>root :to => "books#index"</tt> declaration in your <tt>routes.rb</tt> doesn&#8217;t take locale into account. (And rightly so: there&#8217;s only one &#8220;root&#8221; <span class="caps">URL</span>.)</p>
310
- <p>You would probably need to map URLs like these:</p>
311
- <div class="code_container">
312
- <pre class="brush: ruby; gutter: false; toolbar: false">
313
- # config/routes.rb
314
- match '/:locale' =&gt; 'dashboard#index'
315
- </pre>
316
- </div>
317
- <p>Do take special care about the <strong>order of your routes</strong>, so this route declaration does not &#8220;eat&#8221; other ones. (You may want to add it directly before the <tt>root :to</tt> declaration.)</p>
318
- <div class="note"><p>Have a look at two plugins which simplify work with routes in this way: Sven Fuchs&#8217;s <a href="https://github.com/svenfuchs/routing-filter/tree/master">routing_filter</a> and Raul Murciano&#8217;s <a href="https://github.com/raul/translate_routes/tree/master">translate_routes</a>.</p></div>
319
- <h4 id="setting-the-locale-from-the-client-supplied-information">2.6 Setting the Locale from the Client Supplied Information</h4>
320
- <p>In specific cases, it would make sense to set the locale from client-supplied information, i.e. not from the <span class="caps">URL</span>. This information may come for example from the users&#8217; preferred language (set in their browser), can be based on the users&#8217; geographical location inferred from their IP, or users can provide it simply by choosing the locale in your application interface and saving it to their profile. This approach is more suitable for web-based applications or services, not for websites &#8212; see the box about <em>sessions</em>, <em>cookies</em> and RESTful architecture above.</p>
321
- <h5 id="using-accept-language">2.6.1 Using <tt>Accept-Language</tt></h5>
322
- <p>One source of client supplied information would be an <tt>Accept-Language</tt> <span class="caps">HTTP</span> header. People may <a href="http://www.w3.org/International/questions/qa-lang-priorities">set this in their browser</a> or other clients (such as <em>curl</em>).</p>
323
- <p>A trivial implementation of using an <tt>Accept-Language</tt> header would be:</p>
324
- <div class="code_container">
325
- <pre class="brush: ruby; gutter: false; toolbar: false">
326
- def set_locale
327
- logger.debug &quot;* Accept-Language: #{request.env['HTTP_ACCEPT_LANGUAGE']}&quot;
328
- I18n.locale = extract_locale_from_accept_language_header
329
- logger.debug &quot;* Locale set to '#{I18n.locale}'&quot;
330
- end
331
- private
332
- def extract_locale_from_accept_language_header
333
- request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
334
- end
335
- </pre>
336
- </div>
337
- <p>Of course, in a production environment you would need much more robust code, and could use a plugin such as Iain Hecker&#8217;s <a href="https://github.com/iain/http_accept_language/tree/master">http_accept_language</a> or even Rack middleware such as Ryan Tomayko&#8217;s <a href="https://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/locale.rb">locale</a>.</p>
338
- <h5 id="using-geoip-or-similar-database">2.6.2 Using GeoIP (or Similar) Database</h5>
339
- <p>Another way of choosing the locale from client information would be to use a database for mapping the client IP to the region, such as <a href="http://www.maxmind.com/app/geolitecountry">GeoIP Lite Country</a>. The mechanics of the code would be very similar to the code above &#8212; you would need to query the database for the user&#8217;s IP, and look up your preferred locale for the country/region/city returned.</p>
340
- <h5 id="user-profile">2.6.3 User Profile</h5>
341
- <p>You can also provide users of your application with means to set (and possibly over-ride) the locale in your application interface, as well. Again, mechanics for this approach would be very similar to the code above &#8212; you&#8217;d probably let users choose a locale from a dropdown list and save it to their profile in the database. Then you&#8217;d set the locale to this value.</p>
342
- <h3 id="internationalizing-your-application">3 Internationalizing your Application</h3>
343
- <p>OK! Now you&#8217;ve initialized I18n support for your Ruby on Rails application and told it which locale to use and how to preserve it between requests. With that in place, you&#8217;re now ready for the really interesting stuff.</p>
344
- <p>Let&#8217;s <em>internationalize</em> our application, i.e. abstract every locale-specific parts, and then <em>localize</em> it, i.e. provide necessary translations for these abstracts.</p>
345
- <p>You most probably have something like this in one of your applications:</p>
346
- <div class="code_container">
347
- <pre class="brush: ruby; gutter: false; toolbar: false">
348
- # config/routes.rb
349
- Yourapp::Application.routes.draw do
350
- root :to =&gt; &quot;home#index&quot;
351
- end
352
-
353
- # app/controllers/home_controller.rb
354
- class HomeController &lt; ApplicationController
355
- def index
356
- flash[:notice] = &quot;Hello Flash&quot;
357
- end
358
- end
359
-
360
- # app/views/home/index.html.erb
361
- &lt;h1&gt;Hello World&lt;/h1&gt;
362
- &lt;p&gt;&lt;%= flash[:notice] %&gt;&lt;/p&gt;
363
- </pre>
364
- </div>
365
- <p><img src="images/i18n/demo_untranslated.png" title="rails i18n demo untranslated" alt="rails i18n demo untranslated" /></p>
366
- <h4 id="adding-translations">3.1 Adding Translations</h4>
367
- <p>Obviously there are <strong>two strings that are localized to English</strong>. In order to internationalize this code, <strong>replace these strings</strong> with calls to Rails&#8217; <tt>#t</tt> helper with a key that makes sense for the translation:</p>
368
- <div class="code_container">
369
- <pre class="brush: ruby; gutter: false; toolbar: false">
370
- # app/controllers/home_controller.rb
371
- class HomeController &lt; ApplicationController
372
- def index
373
- flash[:notice] = t(:hello_flash)
374
- end
375
- end
376
-
377
- # app/views/home/index.html.erb
378
- &lt;h1&gt;&lt;%=t :hello_world %&gt;&lt;/h1&gt;
379
- &lt;p&gt;&lt;%= flash[:notice] %&gt;&lt;/p&gt;
380
- </pre>
381
- </div>
382
- <p>When you now render this view, it will show an error message which tells you that the translations for the keys <tt>:hello_world</tt> and <tt>:hello_flash</tt> are missing.</p>
383
- <p><img src="images/i18n/demo_translation_missing.png" title="rails i18n demo translation missing" alt="rails i18n demo translation missing" /></p>
384
- <div class="note"><p>Rails adds a <tt>t</tt> (<tt>translate</tt>) helper method to your views so that you do not need to spell out <tt>I18n.t</tt> all the time. Additionally this helper will catch missing translations and wrap the resulting error message into a <tt>&lt;span class="translation_missing"&gt;</tt>.</p></div>
385
- <p>So let&#8217;s add the missing translations into the dictionary files (i.e. do the &#8220;localization&#8221; part):</p>
386
- <div class="code_container">
387
- <pre class="brush: ruby; gutter: false; toolbar: false">
388
- # config/locales/en.yml
389
- en:
390
- hello_world: Hello world!
391
- hello_flash: Hello flash!
392
-
393
- # config/locales/pirate.yml
394
- pirate:
395
- hello_world: Ahoy World
396
- hello_flash: Ahoy Flash
397
- </pre>
398
- </div>
399
- <p>There you go. Because you haven&#8217;t changed the default_locale, I18n will use English. Your application now shows:</p>
400
- <p><img src="images/i18n/demo_translated_en.png" title="rails i18n demo translated to English" alt="rails i18n demo translated to English" /></p>
401
- <p>And when you change the <span class="caps">URL</span> to pass the pirate locale (<tt>http://localhost:3000?locale=pirate</tt>), you&#8217;ll get:</p>
402
- <p><img src="images/i18n/demo_translated_pirate.png" title="rails i18n demo translated to pirate" alt="rails i18n demo translated to pirate" /></p>
403
- <div class="note"><p>You need to restart the server when you add new locale files.</p></div>
404
- <p>You may use <span class="caps">YAML</span> (<tt>.yml</tt>) or plain Ruby (<tt>.rb</tt>) files for storing your translations in SimpleStore. <span class="caps">YAML</span> is the preferred option among Rails developers. However, it has one big disadvantage. <span class="caps">YAML</span> is very sensitive to whitespace and special characters, so the application may not load your dictionary properly. Ruby files will crash your application on first request, so you may easily find what&#8217;s wrong. (If you encounter any &#8220;weird issues&#8221; with <span class="caps">YAML</span> dictionaries, try putting the relevant portion of your dictionary into a Ruby file.)</p>
405
- <h4 id="passing-variables-to-translations">3.2 Passing variables to translations</h4>
406
- <p>You can use variables in the translation messages and pass their values from the view.</p>
407
- <div class="code_container">
408
- <pre class="brush: ruby; gutter: false; toolbar: false">
409
- # app/views/home/index.html.erb
410
- &lt;%=t 'greet_username', :user =&gt; &quot;Bill&quot;, :message =&gt; &quot;Goodbye&quot; %&gt;
411
-
412
- # config/locales/en.yml
413
- en:
414
- greet_username: &quot;%{message}, %{user}!&quot;
415
- </pre>
416
- </div>
417
- <h4 id="adding-date-time-formats">3.3 Adding Date/Time Formats</h4>
418
- <p>OK! Now let&#8217;s add a timestamp to the view, so we can demo the <strong>date/time localization</strong> feature as well. To localize the time format you pass the Time object to <tt>I18n.l</tt> or (preferably) use Rails&#8217; <tt>#l</tt> helper. You can pick a format by passing the <tt>:format</tt> option &#8212; by default the <tt>:default</tt> format is used.</p>
419
- <div class="code_container">
420
- <pre class="brush: ruby; gutter: false; toolbar: false">
421
- # app/views/home/index.html.erb
422
- &lt;h1&gt;&lt;%=t :hello_world %&gt;&lt;/h1&gt;
423
- &lt;p&gt;&lt;%= flash[:notice] %&gt;&lt;/p
424
- &lt;p&gt;&lt;%= l Time.now, :format =&gt; :short %&gt;&lt;/p&gt;
425
- </pre>
426
- </div>
427
- <p>And in our pirate translations file let&#8217;s add a time format (it&#8217;s already there in Rails&#8217; defaults for English):</p>
428
- <div class="code_container">
429
- <pre class="brush: ruby; gutter: false; toolbar: false">
430
- # config/locales/pirate.yml
431
- pirate:
432
- time:
433
- formats:
434
- short: &quot;arrrround %H'ish&quot;
435
- </pre>
436
- </div>
437
- <p>So that would give you:</p>
438
- <p><img src="images/i18n/demo_localized_pirate.png" title="rails i18n demo localized time to pirate" alt="rails i18n demo localized time to pirate" /></p>
439
- <div class="info"><p>Right now you might need to add some more date/time formats in order to make the I18n backend work as expected (at least for the &#8216;pirate&#8217; locale). Of course, there&#8217;s a great chance that somebody already did all the work by <strong>translating Rails&#8217; defaults for your locale</strong>. See the <a href="https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale">rails-i18n repository at Github</a> for an archive of various locale files. When you put such file(s) in <tt>config/locales/</tt> directory, they will automatically be ready for use.</p></div>
440
- <h4 id="localized-views">3.4 Localized Views</h4>
441
- <p>Rails 2.3 introduces another convenient localization feature: localized views (templates). Let&#8217;s say you have a <em>BooksController</em> in your application. Your <em>index</em> action renders content in <tt>app/views/books/index.html.erb</tt> template. When you put a <em>localized variant</em> of this template: <strong><tt>index.es.html.erb</tt></strong> in the same directory, Rails will render content in this template, when the locale is set to <tt>:es</tt>. When the locale is set to the default locale, the generic <tt>index.html.erb</tt> view will be used. (Future Rails versions may well bring this <em>automagic</em> localization to assets in <tt>public</tt>, etc.)</p>
442
- <p>You can make use of this feature, e.g. when working with a large amount of static content, which would be clumsy to put inside <span class="caps">YAML</span> or Ruby dictionaries. Bear in mind, though, that any change you would like to do later to the template must be propagated to all of them.</p>
443
- <h4 id="organization-of-locale-files">3.5 Organization of Locale Files</h4>
444
- <p>When you are using the default SimpleStore shipped with the i18n library, dictionaries are stored in plain-text files on the disc. Putting translations for all parts of your application in one file per locale could be hard to manage. You can store these files in a hierarchy which makes sense to you.</p>
445
- <p>For example, your <tt>config/locales</tt> directory could look like this:</p>
446
- <pre>
447
- |-defaults
448
- |---es.rb
449
- |---en.rb
450
- |-models
451
- |---book
452
- |-----es.rb
453
- |-----en.rb
454
- |-views
455
- |---defaults
456
- |-----es.rb
457
- |-----en.rb
458
- |---books
459
- |-----es.rb
460
- |-----en.rb
461
- |---users
462
- |-----es.rb
463
- |-----en.rb
464
- |---navigation
465
- |-----es.rb
466
- |-----en.rb
467
- </pre>
468
- <p>This way, you can separate model and model attribute names from text inside views, and all of this from the &#8220;defaults&#8221; (e.g. date and time formats). Other stores for the i18n library could provide different means of such separation.</p>
469
- <div class="note"><p>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:</p></div>
470
- <div class="code_container">
471
- <pre class="brush: ruby; gutter: false; toolbar: false">
472
- # config/application.rb
473
- config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
474
- </pre>
475
- </div>
476
- <p>Do check the <a href="http://rails-i18n.org/wiki">Rails i18n Wiki</a> for list of tools available for managing translations.</p>
477
- <h3 id="overview-of-the-i18n-api-features">4 Overview of the I18n <span class="caps">API</span> Features</h3>
478
- <p>You should have good understanding of using the i18n library now, knowing all necessary aspects of internationalizing a basic Rails application. In the following chapters, we&#8217;ll cover it&#8217;s features in more depth.</p>
479
- <p>Covered are features like these:</p>
480
- <ul>
481
- <li>looking up translations</li>
482
- <li>interpolating data into translations</li>
483
- <li>pluralizing translations</li>
484
- <li>using safe <span class="caps">HTML</span> translations</li>
485
- <li>localizing dates, numbers, currency, etc.</li>
486
- </ul>
487
- <h4 id="looking-up-translations">4.1 Looking up Translations</h4>
488
- <h5 id="basic-lookup-scopes-and-nested-keys">4.1.1 Basic Lookup, Scopes and Nested Keys</h5>
489
- <p>Translations are looked up by keys which can be both Symbols or Strings, so these calls are equivalent:</p>
490
- <div class="code_container">
491
- <pre class="brush: ruby; gutter: false; toolbar: false">
492
- I18n.t :message
493
- I18n.t 'message'
494
- </pre>
495
- </div>
496
- <p>The <tt>translate</tt> method also takes a <tt>:scope</tt> option which can contain one or more additional keys that will be used to specify a “namespace” or scope for a translation key:</p>
497
- <div class="code_container">
498
- <pre class="brush: ruby; gutter: false; toolbar: false">
499
- I18n.t :record_invalid, :scope =&gt; [:activerecord, :errors, :messages]
500
- </pre>
501
- </div>
502
- <p>This looks up the <tt>:record_invalid</tt> message in the Active Record error messages.</p>
503
- <p>Additionally, both the key and scopes can be specified as dot-separated keys as in:</p>
504
- <div class="code_container">
505
- <pre class="brush: ruby; gutter: false; toolbar: false">
506
- I18n.translate &quot;activerecord.errors.messages.record_invalid&quot;
507
- </pre>
508
- </div>
509
- <p>Thus the following calls are equivalent:</p>
510
- <div class="code_container">
511
- <pre class="brush: ruby; gutter: false; toolbar: false">
512
- I18n.t 'activerecord.errors.messages.record_invalid'
513
- I18n.t 'errors.messages.record_invalid', :scope =&gt; :active_record
514
- I18n.t :record_invalid, :scope =&gt; 'activerecord.errors.messages'
515
- I18n.t :record_invalid, :scope =&gt; [:activerecord, :errors, :messages]
516
- </pre>
517
- </div>
518
- <h5 id="defaults">4.1.2 Defaults</h5>
519
- <p>When a <tt>:default</tt> option is given, its value will be returned if the translation is missing:</p>
520
- <div class="code_container">
521
- <pre class="brush: ruby; gutter: false; toolbar: false">
522
- I18n.t :missing, :default =&gt; 'Not here'
523
- # =&gt; 'Not here'
524
- </pre>
525
- </div>
526
- <p>If the <tt>:default</tt> value is a Symbol, it will be used as a key and translated. One can provide multiple values as default. The first one that results in a value will be returned.</p>
527
- <p>E.g., the following first tries to translate the key <tt>:missing</tt> and then the key <tt>:also_missing.</tt> As both do not yield a result, the string &#8220;Not here&#8221; will be returned:</p>
528
- <div class="code_container">
529
- <pre class="brush: ruby; gutter: false; toolbar: false">
530
- I18n.t :missing, :default =&gt; [:also_missing, 'Not here']
531
- # =&gt; 'Not here'
532
- </pre>
533
- </div>
534
- <h5 id="bulk-and-namespace-lookup">4.1.3 Bulk and Namespace Lookup</h5>
535
- <p>To look up multiple translations at once, an array of keys can be passed:</p>
536
- <div class="code_container">
537
- <pre class="brush: ruby; gutter: false; toolbar: false">
538
- I18n.t [:odd, :even], :scope =&gt; 'activerecord.errors.messages'
539
- # =&gt; [&quot;must be odd&quot;, &quot;must be even&quot;]
540
- </pre>
541
- </div>
542
- <p>Also, a key can translate to a (potentially nested) hash of grouped translations. E.g., one can receive <em>all</em> Active Record error messages as a Hash with:</p>
543
- <div class="code_container">
544
- <pre class="brush: ruby; gutter: false; toolbar: false">
545
- I18n.t 'activerecord.errors.messages'
546
- # =&gt; { :inclusion =&gt; &quot;is not included in the list&quot;, :exclusion =&gt; ... }
547
- </pre>
548
- </div>
549
- <h5 id="lazy-lookup">4.1.4 &#8220;Lazy&#8221; Lookup</h5>
550
- <p>Rails implements a convenient way to look up the locale inside <em>views</em>. When you have the following dictionary:</p>
551
- <div class="code_container">
552
- <pre class="brush: plain; gutter: false; toolbar: false">
553
- es:
554
- books:
555
- index:
556
- title: &quot;Título&quot;
557
- </pre>
558
- </div>
559
- <p>you can look up the <tt>books.index.title</tt> value <strong>inside</strong> <tt>app/views/books/index.html.erb</tt> template like this (note the dot):</p>
560
- <div class="code_container">
561
- <pre class="brush: ruby; gutter: false; toolbar: false">
562
- &lt;%= t '.title' %&gt;
563
- </pre>
564
- </div>
565
- <h4 id="interpolation">4.2 Interpolation</h4>
566
- <p>In many cases you want to abstract your translations so that <strong>variables can be interpolated into the translation</strong>. For this reason the I18n <span class="caps">API</span> provides an interpolation feature.</p>
567
- <p>All options besides <tt>:default</tt> and <tt>:scope</tt> that are passed to <tt>#translate</tt> will be interpolated to the translation:</p>
568
- <div class="code_container">
569
- <pre class="brush: ruby; gutter: false; toolbar: false">
570
- I18n.backend.store_translations :en, :thanks =&gt; 'Thanks %{name}!'
571
- I18n.translate :thanks, :name =&gt; 'Jeremy'
572
- # =&gt; 'Thanks Jeremy!'
573
- </pre>
574
- </div>
575
- <p>If a translation uses <tt>:default</tt> or <tt>:scope</tt> as an interpolation variable, an I<tt>18n::ReservedInterpolationKey</tt> exception is raised. If a translation expects an interpolation variable, but this has not been passed to <tt>#translate</tt>, an <tt>I18n::MissingInterpolationArgument</tt> exception is raised.</p>
576
- <h4 id="pluralization">4.3 Pluralization</h4>
577
- <p>In English there are only one singular and one plural form for a given string, e.g. &#8220;1 message&#8221; and &#8220;2 messages&#8221;. Other languages (<a href="http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ar">Arabic</a>, <a href="http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ja">Japanese</a>, <a href="http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ru">Russian</a> and many more) have different grammars that have additional or fewer <a href="http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html">plural forms</a>. Thus, the I18n <span class="caps">API</span> provides a flexible pluralization feature.</p>
578
- <p>The <tt>:count</tt> interpolation variable has a special role in that it both is interpolated to the translation and used to pick a pluralization from the translations according to the pluralization rules defined by <span class="caps">CLDR</span>:</p>
579
- <div class="code_container">
580
- <pre class="brush: ruby; gutter: false; toolbar: false">
581
- I18n.backend.store_translations :en, :inbox =&gt; {
582
- :one =&gt; '1 message',
583
- :other =&gt; '%{count} messages'
584
- }
585
- I18n.translate :inbox, :count =&gt; 2
586
- # =&gt; '2 messages'
587
- </pre>
588
- </div>
589
- <p>The algorithm for pluralizations in <tt>:en</tt> is as simple as:</p>
590
- <div class="code_container">
591
- <pre class="brush: ruby; gutter: false; toolbar: false">
592
- entry[count == 1 ? 0 : 1]
593
- </pre>
594
- </div>
595
- <p>I.e. the translation denoted as <tt>:one</tt> is regarded as singular, the other is used as plural (including the count being zero).</p>
596
- <p>If the lookup for the key does not return a Hash suitable for pluralization, an <tt>18n::InvalidPluralizationData</tt> exception is raised.</p>
597
- <h4 id="setting-and-passing-a-locale">4.4 Setting and Passing a Locale</h4>
598
- <p>The locale can be either set pseudo-globally to <tt>I18n.locale</tt> (which uses <tt>Thread.current</tt> like, e.g., <tt>Time.zone</tt>) or can be passed as an option to <tt>#translate</tt> and <tt>#localize</tt>.</p>
599
- <p>If no locale is passed, <tt>I18n.locale</tt> is used:</p>
600
- <div class="code_container">
601
- <pre class="brush: ruby; gutter: false; toolbar: false">
602
- I18n.locale = :de
603
- I18n.t :foo
604
- I18n.l Time.now
605
- </pre>
606
- </div>
607
- <p>Explicitly passing a locale:</p>
608
- <div class="code_container">
609
- <pre class="brush: ruby; gutter: false; toolbar: false">
610
- I18n.t :foo, :locale =&gt; :de
611
- I18n.l Time.now, :locale =&gt; :de
612
- </pre>
613
- </div>
614
- <p>The <tt>I18n.locale</tt> defaults to <tt>I18n.default_locale</tt> which defaults to :<tt>en</tt>. The default locale can be set like this:</p>
615
- <div class="code_container">
616
- <pre class="brush: ruby; gutter: false; toolbar: false">
617
- I18n.default_locale = :de
618
- </pre>
619
- </div>
620
- <h4 id="using-safe-html-translations">4.5 Using Safe <span class="caps">HTML</span> Translations</h4>
621
- <p>Keys with a &#8216;_html&#8217; suffix and keys named &#8216;html&#8217; are marked as <span class="caps">HTML</span> safe. Use them in views without escaping.</p>
622
- <div class="code_container">
623
- <pre class="brush: ruby; gutter: false; toolbar: false">
624
- # config/locales/en.yml
625
- en:
626
- welcome: &lt;b&gt;welcome!&lt;/b&gt;
627
- hello_html: &lt;b&gt;hello!&lt;/b&gt;
628
- title:
629
- html: &lt;b&gt;title!&lt;/b&gt;
630
-
631
- # app/views/home/index.html.erb
632
- &lt;div&gt;&lt;%= t('welcome') %&gt;&lt;/div&gt;
633
- &lt;div&gt;&lt;%= raw t('welcome') %&gt;&lt;/div&gt;
634
- &lt;div&gt;&lt;%= t('hello_html') %&gt;&lt;/div&gt;
635
- &lt;div&gt;&lt;%= t('title.html') %&gt;&lt;/div&gt;
636
- </pre>
637
- </div>
638
- <p><img src="images/i18n/demo_html_safe.png" title="i18n demo html safe" alt="i18n demo html safe" /></p>
639
- <h3 id="how-to-store-your-custom-translations">5 How to Store your Custom Translations</h3>
640
- <p>The Simple backend shipped with Active Support allows you to store translations in both plain Ruby and <span class="caps">YAML</span> format. <sup class="footnote" id="fnr2"><a href="#fn2">2</a></sup></p>
641
- <p>For example a Ruby Hash providing translations can look like this:</p>
642
- <div class="code_container">
643
- <pre class="brush: ruby; gutter: false; toolbar: false">
644
- {
645
- :pt =&gt; {
646
- :foo =&gt; {
647
- :bar =&gt; &quot;baz&quot;
648
- }
649
- }
650
- }
651
- </pre>
652
- </div>
653
- <p>The equivalent <span class="caps">YAML</span> file would look like this:</p>
654
- <div class="code_container">
655
- <pre class="brush: ruby; gutter: false; toolbar: false">
656
- pt:
657
- foo:
658
- bar: baz
659
- </pre>
660
- </div>
661
- <p>As you see, in both cases the top level key is the locale. <tt>:foo</tt> is a namespace key and <tt>:bar</tt> is the key for the translation &#8220;baz&#8221;.</p>
662
- <p>Here is a &#8220;real&#8221; example from the Active Support <tt>en.yml</tt> translations <span class="caps">YAML</span> file:</p>
663
- <div class="code_container">
664
- <pre class="brush: ruby; gutter: false; toolbar: false">
665
- en:
666
- date:
667
- formats:
668
- default: &quot;%Y-%m-%d&quot;
669
- short: &quot;%b %d&quot;
670
- long: &quot;%B %d, %Y&quot;
671
- </pre>
672
- </div>
673
- <p>So, all of the following equivalent lookups will return the <tt>:short</tt> date format <tt>"%B %d"</tt>:</p>
674
- <div class="code_container">
675
- <pre class="brush: ruby; gutter: false; toolbar: false">
676
- I18n.t 'date.formats.short'
677
- I18n.t 'formats.short', :scope =&gt; :date
678
- I18n.t :short, :scope =&gt; 'date.formats'
679
- I18n.t :short, :scope =&gt; [:date, :formats]
680
- </pre>
681
- </div>
682
- <p>Generally we recommend using <span class="caps">YAML</span> as a format for storing translations. There are cases, though, where you want to store Ruby lambdas as part of your locale data, e.g. for special date formats.</p>
683
- <h4 id="translations-for-active-record-models">5.1 Translations for Active Record Models</h4>
684
- <p>You can use the methods <tt>Model.model_name.human</tt> and <tt>Model.human_attribute_name(attribute)</tt> to transparently look up translations for your model and attribute names.</p>
685
- <p>For example when you add the following translations:</p>
686
- <div class="code_container">
687
- <pre class="brush: ruby; gutter: false; toolbar: false">
688
- en:
689
- activerecord:
690
- models:
691
- user: Dude
692
- attributes:
693
- user:
694
- login: &quot;Handle&quot;
695
- # will translate User attribute &quot;login&quot; as &quot;Handle&quot;
696
- </pre>
697
- </div>
698
- <p>Then <tt>User.model_name.human</tt> will return &#8220;Dude&#8221; and <tt>User.human_attribute_name("login")</tt> will return &#8220;Handle&#8221;.</p>
699
- <h5 id="error-message-scopes">5.1.1 Error Message Scopes</h5>
700
- <p>Active Record validation error messages can also be translated easily. Active Record gives you a couple of namespaces where you can place your message translations in order to provide different messages and translation for certain models, attributes, and/or validations. It also transparently takes single table inheritance into account.</p>
701
- <p>This gives you quite powerful means to flexibly adjust your messages to your application&#8217;s needs.</p>
702
- <p>Consider a User model with a validation for the name attribute like this:</p>
703
- <div class="code_container">
704
- <pre class="brush: ruby; gutter: false; toolbar: false">
705
- class User &lt; ActiveRecord::Base
706
- validates :name, :presence =&gt; true
707
- end
708
- </pre>
709
- </div>
710
- <p>The key for the error message in this case is <tt>:blank</tt>. Active Record will look up this key in the namespaces:</p>
711
- <div class="code_container">
712
- <pre class="brush: ruby; gutter: false; toolbar: false">
713
- activerecord.errors.models.[model_name].attributes.[attribute_name]
714
- activerecord.errors.models.[model_name]
715
- activerecord.errors.messages
716
- errors.attributes.[attribute_name]
717
- errors.messages
718
- </pre>
719
- </div>
720
- <p>Thus, in our example it will try the following keys in this order and return the first result:</p>
721
- <div class="code_container">
722
- <pre class="brush: ruby; gutter: false; toolbar: false">
723
- activerecord.errors.models.user.attributes.name.blank
724
- activerecord.errors.models.user.blank
725
- activerecord.errors.messages.blank
726
- errors.attributes.name.blank
727
- errors.messages.blank
728
- </pre>
729
- </div>
730
- <p>When your models are additionally using inheritance then the messages are looked up in the inheritance chain.</p>
731
- <p>For example, you might have an Admin model inheriting from User:</p>
732
- <div class="code_container">
733
- <pre class="brush: ruby; gutter: false; toolbar: false">
734
- class Admin &lt; User
735
- validates :name, :presence =&gt; true
736
- end
737
- </pre>
738
- </div>
739
- <p>Then Active Record will look for messages in this order:</p>
740
- <div class="code_container">
741
- <pre class="brush: ruby; gutter: false; toolbar: false">
742
- activerecord.errors.models.admin.attributes.name.blank
743
- activerecord.errors.models.admin.blank
744
- activerecord.errors.models.user.attributes.name.blank
745
- activerecord.errors.models.user.blank
746
- activerecord.errors.messages.blank
747
- errors.attributes.name.blank
748
- errors.messages.blank
749
- </pre>
750
- </div>
751
- <p>This way you can provide special translations for various error messages at different points in your models inheritance chain and in the attributes, models, or default scopes.</p>
752
- <h5 id="error-message-interpolation">5.1.2 Error Message Interpolation</h5>
753
- <p>The translated model name, translated attribute name, and value are always available for interpolation.</p>
754
- <p>So, for example, instead of the default error message <tt>"can not be blank"</tt> you could use the attribute name like this : <tt>"Please fill in your %{attribute}"</tt>.</p>
755
- <ul>
756
- <li><tt>count</tt>, where available, can be used for pluralization if present:</li>
757
- </ul>
758
- <table>
759
- <tr>
760
- <th>validation </th>
761
- <th>with option </th>
762
- <th>message </th>
763
- <th>interpolation</th>
764
- </tr>
765
- <tr>
766
- <td> confirmation </td>
767
- <td> &#8211; </td>
768
- <td> :confirmation </td>
769
- <td> -</td>
770
- </tr>
771
- <tr>
772
- <td> acceptance </td>
773
- <td> &#8211; </td>
774
- <td> :accepted </td>
775
- <td> -</td>
776
- </tr>
777
- <tr>
778
- <td> presence </td>
779
- <td> &#8211; </td>
780
- <td> :blank </td>
781
- <td> -</td>
782
- </tr>
783
- <tr>
784
- <td> length </td>
785
- <td> :within, :in </td>
786
- <td> :too_short </td>
787
- <td> count</td>
788
- </tr>
789
- <tr>
790
- <td> length </td>
791
- <td> :within, :in </td>
792
- <td> :too_long </td>
793
- <td> count</td>
794
- </tr>
795
- <tr>
796
- <td> length </td>
797
- <td> :is </td>
798
- <td> :wrong_length </td>
799
- <td> count</td>
800
- </tr>
801
- <tr>
802
- <td> length </td>
803
- <td> :minimum </td>
804
- <td> :too_short </td>
805
- <td> count</td>
806
- </tr>
807
- <tr>
808
- <td> length </td>
809
- <td> :maximum </td>
810
- <td> :too_long </td>
811
- <td> count</td>
812
- </tr>
813
- <tr>
814
- <td> uniqueness </td>
815
- <td> &#8211; </td>
816
- <td> :taken </td>
817
- <td> -</td>
818
- </tr>
819
- <tr>
820
- <td> format </td>
821
- <td> &#8211; </td>
822
- <td> :invalid </td>
823
- <td> -</td>
824
- </tr>
825
- <tr>
826
- <td> inclusion </td>
827
- <td> &#8211; </td>
828
- <td> :inclusion </td>
829
- <td> -</td>
830
- </tr>
831
- <tr>
832
- <td> exclusion </td>
833
- <td> &#8211; </td>
834
- <td> :exclusion </td>
835
- <td> -</td>
836
- </tr>
837
- <tr>
838
- <td> associated </td>
839
- <td> &#8211; </td>
840
- <td> :invalid </td>
841
- <td> -</td>
842
- </tr>
843
- <tr>
844
- <td> numericality </td>
845
- <td> &#8211; </td>
846
- <td> :not_a_number </td>
847
- <td> -</td>
848
- </tr>
849
- <tr>
850
- <td> numericality </td>
851
- <td> :greater_than </td>
852
- <td> :greater_than </td>
853
- <td> count</td>
854
- </tr>
855
- <tr>
856
- <td> numericality </td>
857
- <td> :greater_than_or_equal_to </td>
858
- <td> :greater_than_or_equal_to </td>
859
- <td> count</td>
860
- </tr>
861
- <tr>
862
- <td> numericality </td>
863
- <td> :equal_to </td>
864
- <td> :equal_to </td>
865
- <td> count</td>
866
- </tr>
867
- <tr>
868
- <td> numericality </td>
869
- <td> :less_than </td>
870
- <td> :less_than </td>
871
- <td> count</td>
872
- </tr>
873
- <tr>
874
- <td> numericality </td>
875
- <td> :less_than_or_equal_to </td>
876
- <td> :less_than_or_equal_to </td>
877
- <td> count</td>
878
- </tr>
879
- <tr>
880
- <td> numericality </td>
881
- <td> :odd </td>
882
- <td> :odd </td>
883
- <td> -</td>
884
- </tr>
885
- <tr>
886
- <td> numericality </td>
887
- <td> :even </td>
888
- <td> :even </td>
889
- <td> -</td>
890
- </tr>
891
- </table>
892
- <h5 id="translations-for-the-active-record-error_messages_for-helper">5.1.3 Translations for the Active Record <tt>error_messages_for</tt> Helper</h5>
893
- <p>If you are using the Active Record <tt>error_messages_for</tt> helper, you will want to add translations for it.</p>
894
- <p>Rails ships with the following translations:</p>
895
- <div class="code_container">
896
- <pre class="brush: plain; gutter: false; toolbar: false">
897
- en:
898
- activerecord:
899
- errors:
900
- template:
901
- header:
902
- one: &quot;1 error prohibited this %{model} from being saved&quot;
903
- other: &quot;%{count} errors prohibited this %{model} from being saved&quot;
904
- body: &quot;There were problems with the following fields:&quot;
905
- </pre>
906
- </div>
907
- <h4 id="overview-of-other-built-in-methods-that-provide-i18n-support">5.2 Overview of Other Built-In Methods that Provide I18n Support</h4>
908
- <p>Rails uses fixed strings and other localizations, such as format strings and other format information in a couple of helpers. Here&#8217;s a brief overview.</p>
909
- <h5 id="action-view-helper-methods">5.2.1 Action View Helper Methods</h5>
910
- <ul>
911
- <li><tt>distance_of_time_in_words</tt> translates and pluralizes its result and interpolates the number of seconds, minutes, hours, and so on. See <a href="https://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L51">datetime.distance_in_words</a> translations.</li>
912
- </ul>
913
- <ul>
914
- <li><tt>datetime_select</tt> and <tt>select_month</tt> use translated month names for populating the resulting select tag. See <a href="https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L15">date.month_names</a> for translations. <tt>datetime_select</tt> also looks up the order option from <a href="https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L18">date.order</a> (unless you pass the option explicitly). All date selection helpers translate the prompt using the translations in the <a href="https://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L83">datetime.prompts</a> scope if applicable.</li>
915
- </ul>
916
- <ul>
917
- <li>The <tt>number_to_currency</tt>, <tt>number_with_precision</tt>, <tt>number_to_percentage</tt>, <tt>number_with_delimiter</tt>, and <tt>number_to_human_size</tt> helpers use the number format settings located in the <a href="https://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L2">number</a> scope.</li>
918
- </ul>
919
- <h5 id="active-record-methods">5.2.2 Active Record Methods</h5>
920
- <ul>
921
- <li><tt>model_name.human</tt> and <tt>human_attribute_name</tt> use translations for model names and attribute names if available in the <a href="https://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml#L29">activerecord.models</a> scope. They also support translations for inherited class names (e.g. for use with <span class="caps">STI</span>) as explained above in &#8220;Error message scopes&#8221;.</li>
922
- </ul>
923
- <ul>
924
- <li><tt>ActiveRecord::Errors#generate_message</tt> (which is used by Active Record validations but may also be used manually) uses <tt>model_name.human</tt> and <tt>human_attribute_name</tt> (see above). It also translates the error message and supports translations for inherited class names as explained above in &#8220;Error message scopes&#8221;.</li>
925
- </ul>
926
- <p>*<tt> ActiveRecord::Errors#full_messages</tt> prepends the attribute name to the error message using a separator that will be looked up from <a href="https://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L91">activerecord.errors.format.separator</a> (and which defaults to <tt>'&nbsp;'</tt>).</p>
927
- <h5 id="active-support-methods">5.2.3 Active Support Methods</h5>
928
- <ul>
929
- <li><tt>Array#to_sentence</tt> uses format settings as given in the <a href="https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L30">support.array</a> scope.</li>
930
- </ul>
931
- <h3 id="customize-your-i18n-setup">6 Customize your I18n Setup</h3>
932
- <h4 id="using-different-backends">6.1 Using Different Backends</h4>
933
- <p>For several reasons the Simple backend shipped with Active Support only does the &#8220;simplest thing that could possibly work&#8221; <em>for Ruby on Rails</em> <sup class="footnote" id="fnr3"><a href="#fn3">3</a></sup> &#8230; which means that it is only guaranteed to work for English and, as a side effect, languages that are very similar to English. Also, the simple backend is only capable of reading translations but can not dynamically store them to any format.</p>
934
- <p>That does not mean you&#8217;re stuck with these limitations, though. The Ruby I18n gem makes it very easy to exchange the Simple backend implementation with something else that fits better for your needs. E.g. you could exchange it with Globalize&#8217;s Static backend:</p>
935
- <div class="code_container">
936
- <pre class="brush: ruby; gutter: false; toolbar: false">
937
- I18n.backend = Globalize::Backend::Static.new
938
- </pre>
939
- </div>
940
- <p>You can also use the Chain backend to chain multiple backends together. This is useful when you want to use standard translations with a Simple backend but store custom application translations in a database or other backends. For example, you could use the Active Record backend and fall back to the (default) Simple backend:</p>
941
- <div class="code_container">
942
- <pre class="brush: ruby; gutter: false; toolbar: false">
943
- I18n.backend = I18n::Backend::Chain.new(I18n::Backend::ActiveRecord.new, I18n.backend)
944
- </pre>
945
- </div>
946
- <h4 id="using-different-exception-handlers">6.2 Using Different Exception Handlers</h4>
947
- <p>The I18n <span class="caps">API</span> defines the following exceptions that will be raised by backends when the corresponding unexpected conditions occur:</p>
948
- <div class="code_container">
949
- <pre class="brush: ruby; gutter: false; toolbar: false">
950
- MissingTranslationData # no translation was found for the requested key
951
- InvalidLocale # the locale set to I18n.locale is invalid (e.g. nil)
952
- InvalidPluralizationData # a count option was passed but the translation data is not suitable for pluralization
953
- MissingInterpolationArgument # the translation expects an interpolation argument that has not been passed
954
- ReservedInterpolationKey # the translation contains a reserved interpolation variable name (i.e. one of: scope, default)
955
- UnknownFileType # the backend does not know how to handle a file type that was added to I18n.load_path
956
- </pre>
957
- </div>
958
- <p>The I18n <span class="caps">API</span> will catch all of these exceptions when they are thrown in the backend and pass them to the default_exception_handler method. This method will re-raise all exceptions except for <tt>MissingTranslationData</tt> exceptions. When a <tt>MissingTranslationData</tt> exception has been caught, it will return the exception’s error message string containing the missing key/scope.</p>
959
- <p>The reason for this is that during development you&#8217;d usually want your views to still render even though a translation is missing.</p>
960
- <p>In other contexts you might want to change this behaviour, though. E.g. the default exception handling does not allow to catch missing translations during automated tests easily. For this purpose a different exception handler can be specified. The specified exception handler must be a method on the I18n module:</p>
961
- <div class="code_container">
962
- <pre class="brush: ruby; gutter: false; toolbar: false">
963
- module I18n
964
- def self.just_raise_that_exception(*args)
965
- raise args.first
966
- end
967
- end
968
-
969
- I18n.exception_handler = :just_raise_that_exception
970
- </pre>
971
- </div>
972
- <p>This would re-raise all caught exceptions including <tt>MissingTranslationData</tt>.</p>
973
- <p>Another example where the default behaviour is less desirable is the Rails TranslationHelper which provides the method <tt>#t</tt> (as well as <tt>#translate</tt>). When a <tt>MissingTranslationData</tt> exception occurs in this context, the helper wraps the message into a span with the <span class="caps">CSS</span> class <tt>translation_missing</tt>.</p>
974
- <p>To do so, the helper forces <tt>I18n#translate</tt> to raise exceptions no matter what exception handler is defined by setting the <tt>:raise</tt> option:</p>
975
- <div class="code_container">
976
- <pre class="brush: ruby; gutter: false; toolbar: false">
977
- I18n.t :foo, :raise =&gt; true # always re-raises exceptions from the backend
978
- </pre>
979
- </div>
980
- <h3 id="conclusion">7 Conclusion</h3>
981
- <p>At this point you should have a good overview about how I18n support in Ruby on Rails works and are ready to start translating your project.</p>
982
- <p>If you find anything missing or wrong in this guide, please file a ticket on our <a href="http://i18n.lighthouseapp.com/projects/14948-rails-i18n/overview">issue tracker</a>. If you want to discuss certain portions or have questions, please sign up to our <a href="http://groups.google.com/group/rails-i18n">mailing list</a>.</p>
983
- <h3 id="contributing-to-rails-i18n">8 Contributing to Rails I18n</h3>
984
- <p>I18n support in Ruby on Rails was introduced in the release 2.2 and is still evolving. The project follows the good Ruby on Rails development tradition of evolving solutions in plugins and real applications first, and only then cherry-picking the best-of-breed of most widely useful features for inclusion in the core.</p>
985
- <p>Thus we encourage everybody to experiment with new ideas and features in plugins or other libraries and make them available to the community. (Don&#8217;t forget to announce your work on our <a href="http://groups.google.com/group/rails-i18n">mailing list</a>!)</p>
986
- <p>If you find your own locale (language) missing from our <a href="https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale">example translations data</a> repository for Ruby on Rails, please <a href="https://github.com/guides/fork-a-project-and-submit-your-modifications"><em>fork</em></a> the repository, add your data and send a <a href="https://github.com/guides/pull-requests">pull request</a>.</p>
987
- <h3 id="resources">9 Resources</h3>
988
- <ul>
989
- <li><a href="http://rails-i18n.org">rails-i18n.org</a> &#8211; Homepage of the rails-i18n project. You can find lots of useful resources on the <a href="http://rails-i18n.org/wiki">wiki</a>.</li>
990
- <li><a href="http://groups.google.com/group/rails-i18n">Google group: rails-i18n</a> &#8211; The project&#8217;s mailing list.</li>
991
- <li><a href="https://github.com/svenfuchs/rails-i18n/tree/master">Github: rails-i18n</a> &#8211; Code repository for the rails-i18n project. Most importantly you can find lots of <a href="https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale">example translations</a> for Rails that should work for your application in most cases.</li>
992
- <li><a href="https://github.com/svenfuchs/i18n/tree/master">Github: i18n</a> &#8211; Code repository for the i18n gem.</li>
993
- <li><a href="http://i18n.lighthouseapp.com/projects/14948-rails-i18n/overview">Lighthouse: rails-i18n</a> &#8211; Issue tracker for the rails-i18n project.</li>
994
- <li><a href="http://i18n.lighthouseapp.com/projects/14947-ruby-i18n/overview">Lighthouse: i18n</a> &#8211; Issue tracker for the i18n gem.</li>
995
- </ul>
996
- <h3 id="authors">10 Authors</h3>
997
- <ul>
998
- <li><a href="http://www.workingwithrails.com/person/9963-sven-fuchs">Sven Fuchs</a> (initial author)</li>
999
- <li><a href="http://www.workingwithrails.com/person/7476-karel-mina-k">Karel Minařík</a></li>
1000
- </ul>
1001
- <p>If you found this guide useful, please consider recommending its authors on <a href="http://www.workingwithrails.com">workingwithrails</a>.</p>
1002
- <h3 id="footnotes">11 Footnotes</h3>
1003
- <p class="footnote" id="fn1"><a href="#fnr1"><sup>1</sup></a> Or, to quote <a href="http://en.wikipedia.org/wiki/Internationalization_and_localization">Wikipedia</a>: <em>&#8220;Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text.&#8221;</em></p>
1004
- <p class="footnote" id="fn2"><a href="#fnr2"><sup>2</sup></a> Other backends might allow or require to use other formats, e.g. a GetText backend might allow to read GetText files.</p>
1005
- <p class="footnote" id="fn3"><a href="#fnr3"><sup>3</sup></a> One of these reasons is that we don&#8217;t want to imply any unnecessary load for applications that do not need any I18n capabilities, so we need to keep the I18n library as simple as possible for English. Another reason is that it is virtually impossible to implement a one-fits-all solution for all problems related to I18n for all existing languages. So a solution that allows us to exchange the entire implementation easily is appropriate anyway. This also makes it much easier to experiment with custom features and extensions.</p>
1006
-
1007
- <h3>Feedback</h3>
1008
- <p>
1009
- You're encouraged to help improve the quality of this guide.
1010
- </p>
1011
- <p>
1012
- If you see any typos or factual errors you are confident to
1013
- patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
1014
- and push the change yourself. That branch of Rails has public write access.
1015
- Commits are still reviewed, but that happens after you've submitted your
1016
- contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
1017
- cross-merged with master periodically.
1018
- </p>
1019
- <p>
1020
- You may also find incomplete content, or stuff that is not up to date.
1021
- Please do add any missing documentation for master. Check the
1022
- <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
1023
- for style and conventions.
1024
- </p>
1025
- <p>
1026
- If for whatever reason you spot something to fix but cannot patch it yourself, please
1027
- <a href="https://github.com/rails/rails/issues">open an issue</a>.
1028
- </p>
1029
- <p>And last but not least, any kind of discussion regarding Ruby on Rails
1030
- documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
1031
- </p>
1032
- </div>
1033
- </div>
1034
- </div>
1035
-
1036
- <hr class="hide" />
1037
- <div id="footer">
1038
- <div class="wrapper">
1039
- <p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
1040
- <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
1041
- </div>
1042
- </div>
1043
-
1044
- <script type="text/javascript" src="javascripts/guides.js"></script>
1045
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
1046
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
1047
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
1048
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
1049
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
1050
- <script type="text/javascript">
1051
- SyntaxHighlighter.all()
1052
- </script>
1053
- </body>
1054
- </html>