summaryrefslogtreecommitdiff
path: root/vendor/plugins/globalize2/README.textile
diff options
context:
space:
mode:
authorhukl <contact@smyck.org>2010-01-14 22:21:43 +0100
committerhukl <contact@smyck.org>2010-01-14 22:21:43 +0100
commit57d1382013c85a7b11ac8ce5e683f6006b12b328 (patch)
tree19646c4fa5952fa1cf0a2c874952affa346373f1 /vendor/plugins/globalize2/README.textile
parent1b86bf5f2e35d1820bfa32d979bcc2d9ff9a9a8e (diff)
Updated globalize2 to latest version. Modified existing code accoringly
Diffstat (limited to 'vendor/plugins/globalize2/README.textile')
-rw-r--r--vendor/plugins/globalize2/README.textile161
1 files changed, 12 insertions, 149 deletions
diff --git a/vendor/plugins/globalize2/README.textile b/vendor/plugins/globalize2/README.textile
index dbd72883..7155a7c7 100644
--- a/vendor/plugins/globalize2/README.textile
+++ b/vendor/plugins/globalize2/README.textile
@@ -1,16 +1,17 @@
1h1. Globalize2 1h1. Globalize2
2 2
3Globalize2 is the successor of Globalize for Rails. 3Globalize2 is the successor of Globalize for Rails.
4 4
5It 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. 5It is compatible with and builds on the new "I18n api in Ruby on Rails":http://guides.rubyonrails.org/i18n.html. and adds model translations to ActiveRecord.
6 6
7Globalize2 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. 7Globalize2 is much more lightweight and compatible than its predecessor was. Model translations in Globalize2 use default ActiveRecord features and do not limit any ActiveRecord functionality any more.
8
9All 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.
10 8
11h2. Requirements 9h2. Requirements
12 10
13Rails 2.2 (currently Rails edge) 11ActiveRecord
12I18n
13
14(or Rails > 2.2)
14 15
15h2. Installation 16h2. Installation
16 17
@@ -20,19 +21,9 @@ To install Globalize2 with its default setup just use:
20script/plugin install git://github.com/joshmh/globalize2.git 21script/plugin install git://github.com/joshmh/globalize2.git
21</code></pre> 22</code></pre>
22 23
23This will:
24
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
28
29h2. Configuration
30
31You might want to add additional configuration to an initializer, e.g. config/initializers/globalize.rb
32
33h2. Model translations 24h2. Model translations
34 25
35Model translations (or content translations) allow you to translate your models' attribute values. E.g. 26Model translations allow you to translate your models' attribute values. E.g.
36 27
37<pre><code> 28<pre><code>
38class Post < ActiveRecord::Base 29class Post < ActiveRecord::Base
@@ -44,10 +35,10 @@ Allows you to values for the attributes :title and :text per locale:
44 35
45<pre><code> 36<pre><code>
46I18n.locale = :en 37I18n.locale = :en
47post.title # Globalize2 rocks! 38post.title # => Globalize2 rocks!
48 39
49I18n.locale = :he 40I18n.locale = :he
50post.title # גלובאלייז2 שולט! 41post.title # => גלובאלייז2 שולט!
51</code></pre> 42</code></pre>
52 43
53In 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: 44In 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:
@@ -69,134 +60,6 @@ end
69 60
70Note that the ActiveRecord model @Post@ must already exist and have a @translates@ directive listing the translated fields. 61Note that the ActiveRecord model @Post@ must already exist and have a @translates@ directive listing the translated fields.
71 62
72h2. Globalize::Backend::Static 63h2. Migration from Globalize
73
74Globalize2 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:
75
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.
79
80h2. Custom pluralization logic
81
82The 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.
83
84To add custom pluralization logic to Globalize' Static backend you can do something like this:
85
86<pre><code>
87@backend.add_pluralizer :cz, lambda{|c|
88 c == 1 ? :one : (2..4).include?(c) ? :few : :other
89}
90</code></pre>
91
92h2. Locale Fallbacks
93
94Globalize2 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.
95
96Globalize2 fallbacks will compute a number of other locales for a given locale. For example:
97
98<pre><code>
99I18n.fallbacks[:"es-MX"] # => [:"es-MX", :es, :"en-US", :en]
100</code></pre>
101
102Globalize2 fallbacks always fall back to
103
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.
107
108The 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).
109
110One can additionally add any number of additional fallback locales manually. These will be added before the default locales to the fallback chain. For example:
111
112<pre><code>
113fb = I18n.fallbacks
114
115fb.map :ca => :"es-ES"
116fb[:ca] # => [:ca, :"es-ES", :es, :"en-US", :en]
117
118fb.map :"ar-PS" => :"he-IL"
119fb[:"ar-PS"] # => [:"ar-PS", :ar, :"he-IL", :he, :"en-US", :en]
120fb[:"ar-EG"] # => [:"ar-EG", :ar, :"en-US", :en]
121
122fb.map :sms => [:"se-FI", :"fi-FI"]
123fb[:sms] # => [:sms, :"se-FI", :se, :"fi-FI", :fi, :"en-US", :en]
124</code></pre>
125
126h2. Globalize::LoadPath
127
128Globalize2 replaces the plain Ruby array that is set to I18n.load_path by default through an instance of Globalize::LoadPath.
129
130This 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:
131
132<pre><code>
133I18n.load_path << "#{RAILS_ROOT}/lib/locales"
134
135# will load all the following files if present:
136lib/locales/all.yml
137lib/locales/fr.yml
138lib/locales/fr/*.yaml
139lib/locales/ru.yml
140lib/locales/ru/*.yaml
141...
142</code></pre>
143
144One 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:
145
146<pre><code>
147I18n.load_path.locales = [:es]
148</code></pre>
149
150One 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:
151
152<pre><code>
153I18n.load_path.extensions = ['sql']
154</code></pre>
155
156Note 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.
157
158
159h2. Globalize::Translation classes
160
161Globalize2'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.
162
163Model translations return instances of Globalize::Translation::Attribute, the Static backend returns instances of Globalize::Translation::Static.
164
165For example:
166
167<pre><code>
168I18n.locale = :de
169
170# Translation::Attribute
171title = Post.first.title # assuming that no translation can be found:
172title.locale # => :en
173title.requested_locale # => :de
174title.fallback? # => true
175
176# Translation::Static
177rails = I18n.t :rails # assuming that no translation can be found:
178rails.locale # => :en
179rails.requested_locale # => :de
180rails.fallback? # => true
181rails.options # returns the options passed to #t
182rails.plural_key # returns the plural_key (e.g. :one, :other)
183rails.original # returns the original translation with no values
184 # interpolated to it (e.g. "Hi {{name}}!")
185</code></pre>
186
187h2. Missing Translations Log Handler
188
189A simple exception handler that behaves like the default exception handler but additionally logs missing translations to a given log.
190
191Useful for identifying missing translations during testing.
192
193E.g.
194
195 require 'globalize/i18n/missing_translations_log_handler
196 I18n.missing_translations_logger = RAILS_DEFAULT_LOGGER
197 I18n.exception_handler = :missing_translations_log_handler
198
199To set up a different log file:
200 64
201 logger = Logger.new("#{RAILS_ROOT}/log/missing_translations.log") 65See this script by Tomasz Stachewicz: http://gist.github.com/120867
202 I18n.missing_translations_logger = logger