railties 3.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (334) hide show
  1. data/CHANGELOG +2201 -0
  2. data/README +286 -0
  3. data/bin/rails +11 -0
  4. data/builtin/rails_info/rails/info.rb +147 -0
  5. data/builtin/rails_info/rails/info_controller.rb +15 -0
  6. data/builtin/rails_info/rails/info_helper.rb +2 -0
  7. data/builtin/routes.rb +3 -0
  8. data/guides/files/javascripts/code_highlighter.js +188 -0
  9. data/guides/files/javascripts/guides.js +7 -0
  10. data/guides/files/javascripts/highlighters.js +90 -0
  11. data/guides/files/stylesheets/main.css +441 -0
  12. data/guides/files/stylesheets/print.css +52 -0
  13. data/guides/files/stylesheets/reset.css +43 -0
  14. data/guides/files/stylesheets/style.css +13 -0
  15. data/guides/files/stylesheets/syntax.css +31 -0
  16. data/guides/images/belongs_to.png +0 -0
  17. data/guides/images/book_icon.gif +0 -0
  18. data/guides/images/bullet.gif +0 -0
  19. data/guides/images/chapters_icon.gif +0 -0
  20. data/guides/images/check_bullet.gif +0 -0
  21. data/guides/images/credits_pic_blank.gif +0 -0
  22. data/guides/images/csrf.png +0 -0
  23. data/guides/images/customized_error_messages.png +0 -0
  24. data/guides/images/error_messages.png +0 -0
  25. data/guides/images/feature_tile.gif +0 -0
  26. data/guides/images/footer_tile.gif +0 -0
  27. data/guides/images/fxn.png +0 -0
  28. data/guides/images/grey_bullet.gif +0 -0
  29. data/guides/images/habtm.png +0 -0
  30. data/guides/images/has_many.png +0 -0
  31. data/guides/images/has_many_through.png +0 -0
  32. data/guides/images/has_one.png +0 -0
  33. data/guides/images/has_one_through.png +0 -0
  34. data/guides/images/header_backdrop.png +0 -0
  35. data/guides/images/header_tile.gif +0 -0
  36. data/guides/images/i18n/demo_localized_pirate.png +0 -0
  37. data/guides/images/i18n/demo_translated_en.png +0 -0
  38. data/guides/images/i18n/demo_translated_pirate.png +0 -0
  39. data/guides/images/i18n/demo_translation_missing.png +0 -0
  40. data/guides/images/i18n/demo_untranslated.png +0 -0
  41. data/guides/images/icons/README +5 -0
  42. data/guides/images/icons/callouts/1.png +0 -0
  43. data/guides/images/icons/callouts/10.png +0 -0
  44. data/guides/images/icons/callouts/11.png +0 -0
  45. data/guides/images/icons/callouts/12.png +0 -0
  46. data/guides/images/icons/callouts/13.png +0 -0
  47. data/guides/images/icons/callouts/14.png +0 -0
  48. data/guides/images/icons/callouts/15.png +0 -0
  49. data/guides/images/icons/callouts/2.png +0 -0
  50. data/guides/images/icons/callouts/3.png +0 -0
  51. data/guides/images/icons/callouts/4.png +0 -0
  52. data/guides/images/icons/callouts/5.png +0 -0
  53. data/guides/images/icons/callouts/6.png +0 -0
  54. data/guides/images/icons/callouts/7.png +0 -0
  55. data/guides/images/icons/callouts/8.png +0 -0
  56. data/guides/images/icons/callouts/9.png +0 -0
  57. data/guides/images/icons/caution.png +0 -0
  58. data/guides/images/icons/example.png +0 -0
  59. data/guides/images/icons/home.png +0 -0
  60. data/guides/images/icons/important.png +0 -0
  61. data/guides/images/icons/next.png +0 -0
  62. data/guides/images/icons/note.png +0 -0
  63. data/guides/images/icons/prev.png +0 -0
  64. data/guides/images/icons/tip.png +0 -0
  65. data/guides/images/icons/up.png +0 -0
  66. data/guides/images/icons/warning.png +0 -0
  67. data/guides/images/nav_arrow.gif +0 -0
  68. data/guides/images/polymorphic.png +0 -0
  69. data/guides/images/posts_index.png +0 -0
  70. data/guides/images/rails_guides_logo.gif +0 -0
  71. data/guides/images/rails_logo_remix.gif +0 -0
  72. data/guides/images/rails_welcome.png +0 -0
  73. data/guides/images/session_fixation.png +0 -0
  74. data/guides/images/tab_grey.gif +0 -0
  75. data/guides/images/tab_info.gif +0 -0
  76. data/guides/images/tab_note.gif +0 -0
  77. data/guides/images/tab_red.gif +0 -0
  78. data/guides/images/tab_yellow.gif +0 -0
  79. data/guides/images/tab_yellow.png +0 -0
  80. data/guides/images/validation_error_messages.png +0 -0
  81. data/guides/rails_guides.rb +44 -0
  82. data/guides/rails_guides/generator.rb +178 -0
  83. data/guides/rails_guides/helpers.rb +34 -0
  84. data/guides/rails_guides/indexer.rb +55 -0
  85. data/guides/rails_guides/levenshtein.rb +31 -0
  86. data/guides/rails_guides/textile_extensions.rb +41 -0
  87. data/guides/source/2_2_release_notes.textile +422 -0
  88. data/guides/source/2_3_release_notes.textile +610 -0
  89. data/guides/source/3_0_release_notes.textile +560 -0
  90. data/guides/source/action_controller_overview.textile +783 -0
  91. data/guides/source/action_mailer_basics.textile +437 -0
  92. data/guides/source/action_view_overview.textile +1481 -0
  93. data/guides/source/active_record_basics.textile +204 -0
  94. data/guides/source/active_record_querying.textile +961 -0
  95. data/guides/source/active_support_core_extensions.textile +1835 -0
  96. data/guides/source/activerecord_validations_callbacks.textile +1127 -0
  97. data/guides/source/ajax_on_rails.textile +342 -0
  98. data/guides/source/association_basics.textile +1816 -0
  99. data/guides/source/caching_with_rails.textile +390 -0
  100. data/guides/source/command_line.textile +589 -0
  101. data/guides/source/configuring.textile +290 -0
  102. data/guides/source/contribute.textile +71 -0
  103. data/guides/source/contributing_to_rails.textile +272 -0
  104. data/guides/source/credits.textile.erb +52 -0
  105. data/guides/source/debugging_rails_applications.textile +709 -0
  106. data/guides/source/form_helpers.textile +768 -0
  107. data/guides/source/generators.textile +378 -0
  108. data/guides/source/getting_started.textile +1310 -0
  109. data/guides/source/i18n.textile +879 -0
  110. data/guides/source/index.textile.erb +124 -0
  111. data/guides/source/layout.html.erb +103 -0
  112. data/guides/source/layouts_and_rendering.textile +979 -0
  113. data/guides/source/migrations.textile +591 -0
  114. data/guides/source/nested_model_forms.textile +222 -0
  115. data/guides/source/performance_testing.textile +531 -0
  116. data/guides/source/plugins.textile +1512 -0
  117. data/guides/source/rails_application_templates.textile +238 -0
  118. data/guides/source/rails_on_rack.textile +306 -0
  119. data/guides/source/routing.textile +903 -0
  120. data/guides/source/security.textile +984 -0
  121. data/guides/source/testing.textile +946 -0
  122. data/lib/generators/erb.rb +8 -0
  123. data/lib/generators/erb/controller/controller_generator.rb +21 -0
  124. data/lib/generators/erb/controller/templates/view.html.erb +2 -0
  125. data/lib/generators/erb/mailer/mailer_generator.rb +20 -0
  126. data/lib/generators/erb/mailer/templates/view.text.erb +3 -0
  127. data/lib/generators/erb/scaffold/scaffold_generator.rb +53 -0
  128. data/lib/generators/erb/scaffold/templates/_form.html.erb +13 -0
  129. data/lib/generators/erb/scaffold/templates/edit.html.erb +6 -0
  130. data/lib/generators/erb/scaffold/templates/index.html.erb +27 -0
  131. data/lib/generators/erb/scaffold/templates/layout.html.erb +16 -0
  132. data/lib/generators/erb/scaffold/templates/new.html.erb +5 -0
  133. data/lib/generators/erb/scaffold/templates/show.html.erb +10 -0
  134. data/lib/generators/rails/app/USAGE +9 -0
  135. data/lib/generators/rails/app/app_generator.rb +262 -0
  136. data/lib/generators/rails/app/templates/Gemfile +34 -0
  137. data/lib/generators/rails/app/templates/README +243 -0
  138. data/lib/generators/rails/app/templates/Rakefile +10 -0
  139. data/lib/generators/rails/app/templates/app/controllers/application_controller.rb +3 -0
  140. data/lib/generators/rails/app/templates/app/helpers/application_helper.rb +2 -0
  141. data/lib/generators/rails/app/templates/app/models/.empty_directory +0 -0
  142. data/lib/generators/rails/app/templates/app/views/layouts/.empty_directory +0 -0
  143. data/lib/generators/rails/app/templates/config.ru +4 -0
  144. data/lib/generators/rails/app/templates/config/application.rb +51 -0
  145. data/lib/generators/rails/app/templates/config/boot.rb +17 -0
  146. data/lib/generators/rails/app/templates/config/databases/frontbase.yml +28 -0
  147. data/lib/generators/rails/app/templates/config/databases/ibm_db.yml +71 -0
  148. data/lib/generators/rails/app/templates/config/databases/mysql.yml +60 -0
  149. data/lib/generators/rails/app/templates/config/databases/oracle.yml +39 -0
  150. data/lib/generators/rails/app/templates/config/databases/postgresql.yml +51 -0
  151. data/lib/generators/rails/app/templates/config/databases/sqlite3.yml +22 -0
  152. data/lib/generators/rails/app/templates/config/environment.rb +5 -0
  153. data/lib/generators/rails/app/templates/config/environments/development.rb.tt +19 -0
  154. data/lib/generators/rails/app/templates/config/environments/production.rb.tt +33 -0
  155. data/lib/generators/rails/app/templates/config/environments/test.rb.tt +29 -0
  156. data/lib/generators/rails/app/templates/config/initializers/backtrace_silencers.rb +7 -0
  157. data/lib/generators/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt +7 -0
  158. data/lib/generators/rails/app/templates/config/initializers/inflections.rb +10 -0
  159. data/lib/generators/rails/app/templates/config/initializers/mime_types.rb +5 -0
  160. data/lib/generators/rails/app/templates/config/initializers/session_store.rb.tt +15 -0
  161. data/lib/generators/rails/app/templates/config/locales/en.yml +5 -0
  162. data/lib/generators/rails/app/templates/config/routes.rb +58 -0
  163. data/lib/generators/rails/app/templates/db/seeds.rb +7 -0
  164. data/lib/generators/rails/app/templates/doc/README_FOR_APP +2 -0
  165. data/lib/generators/rails/app/templates/gitignore +4 -0
  166. data/lib/generators/rails/app/templates/public/404.html +26 -0
  167. data/lib/generators/rails/app/templates/public/422.html +26 -0
  168. data/lib/generators/rails/app/templates/public/500.html +26 -0
  169. data/lib/generators/rails/app/templates/public/favicon.ico +0 -0
  170. data/lib/generators/rails/app/templates/public/images/rails.png +0 -0
  171. data/lib/generators/rails/app/templates/public/index.html +278 -0
  172. data/lib/generators/rails/app/templates/public/javascripts/application.js +2 -0
  173. data/lib/generators/rails/app/templates/public/javascripts/controls.js +963 -0
  174. data/lib/generators/rails/app/templates/public/javascripts/dragdrop.js +973 -0
  175. data/lib/generators/rails/app/templates/public/javascripts/effects.js +1128 -0
  176. data/lib/generators/rails/app/templates/public/javascripts/prototype.js +4320 -0
  177. data/lib/generators/rails/app/templates/public/javascripts/rails.js +110 -0
  178. data/lib/generators/rails/app/templates/public/robots.txt +5 -0
  179. data/lib/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
  180. data/lib/generators/rails/app/templates/script/rails +9 -0
  181. data/lib/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
  182. data/lib/generators/rails/app/templates/test/functional/.empty_directory +0 -0
  183. data/lib/generators/rails/app/templates/test/integration/.empty_directory +0 -0
  184. data/lib/generators/rails/app/templates/test/performance/browsing_test.rb +9 -0
  185. data/lib/generators/rails/app/templates/test/test_helper.rb +13 -0
  186. data/lib/generators/rails/app/templates/test/unit/.empty_directory +0 -0
  187. data/lib/generators/rails/controller/USAGE +18 -0
  188. data/lib/generators/rails/controller/controller_generator.rb +14 -0
  189. data/lib/generators/rails/controller/templates/controller.rb +7 -0
  190. data/lib/generators/rails/generator/USAGE +11 -0
  191. data/lib/generators/rails/generator/generator_generator.rb +25 -0
  192. data/lib/generators/rails/generator/templates/%file_name%_generator.rb.tt +5 -0
  193. data/lib/generators/rails/generator/templates/USAGE.tt +8 -0
  194. data/lib/generators/rails/generator/templates/templates/.empty_directory +0 -0
  195. data/lib/generators/rails/helper/USAGE +17 -0
  196. data/lib/generators/rails/helper/helper_generator.rb +13 -0
  197. data/lib/generators/rails/helper/templates/helper.rb +2 -0
  198. data/lib/generators/rails/integration_test/USAGE +10 -0
  199. data/lib/generators/rails/integration_test/integration_test_generator.rb +7 -0
  200. data/lib/generators/rails/mailer/USAGE +15 -0
  201. data/lib/generators/rails/mailer/mailer_generator.rb +14 -0
  202. data/lib/generators/rails/mailer/templates/mailer.rb +16 -0
  203. data/lib/generators/rails/metal/USAGE +8 -0
  204. data/lib/generators/rails/metal/metal_generator.rb +11 -0
  205. data/lib/generators/rails/metal/templates/metal.rb +12 -0
  206. data/lib/generators/rails/migration/USAGE +29 -0
  207. data/lib/generators/rails/migration/migration_generator.rb +8 -0
  208. data/lib/generators/rails/model/USAGE +30 -0
  209. data/lib/generators/rails/model/model_generator.rb +8 -0
  210. data/lib/generators/rails/model_subclass/model_subclass_generator.rb +12 -0
  211. data/lib/generators/rails/observer/USAGE +12 -0
  212. data/lib/generators/rails/observer/observer_generator.rb +7 -0
  213. data/lib/generators/rails/performance_test/USAGE +10 -0
  214. data/lib/generators/rails/performance_test/performance_test_generator.rb +7 -0
  215. data/lib/generators/rails/plugin/USAGE +13 -0
  216. data/lib/generators/rails/plugin/plugin_generator.rb +47 -0
  217. data/lib/generators/rails/plugin/templates/MIT-LICENSE.tt +20 -0
  218. data/lib/generators/rails/plugin/templates/README.tt +13 -0
  219. data/lib/generators/rails/plugin/templates/Rakefile.tt +10 -0
  220. data/lib/generators/rails/plugin/templates/init.rb +1 -0
  221. data/lib/generators/rails/plugin/templates/install.rb +1 -0
  222. data/lib/generators/rails/plugin/templates/lib/%file_name%.rb.tt +1 -0
  223. data/lib/generators/rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt +4 -0
  224. data/lib/generators/rails/plugin/templates/uninstall.rb +1 -0
  225. data/lib/generators/rails/resource/USAGE +23 -0
  226. data/lib/generators/rails/resource/resource_generator.rb +34 -0
  227. data/lib/generators/rails/scaffold/USAGE +29 -0
  228. data/lib/generators/rails/scaffold/scaffold_generator.rb +13 -0
  229. data/lib/generators/rails/scaffold_controller/USAGE +20 -0
  230. data/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb +27 -0
  231. data/lib/generators/rails/scaffold_controller/templates/controller.rb +85 -0
  232. data/lib/generators/rails/session_migration/USAGE +8 -0
  233. data/lib/generators/rails/session_migration/session_migration_generator.rb +8 -0
  234. data/lib/generators/rails/stylesheets/USAGE +5 -0
  235. data/lib/generators/rails/stylesheets/stylesheets_generator.rb +9 -0
  236. data/lib/generators/rails/stylesheets/templates/scaffold.css +61 -0
  237. data/lib/generators/test_unit.rb +8 -0
  238. data/lib/generators/test_unit/controller/controller_generator.rb +14 -0
  239. data/lib/generators/test_unit/controller/templates/functional_test.rb +8 -0
  240. data/lib/generators/test_unit/helper/helper_generator.rb +13 -0
  241. data/lib/generators/test_unit/helper/templates/helper_test.rb +4 -0
  242. data/lib/generators/test_unit/integration/integration_generator.rb +13 -0
  243. data/lib/generators/test_unit/integration/templates/integration_test.rb +10 -0
  244. data/lib/generators/test_unit/mailer/mailer_generator.rb +21 -0
  245. data/lib/generators/test_unit/mailer/templates/fixture +3 -0
  246. data/lib/generators/test_unit/mailer/templates/functional_test.rb +22 -0
  247. data/lib/generators/test_unit/model/model_generator.rb +24 -0
  248. data/lib/generators/test_unit/model/templates/fixtures.yml +23 -0
  249. data/lib/generators/test_unit/model/templates/unit_test.rb +8 -0
  250. data/lib/generators/test_unit/observer/observer_generator.rb +13 -0
  251. data/lib/generators/test_unit/observer/templates/unit_test.rb +8 -0
  252. data/lib/generators/test_unit/performance/performance_generator.rb +13 -0
  253. data/lib/generators/test_unit/performance/templates/performance_test.rb +9 -0
  254. data/lib/generators/test_unit/plugin/plugin_generator.rb +13 -0
  255. data/lib/generators/test_unit/plugin/templates/%file_name%_test.rb.tt +8 -0
  256. data/lib/generators/test_unit/plugin/templates/test_helper.rb +3 -0
  257. data/lib/generators/test_unit/scaffold/scaffold_generator.rb +18 -0
  258. data/lib/generators/test_unit/scaffold/templates/functional_test.rb +47 -0
  259. data/lib/rails.rb +95 -0
  260. data/lib/rails/all.rb +14 -0
  261. data/lib/rails/application.rb +125 -0
  262. data/lib/rails/application/bootstrap.rb +85 -0
  263. data/lib/rails/application/configurable.rb +19 -0
  264. data/lib/rails/application/configuration.rb +86 -0
  265. data/lib/rails/application/finisher.rb +49 -0
  266. data/lib/rails/application/metal_loader.rb +50 -0
  267. data/lib/rails/application/railties.rb +31 -0
  268. data/lib/rails/application/routes_reloader.rb +46 -0
  269. data/lib/rails/backtrace_cleaner.rb +54 -0
  270. data/lib/rails/code_statistics.rb +107 -0
  271. data/lib/rails/commands.rb +72 -0
  272. data/lib/rails/commands/application.rb +13 -0
  273. data/lib/rails/commands/console.rb +55 -0
  274. data/lib/rails/commands/dbconsole.rb +104 -0
  275. data/lib/rails/commands/destroy.rb +9 -0
  276. data/lib/rails/commands/generate.rb +9 -0
  277. data/lib/rails/commands/performance/benchmarker.rb +23 -0
  278. data/lib/rails/commands/performance/profiler.rb +46 -0
  279. data/lib/rails/commands/plugin.rb +542 -0
  280. data/lib/rails/commands/runner.rb +51 -0
  281. data/lib/rails/commands/server.rb +75 -0
  282. data/lib/rails/commands/update.rb +9 -0
  283. data/lib/rails/configuration.rb +191 -0
  284. data/lib/rails/console/app.rb +33 -0
  285. data/lib/rails/console/helpers.rb +5 -0
  286. data/lib/rails/console/sandbox.rb +6 -0
  287. data/lib/rails/deprecation.rb +62 -0
  288. data/lib/rails/dispatcher.rb +24 -0
  289. data/lib/rails/engine.rb +130 -0
  290. data/lib/rails/engine/configurable.rb +25 -0
  291. data/lib/rails/engine/configuration.rb +49 -0
  292. data/lib/rails/generators.rb +289 -0
  293. data/lib/rails/generators/actions.rb +309 -0
  294. data/lib/rails/generators/active_model.rb +78 -0
  295. data/lib/rails/generators/base.rb +362 -0
  296. data/lib/rails/generators/generated_attribute.rb +48 -0
  297. data/lib/rails/generators/migration.rb +64 -0
  298. data/lib/rails/generators/named_base.rb +95 -0
  299. data/lib/rails/generators/resource_helpers.rb +80 -0
  300. data/lib/rails/generators/test_case.rb +239 -0
  301. data/lib/rails/initializable.rb +97 -0
  302. data/lib/rails/paths.rb +144 -0
  303. data/lib/rails/performance_test_help.rb +3 -0
  304. data/lib/rails/plugin.rb +64 -0
  305. data/lib/rails/rack.rb +8 -0
  306. data/lib/rails/rack/debugger.rb +23 -0
  307. data/lib/rails/rack/log_tailer.rb +33 -0
  308. data/lib/rails/rack/logger.rb +34 -0
  309. data/lib/rails/rack/static.rb +5 -0
  310. data/lib/rails/railtie.rb +74 -0
  311. data/lib/rails/railtie/configurable.rb +23 -0
  312. data/lib/rails/railtie/configuration.rb +9 -0
  313. data/lib/rails/railties_path.rb +1 -0
  314. data/lib/rails/ruby_version_check.rb +10 -0
  315. data/lib/rails/rubyprof_ext.rb +35 -0
  316. data/lib/rails/source_annotation_extractor.rb +102 -0
  317. data/lib/rails/subscriber.rb +108 -0
  318. data/lib/rails/subscriber/test_helper.rb +98 -0
  319. data/lib/rails/tasks.rb +16 -0
  320. data/lib/rails/tasks/annotations.rake +20 -0
  321. data/lib/rails/tasks/documentation.rake +96 -0
  322. data/lib/rails/tasks/framework.rake +69 -0
  323. data/lib/rails/tasks/log.rake +9 -0
  324. data/lib/rails/tasks/middleware.rake +7 -0
  325. data/lib/rails/tasks/misc.rake +71 -0
  326. data/lib/rails/tasks/routes.rake +17 -0
  327. data/lib/rails/tasks/statistics.rake +16 -0
  328. data/lib/rails/tasks/tmp.rake +37 -0
  329. data/lib/rails/test_help.rb +30 -0
  330. data/lib/rails/test_unit/railtie.rb +17 -0
  331. data/lib/rails/test_unit/testing.rake +130 -0
  332. data/lib/rails/version.rb +9 -0
  333. data/lib/rails/webrick_server.rb +156 -0
  334. metadata +427 -0
@@ -0,0 +1,879 @@
1
+ h2. Rails Internationalization (I18n) API
2
+
3
+ The Ruby I18n (shorthand for _internationalization_) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for *translating your application to a single custom language* other than English or for *providing multi-language support* in your application.
4
+
5
+ The process of "internationalization" usually means to abstract all strings and other locale specific bits (such as date or currency formats) out of your application. The process of "localization" means to provide translations and localized formats for these bits. [1]
6
+
7
+ So, in the process of _internationalizing_ your Rails application you have to:
8
+
9
+ * Ensure you have support for i18n
10
+ * Tell Rails where to find locale dictionaries
11
+ * Tell Rails how to set, preserve and switch locale
12
+
13
+ In the process of _localizing_ your application you'll probably want to do following three things:
14
+
15
+ * Replace or supplement Rails' default locale -- e.g. date and time formats, month names, Active Record model names, etc
16
+ * Abstract strings in your application into keyed dictionaries -- e.g. flash messages, static text in your views, etc.
17
+ * Store the resulting dictionaries somewhere
18
+
19
+ This guide will walk you through the I18n API and contains a tutorial how to internationalize a Rails application from the start.
20
+
21
+ endprologue.
22
+
23
+ NOTE: 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 "I18n Wiki":http://rails-i18n.org/wiki for more information.
24
+
25
+ h3. How I18n in Ruby on Rails Works
26
+
27
+ 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 API focuses on:
28
+
29
+ * providing support for English and similar languages out of the box
30
+ * making it easy to customize and extend everything for other languages
31
+
32
+ As part of this solution, *every static string in the Rails framework* -- e.g. Active Record validation messages, time and date formats -- *has been internationalized*, so _localization_ of a Rails application means "over-riding" these defaults.
33
+
34
+ h4. The Overall Architecture of the Library
35
+
36
+ Thus, the Ruby I18n gem is split into two parts:
37
+
38
+ * The public API of the i18n framework -- a Ruby module with public methods that define how the library works
39
+ * A default backend (which is intentionally named _Simple_ backend) that implements these methods
40
+
41
+ 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.
42
+
43
+ NOTE: 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 "Using different backends":#using-different-backends below.
44
+
45
+ h4. The Public I18n API
46
+
47
+ The most important methods of the I18n API are:
48
+
49
+ <ruby>
50
+ translate # Lookup text translations
51
+ localize # Localize Date and Time objects to local formats
52
+ </ruby>
53
+
54
+ These have the aliases #t and #l so you can use them like this:
55
+
56
+ <ruby>
57
+ I18n.t 'store.title'
58
+ I18n.l Time.now
59
+ </ruby>
60
+
61
+ There are also attribute readers and writers for the following attributes:
62
+
63
+ <ruby>
64
+ load_path # Announce your custom translation files
65
+ locale # Get and set the current locale
66
+ default_locale # Get and set the default locale
67
+ exception_handler # Use a different exception_handler
68
+ backend # Use a different backend
69
+ </ruby>
70
+
71
+ So, let's internationalize a simple Rails application from the ground up in the next chapters!
72
+
73
+ h3. Setup the Rails Application for Internationalization
74
+
75
+ There are just a few simple steps to get up and running with I18n support for your application.
76
+
77
+ h4. Configure the I18n Module
78
+
79
+ Following the _convention over configuration_ philosophy, Rails will set up your application with reasonable defaults. If you need different settings, you can overwrite them easily.
80
+
81
+ Rails adds all +.rb+ and +.yml+ files from the +config/locales+ directory to your *translations load path*, automatically.
82
+
83
+ The default +en.yml+ locale in this directory contains a sample pair of translation strings:
84
+
85
+ <ruby>
86
+ en:
87
+ hello: "Hello world"
88
+ </ruby>
89
+
90
+ This means, that in the +:en+ locale, the key _hello_ will map to the _Hello world_ string. Every string inside Rails is internationalized in this way, see for instance Active Record validation messages in the "+activerecord/lib/active_record/locale/en.yml+":http://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml file or time and date formats in the "+activesupport/lib/active_support/locale/en.yml+":http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml file. You can use YAML or standard Ruby Hashes to store translations in the default (Simple) backend.
91
+
92
+ The I18n library will use *English* as a *default locale*, i.e. if you don't set a different locale, +:en+ will be used for looking up translations.
93
+
94
+ NOTE: The i18n library takes a *pragmatic approach* to locale keys (after "some discussion":http://groups.google.com/group/rails-i18n/browse_thread/thread/14dede2c7dbe9470/80eec34395f64f3c?hl=en), including only the _locale_ ("language") part, like +:en+, +:pl+, not the _region_ part, like +:en-US+ or +:en-UK+, which are traditionally used for separating "languages" and "regional setting" or "dialects". Many international applications use only the "language" element of a locale such as +:cz+, +:th+ or +:es+ (for Czech, Thai and Spanish). However, there are also regional differences within different language groups that may be important. For instance, in the +:en-US+ locale you would have $ as a currency symbol, while in +:en-UK+, you would have £. Nothing stops you from separating regional and other settings in this way: you just have to provide full "English - United Kingdom" locale in a +:en-UK+ dictionary. Various "Rails I18n plugins":http://rails-i18n.org/wiki such as "Globalize2":http://github.com/joshmh/globalize2/tree/master may help you implement it.
95
+
96
+ The *translations load path* (+I18n.load_path+) 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.
97
+
98
+ 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.
99
+
100
+ 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.
101
+
102
+ <ruby>
103
+ # The internationalization framework can be changed
104
+ # to have another default locale (standard is :en) or more load paths.
105
+ # All files from config/locales/*.rb,yml are added automatically.
106
+ # config.i18n.load_path << Dir[File.join(RAILS_ROOT, 'my', 'locales', '*.{rb,yml}')]
107
+ # config.i18n.default_locale = :de
108
+ </ruby>
109
+
110
+ h4. Optional: Custom I18n Configuration Setup
111
+
112
+ 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.
113
+
114
+ 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:
115
+
116
+ <ruby>
117
+ # in config/initializers/locale.rb
118
+
119
+ # tell the I18n library where to find your translations
120
+ I18n.load_path << Dir[ File.join(RAILS_ROOT, 'lib', 'locale',
121
+ '*.{rb,yml}') ]
122
+
123
+ # set default locale to something other than :en
124
+ I18n.default_locale = :pt
125
+ </ruby>
126
+
127
+ h4. Setting and Passing the Locale
128
+
129
+ 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.
130
+
131
+ 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.
132
+
133
+ WARNING: You may be tempted to store the chosen locale in a _session_ or a _cookie_. *Do not do so*. The locale should be transparent and a part of the URL. This way you don't break people's basic assumptions about the web itself: if you send a URL of some page to a friend, she should see the same page, same content. A fancy word for this would be that you're being "_RESTful_":http://en.wikipedia.org/wiki/Representational_State_Transfer. Read more about the RESTful approach in "Stefan Tilkov's articles":http://www.infoq.com/articles/rest-introduction. There may be some exceptions to this rule, which are discussed below.
134
+
135
+ The _setting part_ is easy. You can set the locale in a +before_filter+ in the ApplicationController like this:
136
+
137
+ <ruby>
138
+ before_filter :set_locale
139
+ def set_locale
140
+ # if params[:locale] is nil then I18n.default_locale will be used
141
+ I18n.locale = params[:locale]
142
+ end
143
+ </ruby>
144
+
145
+ This requires you to pass the locale as a URL query parameter as in +http://example.com/books?locale=pt+. (This is, for example, Google's approach.) So +http://localhost:3000?locale=pt+ will load the Portuguese localization, whereas +http://localhost:3000?locale=de+ would load the German localization, and so on. You may skip the next section and head over to the *Internationalize your application* section, if you want to try things out by manually placing the locale in the URL and reloading the page.
146
+
147
+ Of course, you probably don't want to manually include the locale in every URL all over your application, or want the URLs look differently, e.g. the usual +http://example.com/pt/books+ versus +http://example.com/en/books+. Let's discuss the different options you have.
148
+
149
+ h4. Setting the Locale from the Domain Name
150
+
151
+ One option you have is to set the locale from the domain name where your application runs. For example, we want +www.example.com+ to load the English (or default) locale, and +www.example.es+ to load the Spanish locale. Thus the _top-level domain name_ is used for locale setting. This has several advantages:
152
+
153
+ * The locale is an _obvious_ part of the URL.
154
+ * People intuitively grasp in which language the content will be displayed.
155
+ * It is very trivial to implement in Rails.
156
+ * Search engines seem to like that content in different languages lives at different, inter-linked domains.
157
+
158
+ You can implement it like this in your +ApplicationController+:
159
+
160
+ <ruby>
161
+ before_filter :set_locale
162
+
163
+ def set_locale
164
+ I18n.locale = extract_locale_from_uri
165
+ end
166
+
167
+ # Get locale from top-level domain or return nil if such locale is not available
168
+ # You have to put something like:
169
+ # 127.0.0.1 application.com
170
+ # 127.0.0.1 application.it
171
+ # 127.0.0.1 application.pl
172
+ # in your /etc/hosts file to try this out locally
173
+ def extract_locale_from_tld
174
+ parsed_locale = request.host.split('.').last
175
+ I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil
176
+ end
177
+ </ruby>
178
+
179
+ We can also set the locale from the _subdomain_ in a very similar way:
180
+
181
+ <ruby>
182
+ # Get locale code from request subdomain (like http://it.application.local:3000)
183
+ # You have to put something like:
184
+ # 127.0.0.1 gr.application.local
185
+ # in your /etc/hosts file to try this out locally
186
+ def extract_locale_from_subdomain
187
+ parsed_locale = request.subdomains.first
188
+ I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil
189
+ end
190
+ </ruby>
191
+
192
+ If your application includes a locale switching menu, you would then have something like this in it:
193
+
194
+ <ruby>
195
+ link_to("Deutsch", "#{APP_CONFIG[:deutsch_website_url]}#{request.env['REQUEST_URI']}")
196
+ </ruby>
197
+
198
+ assuming you would set +APP_CONFIG[:deutsch_website_url]+ to some value like +http://www.application.de+.
199
+
200
+ This solution has aforementioned advantages, however, you may not be able or may not want to provide different localizations ("language versions") on different domains. The most obvious solution would be to include locale code in the URL params (or request path).
201
+
202
+ h4. Setting the Locale from the URL Params
203
+
204
+ The most usual way of setting (and passing) the locale would be to include it in URL params, as we did in the +I18n.locale = params[:locale]+ _before_filter_ in the first example. We would like to have URLs like +www.example.com/books?locale=ja+ or +www.example.com/ja/books+ in this case.
205
+
206
+ This approach has almost the same set of advantages as setting the locale from the domain name: namely that it's RESTful and in accord with the rest of the World Wide Web. It does require a little bit more work to implement, though.
207
+
208
+ Getting the locale from +params+ and setting it accordingly is not hard; including it in every URL and thus *passing it through the requests* is. To include an explicit option in every URL (e.g. +link_to( books_url(:locale => I18n.locale))+) would be tedious and probably impossible, of course.
209
+
210
+ Rails contains infrastructure for "centralizing dynamic decisions about the URLs" in its "+ApplicationController#default_url_options+":http://api.rubyonrails.org/classes/ActionController/Base.html#M000515, which is useful precisely in this scenario: it enables us to set "defaults" for "+url_for+":http://api.rubyonrails.org/classes/ActionController/Base.html#M000503 and helper methods dependent on it (by implementing/overriding this method).
211
+
212
+ We can include something like this in our ApplicationController then:
213
+
214
+ <ruby>
215
+ # app/controllers/application_controller.rb
216
+ def default_url_options(options={})
217
+ logger.debug "default_url_options is passed options: #{options.inspect}\n"
218
+ { :locale => I18n.locale }
219
+ end
220
+ </ruby>
221
+
222
+ Every helper method dependent on +url_for+ (e.g. helpers for named routes like +root_path+ or +root_url+, resource routes like +books_path+ or +books_url+, etc.) will now *automatically include the locale in the query string*, like this: +http://localhost:3001/?locale=ja+.
223
+
224
+ You may be satisfied with this. It does impact the readability of URLs, though, when the locale "hangs" at the end of every URL 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.
225
+
226
+ You probably want URLs to look like this: +www.example.com/en/books+ (which loads the English locale) and +www.example.com/nl/books+ (which loads the Netherlands locale). This is achievable with the "over-riding +default_url_options+" strategy from above: you just have to set up your routes with "+path_prefix+":http://api.rubyonrails.org/classes/ActionController/Resources.html#M000354 option in this way:
227
+
228
+ <ruby>
229
+ # config/routes.rb
230
+ map.resources :books, :path_prefix => '/:locale'
231
+ </ruby>
232
+
233
+ 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).
234
+
235
+ 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.)
236
+
237
+ You would probably need to map URLs like these:
238
+
239
+ <ruby>
240
+ # config/routes.rb
241
+ map.dashboard '/:locale', :controller => "dashboard"
242
+ </ruby>
243
+
244
+ 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.)
245
+
246
+ 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.
247
+
248
+ h4. Setting the Locale from the Client Supplied Information
249
+
250
+ In specific cases, it would make sense to set the locale from client-supplied information, i.e. not from the URL. This information may come for example from the users' preferred language (set in their browser), can be based on the users' 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 -- see the box about _sessions_, _cookies_ and RESTful architecture above.
251
+
252
+
253
+ h5. Using +Accept-Language+
254
+
255
+ One source of client supplied information would be an +Accept-Language+ HTTP header. People may "set this in their browser":http://www.w3.org/International/questions/qa-lang-priorities or other clients (such as _curl_).
256
+
257
+ A trivial implementation of using an +Accept-Language+ header would be:
258
+
259
+ <ruby>
260
+ def set_locale
261
+ logger.debug "* Accept-Language: #{request.env['HTTP_ACCEPT_LANGUAGE']}"
262
+ I18n.locale = extract_locale_from_accept_language_header
263
+ logger.debug "* Locale set to '#{I18n.locale}'"
264
+ end
265
+ private
266
+ def extract_locale_from_accept_language_header
267
+ request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
268
+ end
269
+ </ruby>
270
+
271
+ Of course, in a production environment you would need much more robust code, and could use a plugin such as Iain Hecker's "http_accept_language":http://github.com/iain/http_accept_language/tree/master or even Rack middleware such as Ryan Tomayko's "locale":http://github.com/rtomayko/rack-contrib/blob/master/lib/rack/locale.rb.
272
+
273
+ h5. Using GeoIP (or Similar) Database
274
+
275
+ 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 "GeoIP Lite Country":http://www.maxmind.com/app/geolitecountry. The mechanics of the code would be very similar to the code above -- you would need to query the database for the user's IP, and look up your preferred locale for the country/region/city returned.
276
+
277
+ h5. User Profile
278
+
279
+ 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 -- you'd probably let users choose a locale from a dropdown list and save it to their profile in the database. Then you'd set the locale to this value.
280
+
281
+ h3. Internationalizing your Application
282
+
283
+ OK! Now you'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're now ready for the really interesting stuff.
284
+
285
+ Let's _internationalize_ our application, i.e. abstract every locale-specific parts, and then _localize_ it, i.e. provide necessary translations for these abstracts.
286
+
287
+ You most probably have something like this in one of your applications:
288
+
289
+ <ruby>
290
+ # config/routes.rb
291
+ ActionController::Routing::Routes.draw do |map|
292
+ map.root :controller => 'home', :action => 'index'
293
+ end
294
+
295
+ # app/controllers/home_controller.rb
296
+ class HomeController < ApplicationController
297
+ def index
298
+ flash[:notice] = "Hello flash!"
299
+ end
300
+ end
301
+
302
+ # app/views/home/index.html.erb
303
+ <h1>Hello world!</h1>
304
+ <p><%= flash[:notice] %></p>
305
+ </ruby>
306
+
307
+ !images/i18n/demo_untranslated.png(rails i18n demo untranslated)!
308
+
309
+ h4. Adding Translations
310
+
311
+ Obviously there are *two strings that are localized to English*. In order to internationalize this code, *replace these strings* with calls to Rails' +#t+ helper with a key that makes sense for the translation:
312
+
313
+ <ruby>
314
+ # app/controllers/home_controller.rb
315
+ class HomeController < ApplicationController
316
+ def index
317
+ flash[:notice] = t(:hello_flash)
318
+ end
319
+ end
320
+
321
+ # app/views/home/index.html.erb
322
+ <h1><%=t :hello_world %></h1>
323
+ <p><%= flash[:notice] %></p>
324
+ </ruby>
325
+
326
+ When you now render this view, it will show an error message which tells you that the translations for the keys +:hello_world+ and +:hello_flash+ are missing.
327
+
328
+ !images/i18n/demo_translation_missing.png(rails i18n demo translation missing)!
329
+
330
+ NOTE: Rails adds a +t+ (+translate+) helper method to your views so that you do not need to spell out +I18n.t+ all the time. Additionally this helper will catch missing translations and wrap the resulting error message into a +&lt;span class="translation_missing"&gt;+.
331
+
332
+ So let's add the missing translations into the dictionary files (i.e. do the "localization" part):
333
+
334
+ <ruby>
335
+ # config/locales/en.yml
336
+ en:
337
+ hello_world: Hello World
338
+ hello_flash: Hello Flash
339
+
340
+ # config/locales/pirate.yml
341
+ pirate:
342
+ hello_world: Ahoy World
343
+ hello_flash: Ahoy Flash
344
+ </ruby>
345
+
346
+ There you go. Because you haven't changed the default_locale, I18n will use English. Your application now shows:
347
+
348
+ !images/i18n/demo_translated_en.png(rails i18n demo translated to English)!
349
+
350
+ And when you change the URL to pass the pirate locale (+http://localhost:3000?locale=pirate+), you'll get:
351
+
352
+ !images/i18n/demo_translated_pirate.png(rails i18n demo translated to pirate)!
353
+
354
+ NOTE: You need to restart the server when you add new locale files.
355
+
356
+ You may use YAML (+.yml+) or plain Ruby (+.rb+) files for storing your translations in SimpleStore. YAML is the preferred option among Rails developers. However, it has one big disadvantage. YAML 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's wrong. (If you encounter any "weird issues" with YAML dictionaries, try putting the relevant portion of your dictionary into a Ruby file.)
357
+
358
+ h4. Adding Date/Time Formats
359
+
360
+ OK! Now let's add a timestamp to the view, so we can demo the *date/time localization* feature as well. To localize the time format you pass the Time object to +I18n.l+ or (preferably) use Rails' +#l+ helper. You can pick a format by passing the +:format+ option -- by default the +:default+ format is used.
361
+
362
+ <ruby>
363
+ # app/views/home/index.html.erb
364
+ <h1><%=t :hello_world %></h1>
365
+ <p><%= flash[:notice] %></p
366
+ <p><%= l Time.now, :format => :short %></p>
367
+ </ruby>
368
+
369
+ And in our pirate translations file let's add a time format (it's already there in Rails' defaults for English):
370
+
371
+ <ruby>
372
+ # config/locales/pirate.yml
373
+ pirate:
374
+ time:
375
+ formats:
376
+ short: "arrrround %H'ish"
377
+ </ruby>
378
+
379
+ So that would give you:
380
+
381
+ !images/i18n/demo_localized_pirate.png(rails i18n demo localized time to pirate)!
382
+
383
+ TIP: 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 'pirate' locale). Of course, there's a great chance that somebody already did all the work by *translating Rails' defaults for your locale*. See the "rails-i18n repository at Github":http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale for an archive of various locale files. When you put such file(s) in +config/locales/+ directory, they will automatically be ready for use.
384
+
385
+ h4. Localized Views
386
+
387
+ Rails 2.3 introduces another convenient localization feature: localized views (templates). Let's say you have a _BooksController_ in your application. Your _index_ action renders content in +app/views/books/index.html.erb+ template. When you put a _localized variant_ of this template: *+index.es.html.erb+* in the same directory, Rails will render content in this template, when the locale is set to +:es+. When the locale is set to the default locale, the generic +index.html.erb+ view will be used. (Future Rails versions may well bring this _automagic_ localization to assets in +public+, etc.)
388
+
389
+ 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 YAML 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.
390
+
391
+ h4. Organization of Locale Files
392
+
393
+ 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.
394
+
395
+ For example, your +config/locales+ directory could look like this:
396
+
397
+ <pre>
398
+ |-defaults
399
+ |---es.rb
400
+ |---en.rb
401
+ |-models
402
+ |---book
403
+ |-----es.rb
404
+ |-----en.rb
405
+ |-views
406
+ |---defaults
407
+ |-----es.rb
408
+ |-----en.rb
409
+ |---books
410
+ |-----es.rb
411
+ |-----en.rb
412
+ |---users
413
+ |-----es.rb
414
+ |-----en.rb
415
+ |---navigation
416
+ |-----es.rb
417
+ |-----en.rb
418
+ </pre>
419
+
420
+ This way, you can separate model and model attribute names from text inside views, and all of this from the "defaults" (e.g. date and time formats). Other stores for the i18n library could provide different means of such separation.
421
+
422
+ 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:
423
+
424
+ <ruby>
425
+ # config/environment.rb
426
+ config.i18n.load_path += Dir[File.join(RAILS_ROOT, 'config', 'locales', '**', '*.{rb,yml}')]
427
+ </ruby>
428
+
429
+ Do check the "Rails i18n Wiki":http://rails-i18n.org/wiki for list of tools available for managing translations.
430
+
431
+ h3. Overview of the I18n API Features
432
+
433
+ 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'll cover it's features in more depth.
434
+
435
+ Covered are features like these:
436
+
437
+ * looking up translations
438
+ * interpolating data into translations
439
+ * pluralizing translations
440
+ * localizing dates, numbers, currency, etc.
441
+
442
+ h4. Looking up Translations
443
+
444
+ h5. Basic Lookup, Scopes and Nested Keys
445
+
446
+ Translations are looked up by keys which can be both Symbols or Strings, so these calls are equivalent:
447
+
448
+ <ruby>
449
+ I18n.t :message
450
+ I18n.t 'message'
451
+ </ruby>
452
+
453
+ The +translate+ method also takes a +:scope+ option which can contain one or more additional keys that will be used to specify a “namespace” or scope for a translation key:
454
+
455
+ <ruby>
456
+ I18n.t :invalid, :scope => [:activerecord, :errors, :messages]
457
+ </ruby>
458
+
459
+ This looks up the +:invalid+ message in the Active Record error messages.
460
+
461
+ Additionally, both the key and scopes can be specified as dot-separated keys as in:
462
+
463
+ <ruby>
464
+ I18n.translate :"activerecord.errors.messages.invalid"
465
+ </ruby>
466
+
467
+ Thus the following calls are equivalent:
468
+
469
+ <ruby>
470
+ I18n.t 'activerecord.errors.messages.invalid'
471
+ I18n.t 'errors.messages.invalid', :scope => :active_record
472
+ I18n.t :invalid, :scope => 'activerecord.errors.messages'
473
+ I18n.t :invalid, :scope => [:activerecord, :errors, :messages]
474
+ </ruby>
475
+
476
+ h5. Defaults
477
+
478
+ When a +:default+ option is given, its value will be returned if the translation is missing:
479
+
480
+ <ruby>
481
+ I18n.t :missing, :default => 'Not here'
482
+ # => 'Not here'
483
+ </ruby>
484
+
485
+ If the +:default+ 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.
486
+
487
+ E.g., the following first tries to translate the key +:missing+ and then the key +:also_missing.+ As both do not yield a result, the string "Not here" will be returned:
488
+
489
+ <ruby>
490
+ I18n.t :missing, :default => [:also_missing, 'Not here']
491
+ # => 'Not here'
492
+ </ruby>
493
+
494
+ h5. Bulk and Namespace Lookup
495
+
496
+ To look up multiple translations at once, an array of keys can be passed:
497
+
498
+ <ruby>
499
+ I18n.t [:odd, :even], :scope => 'activerecord.errors.messages'
500
+ # => ["must be odd", "must be even"]
501
+ </ruby>
502
+
503
+ Also, a key can translate to a (potentially nested) hash of grouped translations. E.g., one can receive _all_ Active Record error messages as a Hash with:
504
+
505
+ <ruby>
506
+ I18n.t 'activerecord.errors.messages'
507
+ # => { :inclusion => "is not included in the list", :exclusion => ... }
508
+ </ruby>
509
+
510
+ h5. "Lazy" Lookup
511
+
512
+ Rails 2.3 implements a convenient way to look up the locale inside _views_. When you have the following dictionary:
513
+
514
+ <yaml>
515
+ es:
516
+ books:
517
+ index:
518
+ title: "Título"
519
+ </yaml>
520
+
521
+ you can look up the +books.index.title+ value *inside* +app/views/books/index.html.erb+ template like this (note the dot):
522
+
523
+ <ruby>
524
+ <%= t '.title' %>
525
+ </ruby>
526
+
527
+ h4. Interpolation
528
+
529
+ In many cases you want to abstract your translations so that *variables can be interpolated into the translation*. For this reason the I18n API provides an interpolation feature.
530
+
531
+ All options besides +:default+ and +:scope+ that are passed to +#translate+ will be interpolated to the translation:
532
+
533
+ <ruby>
534
+ I18n.backend.store_translations :en, :thanks => 'Thanks {{name}}!'
535
+ I18n.translate :thanks, :name => 'Jeremy'
536
+ # => 'Thanks Jeremy!'
537
+ </ruby>
538
+
539
+ If a translation uses +:default+ or +:scope+ as an interpolation variable, an I+18n::ReservedInterpolationKey+ exception is raised. If a translation expects an interpolation variable, but this has not been passed to +#translate+, an +I18n::MissingInterpolationArgument+ exception is raised.
540
+
541
+ h4. Pluralization
542
+
543
+ In English there are only one singular and one plural form for a given string, e.g. "1 message" and "2 messages". Other languages ("Arabic":http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ar, "Japanese":http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ja, "Russian":http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ru and many more) have different grammars that have additional or fewer "plural forms":http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html. Thus, the I18n API provides a flexible pluralization feature.
544
+
545
+ The +:count+ 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 CLDR:
546
+
547
+ <ruby>
548
+ I18n.backend.store_translations :en, :inbox => {
549
+ :one => '1 message',
550
+ :other => '{{count}} messages'
551
+ }
552
+ I18n.translate :inbox, :count => 2
553
+ # => '2 messages'
554
+ </ruby>
555
+
556
+ The algorithm for pluralizations in +:en+ is as simple as:
557
+
558
+ <ruby>
559
+ entry[count == 1 ? 0 : 1]
560
+ </ruby>
561
+
562
+ I.e. the translation denoted as +:one+ is regarded as singular, the other is used as plural (including the count being zero).
563
+
564
+ If the lookup for the key does not return a Hash suitable for pluralization, an +18n::InvalidPluralizationData+ exception is raised.
565
+
566
+ h4. Setting and Passing a Locale
567
+
568
+ The locale can be either set pseudo-globally to +I18n.locale+ (which uses +Thread.current+ like, e.g., +Time.zone+) or can be passed as an option to +#translate+ and +#localize+.
569
+
570
+ If no locale is passed, +I18n.locale+ is used:
571
+
572
+ <ruby>
573
+ I18n.locale = :de
574
+ I18n.t :foo
575
+ I18n.l Time.now
576
+ </ruby>
577
+
578
+ Explicitely passing a locale:
579
+
580
+ <ruby>
581
+ I18n.t :foo, :locale => :de
582
+ I18n.l Time.now, :locale => :de
583
+ </ruby>
584
+
585
+ The +I18n.locale+ defaults to +I18n.default_locale+ which defaults to :+en+. The default locale can be set like this:
586
+
587
+ <ruby>
588
+ I18n.default_locale = :de
589
+ </ruby>
590
+
591
+ h3. How to Store your Custom Translations
592
+
593
+ The Simple backend shipped with Active Support allows you to store translations in both plain Ruby and YAML format. [2]
594
+
595
+ For example a Ruby Hash providing translations can look like this:
596
+
597
+ <ruby>
598
+ {
599
+ :pt => {
600
+ :foo => {
601
+ :bar => "baz"
602
+ }
603
+ }
604
+ }
605
+ </ruby>
606
+
607
+ The equivalent YAML file would look like this:
608
+
609
+ <ruby>
610
+ pt:
611
+ foo:
612
+ bar: baz
613
+ </ruby>
614
+
615
+ As you see, in both cases the toplevel key is the locale. +:foo+ is a namespace key and +:bar+ is the key for the translation "baz".
616
+
617
+ Here is a "real" example from the Active Support +en.yml+ translations YAML file:
618
+
619
+ <ruby>
620
+ en:
621
+ date:
622
+ formats:
623
+ default: "%Y-%m-%d"
624
+ short: "%b %d"
625
+ long: "%B %d, %Y"
626
+ </ruby>
627
+
628
+ So, all of the following equivalent lookups will return the +:short+ date format +"%B %d"+:
629
+
630
+ <ruby>
631
+ I18n.t 'date.formats.short'
632
+ I18n.t 'formats.short', :scope => :date
633
+ I18n.t :short, :scope => 'date.formats'
634
+ I18n.t :short, :scope => [:date, :formats]
635
+ </ruby>
636
+
637
+ Generally we recommend using YAML 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.
638
+
639
+ h4. Translations for Active Record Models
640
+
641
+ You can use the methods +Model.human_name+ and +Model.human_attribute_name(attribute)+ to transparently look up translations for your model and attribute names.
642
+
643
+ For example when you add the following translations:
644
+
645
+ <ruby>
646
+ en:
647
+ activerecord:
648
+ models:
649
+ user: Dude
650
+ attributes:
651
+ user:
652
+ login: "Handle"
653
+ # will translate User attribute "login" as "Handle"
654
+ </ruby>
655
+
656
+ Then +User.human_name+ will return "Dude" and +User.human_attribute_name("login")+ will return "Handle".
657
+
658
+ h5. Error Message Scopes
659
+
660
+ 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.
661
+
662
+ This gives you quite powerful means to flexibly adjust your messages to your application's needs.
663
+
664
+ Consider a User model with a +validates_presence_of+ validation for the name attribute like this:
665
+
666
+ <ruby>
667
+ class User < ActiveRecord::Base
668
+ validates_presence_of :name
669
+ end
670
+ </ruby>
671
+
672
+ The key for the error message in this case is +:blank+. Active Record will look up this key in the namespaces:
673
+
674
+ <ruby>
675
+ activerecord.errors.models.[model_name].attributes.[attribute_name]
676
+ activerecord.errors.models.[model_name]
677
+ activerecord.errors.messages
678
+ </ruby>
679
+
680
+ Thus, in our example it will try the following keys in this order and return the first result:
681
+
682
+ <ruby>
683
+ activerecord.errors.models.user.attributes.name.blank
684
+ activerecord.errors.models.user.blank
685
+ activerecord.errors.messages.blank
686
+ </ruby>
687
+
688
+ When your models are additionally using inheritance then the messages are looked up in the inheritance chain.
689
+
690
+ For example, you might have an Admin model inheriting from User:
691
+
692
+ <ruby>
693
+ class Admin < User
694
+ validates_presence_of :name
695
+ end
696
+ </ruby>
697
+
698
+ Then Active Record will look for messages in this order:
699
+
700
+ <ruby>
701
+ activerecord.errors.models.admin.attributes.title.blank
702
+ activerecord.errors.models.admin.blank
703
+ activerecord.errors.models.user.attributes.title.blank
704
+ activerecord.errors.models.user.blank
705
+ activerecord.errors.messages.blank
706
+ </ruby>
707
+
708
+ 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.
709
+
710
+ h5. Error Message Interpolation
711
+
712
+ The translated model name, translated attribute name, and value are always available for interpolation.
713
+
714
+ So, for example, instead of the default error message +"can not be blank"+ you could use the attribute name like this : +"Please fill in your {{attribute}}"+.
715
+
716
+ * +count+, where available, can be used for pluralization if present:
717
+
718
+ |_. validation |_.with option |_.message |_.interpolation|
719
+ | validates_confirmation_of | - | :confirmation | -|
720
+ | validates_acceptance_of | - | :accepted | -|
721
+ | validates_presence_of | - | :blank | -|
722
+ | validates_length_of | :within, :in | :too_short | count|
723
+ | validates_length_of | :within, :in | :too_long | count|
724
+ | validates_length_of | :is | :wrong_length | count|
725
+ | validates_length_of | :minimum | :too_short | count|
726
+ | validates_length_of | :maximum | :too_long | count|
727
+ | validates_uniqueness_of | - | :taken | -|
728
+ | validates_format_of | - | :invalid | -|
729
+ | validates_inclusion_of | - | :inclusion | -|
730
+ | validates_exclusion_of | - | :exclusion | -|
731
+ | validates_associated | - | :invalid | -|
732
+ | validates_numericality_of | - | :not_a_number | -|
733
+ | validates_numericality_of | :greater_than | :greater_than | count|
734
+ | validates_numericality_of | :greater_than_or_equal_to | :greater_than_or_equal_to | count|
735
+ | validates_numericality_of | :equal_to | :equal_to | count|
736
+ | validates_numericality_of | :less_than | :less_than | count|
737
+ | validates_numericality_of | :less_than_or_equal_to | :less_than_or_equal_to | count|
738
+ | validates_numericality_of | :odd | :odd | -|
739
+ | validates_numericality_of | :even | :even | -|
740
+
741
+ h5. Translations for the Active Record +error_messages_for+ Helper
742
+
743
+ If you are using the Active Record +error_messages_for+ helper, you will want to add translations for it.
744
+
745
+ Rails ships with the following translations:
746
+
747
+ <yaml>
748
+ en:
749
+ activerecord:
750
+ errors:
751
+ template:
752
+ header:
753
+ one: "1 error prohibited this {{model}} from being saved"
754
+ other: "{{count}} errors prohibited this {{model}} from being saved"
755
+ body: "There were problems with the following fields:"
756
+ </yaml>
757
+
758
+ h4. Overview of Other Built-In Methods that Provide I18n Support
759
+
760
+ Rails uses fixed strings and other localizations, such as format strings and other format information in a couple of helpers. Here's a brief overview.
761
+
762
+ h5. Action View Helper Methods
763
+
764
+ * +distance_of_time_in_words+ translates and pluralizes its result and interpolates the number of seconds, minutes, hours, and so on. See "datetime.distance_in_words":http://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L51 translations.
765
+
766
+ * +datetime_select+ and +select_month+ use translated month names for populating the resulting select tag. See "date.month_names":http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L15 for translations. +datetime_select+ also looks up the order option from "date.order":http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L18 (unless you pass the option explicitely). All date selection helpers translate the prompt using the translations in the "datetime.prompts":http://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L83 scope if applicable.
767
+
768
+ * The +number_to_currency+, +number_with_precision+, +number_to_percentage+, +number_with_delimiter+, and +number_to_human_size+ helpers use the number format settings located in the "number":http://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L2 scope.
769
+
770
+ h5. Active Record Methods
771
+
772
+ * +human_name+ and +human_attribute_name+ use translations for model names and attribute names if available in the "activerecord.models":http://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml#L43 scope. They also support translations for inherited class names (e.g. for use with STI) as explained above in "Error message scopes".
773
+
774
+ * +ActiveRecord::Errors#generate_message+ (which is used by Active Record validations but may also be used manually) uses +human_name+ and +human_attribute_name+ (see above). It also translates the error message and supports translations for inherited class names as explained above in "Error message scopes".
775
+
776
+ *+ ActiveRecord::Errors#full_messages+ prepends the attribute name to the error message using a separator that will be looked up from "activerecord.errors.format.separator":http://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L91 (and which defaults to +'&nbsp;'+).
777
+
778
+ h5. Active Support Methods
779
+
780
+ * +Array#to_sentence+ uses format settings as given in the "support.array":http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L30 scope.
781
+
782
+
783
+ h3. Customize your I18n Setup
784
+
785
+ h4. Using Different Backends
786
+
787
+ For several reasons the Simple backend shipped with Active Support only does the "simplest thing that could possibly work" _for Ruby on Rails_ [3] ... 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.
788
+
789
+ That does not mean you'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's Static backend:
790
+
791
+ <ruby>
792
+ I18n.backend = Globalize::Backend::Static.new
793
+ </ruby>
794
+
795
+ h4. Using Different Exception Handlers
796
+
797
+ The I18n API defines the following exceptions that will be raised by backends when the corresponding unexpected conditions occur:
798
+
799
+ <ruby>
800
+ MissingTranslationData # no translation was found for the requested key
801
+ InvalidLocale # the locale set to I18n.locale is invalid (e.g. nil)
802
+ InvalidPluralizationData # a count option was passed but the translation data is not suitable for pluralization
803
+ MissingInterpolationArgument # the translation expects an interpolation argument that has not been passed
804
+ ReservedInterpolationKey # the translation contains a reserved interpolation variable name (i.e. one of: scope, default)
805
+ UnknownFileType # the backend does not know how to handle a file type that was added to I18n.load_path
806
+ </ruby>
807
+
808
+ The I18n API 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 +MissingTranslationData+ exceptions. When a +MissingTranslationData+ exception has been caught, it will return the exception’s error message string containing the missing key/scope.
809
+
810
+ The reason for this is that during development you'd usually want your views to still render even though a translation is missing.
811
+
812
+ 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:
813
+
814
+ <ruby>
815
+ module I18n
816
+ def just_raise_that_exception(*args)
817
+ raise args.first
818
+ end
819
+ end
820
+
821
+ I18n.exception_handler = :just_raise_that_exception
822
+ </ruby>
823
+
824
+ This would re-raise all caught exceptions including +MissingTranslationData+.
825
+
826
+ Another example where the default behaviour is less desirable is the Rails TranslationHelper which provides the method +#t+ (as well as +#translate+). When a +MissingTranslationData+ exception occurs in this context, the helper wraps the message into a span with the CSS class +translation_missing+.
827
+
828
+ To do so, the helper forces +I18n#translate+ to raise exceptions no matter what exception handler is defined by setting the +:raise+ option:
829
+
830
+ <ruby>
831
+ I18n.t :foo, :raise => true # always re-raises exceptions from the backend
832
+ </ruby>
833
+
834
+ h3. Conclusion
835
+
836
+ 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.
837
+
838
+ If you find anything missing or wrong in this guide please file a ticket on "our issue tracker":http://i18n.lighthouseapp.com/projects/14948-rails-i18n/overview. If you want to discuss certain portions or have questions please sign up to our "mailinglist":http://groups.google.com/group/rails-i18n.
839
+
840
+
841
+ h3. Contributing to Rails I18n
842
+
843
+ 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-bread of most widely useful features for inclusion in the core.
844
+
845
+ Thus we encourage everybody to experiment with new ideas and features in plugins or other libraries and make them available to the community. (Don't forget to announce your work on our "mailing list":http://groups.google.com/group/rails-i18n!)
846
+
847
+ If you find your own locale (language) missing from our "example translations data":http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale repository for Ruby on Rails, please "_fork_":http://github.com/guides/fork-a-project-and-submit-your-modifications the repository, add your data and send a "pull request":http://github.com/guides/pull-requests.
848
+
849
+
850
+ h3. Resources
851
+
852
+ * "rails-i18n.org":http://rails-i18n.org - Homepage of the rails-i18n project. You can find lots of useful resources on the "wiki":http://rails-i18n.org/wiki.
853
+ * "rails-i18n Google group":http://groups.google.com/group/rails-i18n - The project's mailing list.
854
+ * "Github: rails-i18n":http://github.com/svenfuchs/rails-i18n/tree/master - Code repository for the rails-i18n project. Most importantly you can find lots of "example translations":http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale for Rails that should work for your application in most cases.
855
+ * "Lighthouse: rails-i18n":http://i18n.lighthouseapp.com/projects/14948-rails-i18n/overview - Issue tracker for the rails-i18n project.
856
+ * "Github: i18n":http://github.com/svenfuchs/i18n/tree/master - Code repository for the i18n gem.
857
+ * "Lighthouse: i18n":http://i18n.lighthouseapp.com/projects/14947-ruby-i18n/overview - Issue tracker for the i18n gem.
858
+
859
+
860
+ h3. Authors
861
+
862
+ * "Sven Fuchs":http://www.workingwithrails.com/person/9963-sven-fuchs (initial author)
863
+ * "Karel Minařík":http://www.workingwithrails.com/person/7476-karel-mina-k
864
+
865
+ If you found this guide useful please consider recommending its authors on "workingwithrails":http://www.workingwithrails.com.
866
+
867
+
868
+ h3. Footnotes
869
+
870
+ fn1. Or, to quote "Wikipedia":http://en.wikipedia.org/wiki/Internationalization_and_localization: _"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."_
871
+
872
+ fn2. Other backends might allow or require to use other formats, e.g. a GetText backend might allow to read GetText files.
873
+
874
+ fn3. One of these reasons is that we don'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.
875
+
876
+
877
+ h3. Changelog
878
+
879
+ "Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213/tickets/23