3 Globalize2 is the successor of Globalize for Rails.
5 It is compatible with and builds on the new "I18n api in Ruby on Rails":http://rails-i18n.org. and adds model translations as well as a bunch of other useful features, such as Locale fallbacks (RFC4647 compliant) and automatic loading of Locale data from defined directory/file locations.
7 Globalize2 is much more lightweight and modular than its predecessor was. Content translations in Globalize2 use default ActiveRecord features and do not limit any functionality any more.
9 All features and tools in Globalize2 are implemented in the most unobstrusive and loosely-coupled way possible, so you can pick whatever features or tools you need for your application and combine them with other tools from other libraries or plugins.
13 Rails 2.2 (currently Rails edge)
17 To install Globalize2 with its default setup just use:
20 script/plugin install git://github.com/joshmh/globalize2.git
25 * activate model translations
26 * set I18n.load_path to an instance of Globalize::LoadPath
27 * set I18n.backend to an instance of Globalize::Backend::Static
31 You might want to add additional configuration to an initializer, e.g. config/initializers/globalize.rb
33 h2. Model translations
35 Model translations (or content translations) allow you to translate your models' attribute values. E.g.
38 class Post < ActiveRecord::Base
39 translates :title, :text
43 Allows you to values for the attributes :title and :text per locale:
47 post.title # Globalize2 rocks!
50 post.title # גלובאלייז2 שולט!
53 In order to make this work, you'll need to add the appropriate translation tables. Globalize2 comes with a handy helper method to help you do this. It's called @create_translation_table!@. Here's an example:
56 class CreatePosts < ActiveRecord::Migration
58 create_table :posts do |t|
61 Post.create_translation_table! :title => :string, :text => :text
65 Post.drop_translation_table!
70 Note that the ActiveRecord model @Post@ must already exist and have a @translates@ directive listing the translated fields.
72 h2. Globalize::Backend::Static
74 Globalize2 ships with a Static backend that builds on the Simple backend from the I18n library (which is shipped with Rails) and adds the following features:
76 * It uses locale fallbacks when looking up translation data.
77 * It returns an instance of Globalize::Translation::Static instead of a plain Ruby String as a translation.
78 * It allows to hook in custom pluralization logic as lambdas.
80 h2. Custom pluralization logic
82 The Simple backend has its pluralization algorithm baked in hardcoded. This algorithm is only suitable for English and other languages that have the same pluralization rules. It is not suitable for, e.g., Czech though.
84 To add custom pluralization logic to Globalize' Static backend you can do something like this:
87 @backend.add_pluralizer :cz, lambda{|c|
88 c == 1 ? :one : (2..4).include?(c) ? :few : :other
94 Globalize2 ships with a Locale fallback tool which extends the I18n module to hold a fallbacks instance which is set to an instance of Globalize::Locale::Fallbacks by default but can be swapped with a different implementation.
96 Globalize2 fallbacks will compute a number of other locales for a given locale. For example:
99 I18n.fallbacks[:"es-MX"] # => [:"es-MX", :es, :"en-US", :en]
102 Globalize2 fallbacks always fall back to
104 * all parents of a given locale (e.g. :es for :"es-MX"),
105 * then to the fallbacks' default locales and all of their parents and
106 * finally to the :root locale.
108 The default locales are set to [:"en-US"] by default but can be set to something else. The root locale is a concept borrowed from "CLDR":http://unicode.org and makes sense for storing common locale data which works as a last default fallback (e.g. "ltr" for bidi directions).
110 One can additionally add any number of additional fallback locales manually. These will be added before the default locales to the fallback chain. For example:
115 fb.map :ca => :"es-ES"
116 fb[:ca] # => [:ca, :"es-ES", :es, :"en-US", :en]
118 fb.map :"ar-PS" => :"he-IL"
119 fb[:"ar-PS"] # => [:"ar-PS", :ar, :"he-IL", :he, :"en-US", :en]
120 fb[:"ar-EG"] # => [:"ar-EG", :ar, :"en-US", :en]
122 fb.map :sms => [:"se-FI", :"fi-FI"]
123 fb[:sms] # => [:sms, :"se-FI", :se, :"fi-FI", :fi, :"en-US", :en]
126 h2. Globalize::LoadPath
128 Globalize2 replaces the plain Ruby array that is set to I18n.load_path by default through an instance of Globalize::LoadPath.
130 This object can be populated with both paths to files and directories. If a path to a directory is added to it it will look up all locale data files present in that directory enforcing the following convention:
133 I18n.load_path << "#{RAILS_ROOT}/lib/locales"
135 # will load all the following files if present:
138 lib/locales/fr/*.yaml
140 lib/locales/ru/*.yaml
144 One can also specify which locales are used. By default this is set to "*" meaning that files for all locales are added. To define that only files for the locale :es are added one can specify:
147 I18n.load_path.locales = [:es]
150 One can also specify which file extensions are used. By default this is set to ['rb', 'yml'] so plain Ruby and YAML files are added if found. To define that only *.sql files are added one can specify:
153 I18n.load_path.extensions = ['sql']
156 Note that Globalize::LoadPath "expands" a directory to its contained file paths immediately when you add it to the load_path. Thus, if you change the locales or extensions settings in the middle of your application the change won't be applied to already added file paths.
159 h2. Globalize::Translation classes
161 Globalize2's Static backend as well as Globalize2 model translations return instances of Globalize::Translation classes (instead of plain Ruby Strings). These are simple and lightweight value objects that carry some additional meta data about the translation and how it was looked up.
163 Model translations return instances of Globalize::Translation::Attribute, the Static backend returns instances of Globalize::Translation::Static.
170 # Translation::Attribute
171 title = Post.first.title # assuming that no translation can be found:
172 title.locale # => :en
173 title.requested_locale # => :de
174 title.fallback? # => true
176 # Translation::Static
177 rails = I18n.t :rails # assuming that no translation can be found:
178 rails.locale # => :en
179 rails.requested_locale # => :de
180 rails.fallback? # => true
181 rails.options # returns the options passed to #t
182 rails.plural_key # returns the plural_key (e.g. :one, :other)
183 rails.original # returns the original translation with no values
184 # interpolated to it (e.g. "Hi {{name}}!")
187 h2. Missing Translations Log Handler
189 A simple exception handler that behaves like the default exception handler but additionally logs missing translations to a given log.
191 Useful for identifying missing translations during testing.
195 require 'globalize/i18n/missing_translations_log_handler
196 I18n.missing_translations_logger = RAILS_DEFAULT_LOGGER
197 I18n.exception_handler = :missing_translations_log_handler
199 To set up a different log file:
201 logger = Logger.new("#{RAILS_ROOT}/log/missing_translations.log")
202 I18n.missing_translations_logger = logger