Module: ZLocalize
- Defined in:
- lib/zlocalize/rails/decimal_attributes.rb,
lib/zlocalize/config.rb,
lib/zlocalize/backend.rb,
lib/zlocalize/harvester.rb,
lib/zlocalize/rails/railtie.rb,
lib/zlocalize/source_processor.rb,
lib/zlocalize/translation_file.rb,
lib/zlocalize/rdoc_source_parser.rb,
lib/zlocalize/rails/translated_columns.rb,
lib/zlocalize/rails/attached_translations.rb
Overview
Translation of attributes values for ActiveRecord ===
Allows a model to have multiple values (one for each given locale) stored for one or more attributes
Uses a Translation model, declared as a polymorphic association
Example use:
class Article < ActiveRecord::Base
....
has_translations
....
end
> @article = Article.new
> @article.insert_translations(:fr => { :title => "L'année en revue" }, :en => { :title => "The Year in Review" })
> @article.translate('title','fr')
=> "L'année en revue"
> @article.translate('title','en')
=> "The year in review"
It is also possible to declare the translations as nested_attributes:
has_translations
accepts_nested_attributes_for :translations, :allow_destroy => true
which would allow to assign translations from within a form (using fields_for helper).
params[:article][:translations_attributes] = [ { :locale => :fr, :name => 'title', :value => "L'année en revue" },
{ :locale => :en, :name => 'title', :value => "The Year in Review" } ]
Defined Under Namespace
Modules: Translatable
Classes: ArrayExpression, ConditionalExpression, Config, DontCareExpression, Expression, Harvester, HarvesterError, HashElementExpression, HashExpression, IdentifierExpression, MissingTranslationDataError, NumberExpression, OperatorExpression, Railtie, RangeExpression, SourceParser, SourceProcessor, StringExpression, SymbolExpression, TranslationEntry, TranslationEntryCollection, TranslationFile, TranslationFileError
Constant Summary
collapse
- INTERPOLATE_MATCH =
/(\\)?\{\{([^\}]+)\}\}/
- SCOPE_MATCH =
/(.*?)(\\)?::(.+)/
- DEFAULT_PLURAL_SELECT_PROC =
lambda { |n| n <= 0 ? 0 : (n > 1 ? 2 : 1) }
- DEFAULT_TITLEIZE_PROC =
lambda { |s| s.to_s.titleize }
- DEFAULT_CONVERT_FLOAT_PROC =
lambda { |s| s.to_s.gsub(",", "") }
- INDENT_STEP =
3
- OPERATOR_TOKENS =
[RDoc::RubyToken::TkGT, RDoc::RubyToken::TkLT, RDoc::RubyToken::TkPLUS,
RDoc::RubyToken::TkMINUS, RDoc::RubyToken::TkMULT, RDoc::RubyToken::TkSTAR,
RDoc::RubyToken::TkDIV,
RDoc::RubyToken::TkMOD, RDoc::RubyToken::TkBITOR, RDoc::RubyToken::TkBITXOR,
RDoc::RubyToken::TkBITAND, RDoc::RubyToken::TkBITNOT, RDoc::RubyToken::TkNOTOP,
RDoc::RubyToken::TkPOW, RDoc::RubyToken::TkCMP, RDoc::RubyToken::TkEQ,
RDoc::RubyToken::TkEQQ, RDoc::RubyToken::TkNEQ, RDoc::RubyToken::TkGEQ,
RDoc::RubyToken::TkLEQ, RDoc::RubyToken::TkANDOP, RDoc::RubyToken::TkOROP,
RDoc::RubyToken::TkMATCH, RDoc::RubyToken::TkNMATCH, RDoc::RubyToken::TkLSHFT,
RDoc::RubyToken::TkRSHFT]
- UNARY_OP_TOKENS =
[RDoc::RubyToken::TkPLUS,RDoc::RubyToken::TkMINUS,RDoc::RubyToken::TkBITNOT,RDoc::RubyToken::TkNOTOP]
- @@load_mutex =
Mutex.new
Class Method Summary
collapse
-
.clean_ruby_string(s) ⇒ Object
-
.config ⇒ Object
-
.convert_float(locale, s) ⇒ Object
-
.default_locale ⇒ Object
-
.default_locale=(value) ⇒ Object
-
.escape_ruby_string(s) ⇒ Object
-
.initialized? ⇒ Boolean
-
.interpolate(locale, string, values = {}) ⇒ Object
Interpolates values into a given string.
-
.locale ⇒ Object
-
.locale=(value) ⇒ Object
-
.locales ⇒ Object
(also: available_locales)
-
.multi_lookup(locale, values, options = {}) ⇒ Object
-
.pluralize(key, count, options = {}) ⇒ Object
(also: p)
-
.pluralize_with_locale(locale, key, count, options = {}) ⇒ Object
-
.reload!(force = false) ⇒ Object
-
.switch_locale(new_locale, &block) ⇒ Object
Change locale inside a block.
-
.titleize(locale, s) ⇒ Object
-
.translate(key, options = {}) ⇒ Object
(also: t)
-
.translate_with_locale(locale, key, options = {}) ⇒ Object
-
.translation_procs ⇒ Object
-
.translations ⇒ Object
-
.unescape_ruby_string(s) ⇒ Object
Class Method Details
.clean_ruby_string(s) ⇒ Object
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/zlocalize/translation_file.rb', line 27
def self.clean_ruby_string(s)
s2 = s.to_s
if s2.size > 1
if (s2[0] == '"' && s2[s2.size-1] == '"') || (s2[0] == "'" && s2[s2.size-1] == "'")
s2 = s2[1..s2.size-2].to_s
end
end
unescape_ruby_string(s2)
end
|
.config ⇒ Object
27
28
29
|
# File 'lib/zlocalize/backend.rb', line 27
def config
@config ||= ZLocalize::Config.new
end
|
.convert_float(locale, s) ⇒ Object
108
109
110
|
# File 'lib/zlocalize/backend.rb', line 108
def convert_float(locale,s)
translation_procs[locale.to_sym][:convert_float].call(s)
end
|
.default_locale ⇒ Object
39
40
41
|
# File 'lib/zlocalize/backend.rb', line 39
def default_locale
I18n.default_locale
end
|
.default_locale=(value) ⇒ Object
43
44
45
|
# File 'lib/zlocalize/backend.rb', line 43
def default_locale=(value)
I18n.default_locale = value
end
|
.escape_ruby_string(s) ⇒ Object
8
9
10
11
12
13
14
15
16
|
# File 'lib/zlocalize/translation_file.rb', line 8
def self.escape_ruby_string(s)
s2 = s.to_s.dup
s2.gsub!("\"","\\\"")
s2.gsub!("\r\n","\n")
s2.gsub!("\n\r","\n")
s2.gsub!("\n","\\n")
s2
end
|
.initialized? ⇒ Boolean
147
148
149
|
# File 'lib/zlocalize/backend.rb', line 147
def initialized?
@initialized ||= false
end
|
.interpolate(locale, string, values = {}) ⇒ Object
Interpolates values into a given string.
interpolate "file {{file}} opened by \\{{user}}", :file => 'test.txt', :user => 'Mr. X'
Note that you have to double escape the \
when you want to escape the {{...}}
key in a string (once for the string and once for the interpolation).
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/zlocalize/backend.rb', line 129
def interpolate(locale, string, values = {})
return string unless string.is_a?(String)
result = string.gsub(INTERPOLATE_MATCH) do
escaped, pattern, key = $1, $2, $2.to_sym
if escaped
"{{#{pattern}}}"
elsif !values.include?(key)
raise ArgumentError, "The #{key} argument is missing in the interpolation of '#{string}'"
else
values[key].to_s
end
end
result
end
|
.locale ⇒ Object
31
32
33
|
# File 'lib/zlocalize/backend.rb', line 31
def locale
I18n.locale
end
|
.locale=(value) ⇒ Object
35
36
37
|
# File 'lib/zlocalize/backend.rb', line 35
def locale=(value)
I18n.locale = value
end
|
.locales ⇒ Object
Also known as:
available_locales
159
160
161
162
|
# File 'lib/zlocalize/backend.rb', line 159
def locales
init_translations unless initialized?
translations.keys.map { |k| k.to_s }
end
|
.multi_lookup(locale, values, options = {}) ⇒ Object
112
113
114
115
116
117
118
119
|
# File 'lib/zlocalize/backend.rb', line 112
def multi_lookup(locale,values,options = {})
result = {}
values.each do |val|
s = lookup(locale,"#{val}")
result[val] = s.html_safe unless s.nil?
end
result
end
|
.pluralize(key, count, options = {}) ⇒ Object
Also known as:
p
53
54
55
56
|
# File 'lib/zlocalize/backend.rb', line 53
def pluralize(key,count,options = {})
loc = options[:locale] || self.locale
pluralize_with_locale(loc,key,count,options)
end
|
.pluralize_with_locale(locale, key, count, options = {}) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/zlocalize/backend.rb', line 87
def pluralize_with_locale(locale, key, count, options = {})
entry = lookup(locale,key)
if entry.nil?
if (default = options.delete(:default))
entry = default
elsif (options.delete(:return_source_on_missing) || @return_source_on_missing) == true
entry = key
else
raise ZLocalize::MissingTranslationDataError.new(locale,key,options)
end
end
n = translation_procs[locale.to_sym][:plural_select].call(count)
options[:count] ||= count
return interpolate(locale, entry[n], options).html_safe
end
|
.reload!(force = false) ⇒ Object
151
152
153
154
155
156
157
|
# File 'lib/zlocalize/backend.rb', line 151
def reload!(force = false)
if force || !@initialized
@initialized = false
@translations = nil
@translation_procs = nil
end
end
|
.switch_locale(new_locale, &block) ⇒ Object
Change locale inside a block
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/zlocalize/backend.rb', line 60
def switch_locale(new_locale,&block)
@@switch_locale_stack ||= []
if block_given?
@@switch_locale_stack.push self.locale
self.locale = new_locale
yield
self.locale = @@switch_locale_stack.pop
else
self.locale = new_locale
end
end
|
.titleize(locale, s) ⇒ Object
104
105
106
|
# File 'lib/zlocalize/backend.rb', line 104
def titleize(locale,s)
translation_procs[locale.to_sym][:titleize].call(s).html_safe
end
|
.translate(key, options = {}) ⇒ Object
Also known as:
t
47
48
49
50
|
# File 'lib/zlocalize/backend.rb', line 47
def translate(key, options = {})
loc = options[:locale] || self.locale
translate_with_locale(loc,key,options)
end
|
.translate_with_locale(locale, key, options = {}) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/zlocalize/backend.rb', line 72
def translate_with_locale(locale, key, options = {})
entry = lookup(locale,"#{key}")
if entry.nil?
if (default = options.delete(:default))
entry = default
elsif (options.delete(:return_source_on_missing) || @return_source_on_missing) == true
entry = remove_scope(key)
else
raise ZLocalize::MissingTranslationDataError.new(locale, key, options)
end
end
return interpolate(locale, entry, options).html_safe
end
|
.translation_procs ⇒ Object
171
172
173
174
|
# File 'lib/zlocalize/backend.rb', line 171
def translation_procs
init_translations unless initialized?
@translation_procs
end
|
.translations ⇒ Object
166
167
168
169
|
# File 'lib/zlocalize/backend.rb', line 166
def translations
init_translations unless initialized?
@translations
end
|
.unescape_ruby_string(s) ⇒ Object
18
19
20
21
22
23
24
25
|
# File 'lib/zlocalize/translation_file.rb', line 18
def self.unescape_ruby_string(s)
s2 = s.to_s
s2.gsub!("\\'","'")
s2.gsub!('\\"','"')
s2.gsub!("\\n","\n")
s2.gsub("\\","")
s2
end
|