Module: Localite::Settings
- Included in:
- Localite
- Defined in:
- lib/localite/settings.rb
Overview
Localite provides three dimensions for translation keys. A call like that:
Localite.locale(:de) do
Localite.format(:fbml) do
Localite.scope("outer") do
Localite.scope("scope") do
"msg".t
end
end
end
end
looks up the following entries in these formats and languages, in that order:
-
“fbml.de.outer.scope.msg”
-
“fbml.en.outer.scope.msg”
-
“html.de.outer.scope.msg”
-
“html.en.outer.scope.msg”
-
“text.de.outer.scope.msg”
-
“text.en.outer.scope.msg”
-
“fbml.de.scope.msg”
-
“fbml.en.scope.msg”
-
“html.de.scope.msg”
-
“html.en.scope.msg”
-
“text.de.scope.msg”
-
“text.en.scope.msg”
-
“fbml.de.msg”
-
“fbml.en.msg”
-
“html.de.msg”
-
“html.en.msg”
-
“text.de.msg”
-
“text.en.msg”
Instance Method Summary collapse
-
#base ⇒ Object
Returns the base locale; e.g.
- #current_format ⇒ Object
- #current_format=(fmt) ⇒ Object
-
#current_locale ⇒ Object
returns the current locale; defaults to the base locale.
- #current_locale=(locale) ⇒ Object
- #current_scope ⇒ Object
-
#format(format, &block) ⇒ Object
The format setting defines how the template engine deals with its parameters.
-
#inspect ⇒ Object
Inspect ========================================================.
-
#locale(locale, &block) ⇒ Object
runs a block in the changed locale.
-
#scope(*args, &block) ⇒ Object
scope allows to set a scope around a translation.
- #scope!(*args, &block) ⇒ Object
Instance Method Details
#base ⇒ Object
Returns the base locale; e.g. :en
43 44 45 |
# File 'lib/localite/settings.rb', line 43 def base :en end |
#current_format ⇒ Object
146 147 148 |
# File 'lib/localite/settings.rb', line 146 def current_format Thread.current[:"localite:format"] || :text end |
#current_format=(fmt) ⇒ Object
150 151 152 |
# File 'lib/localite/settings.rb', line 150 def current_format=(fmt) Thread.current[:"localite:format"] = fmt end |
#current_locale ⇒ Object
returns the current locale; defaults to the base locale
49 50 51 |
# File 'lib/localite/settings.rb', line 49 def current_locale Thread.current[:"localite:locale"] || base end |
#current_locale=(locale) ⇒ Object
53 54 55 |
# File 'lib/localite/settings.rb', line 53 def current_locale=(locale) I18n.locale = Thread.current[:"localite:locale"] = locale end |
#current_scope ⇒ Object
130 131 132 |
# File 'lib/localite/settings.rb', line 130 def current_scope Thread.current[:"localite:scopes"] ||= Localite::Scopes.new end |
#format(format, &block) ⇒ Object
The format setting defines how the template engine deals with its parameters. In :html mode all parameters will be subject to HTML escaping, while in :text mode the parameters remain unchanged.
142 143 144 |
# File 'lib/localite/settings.rb', line 142 def format(format, &block) scope :format => format, &block end |
#inspect ⇒ Object
Inspect ========================================================
156 157 158 |
# File 'lib/localite/settings.rb', line 156 def inspect "<Localite: [#{current_locale}/#{current_format}]: #{Localite.current_scope.inspect}" end |
#locale(locale, &block) ⇒ Object
runs a block in the changed locale
59 60 61 |
# File 'lib/localite/settings.rb', line 59 def locale(locale, &block) scope :locale => locale, &block end |
#scope(*args, &block) ⇒ Object
scope allows to set a scope around a translation. A scoped translation
Localite.scope("scope") do
"msg".t
end
will look up “scope.msg” and “msg”, in that order, and return the first matching translation in the current locale. Scopes can be stacked; looking up a scoped translation
Localite.scope("outer") do
Localite.scope("scope") do
"msg".t
end
end
will look up “outer.scope.msg”, “scope.msg”, “msg”.
If no translation will be found we look up the same entries in the base locale.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/localite/settings.rb', line 85 def scope(*args, &block) = if args.last.is_a?(Hash) args.pop else {} end # # set locale if locale = [:locale] old_locale = self.current_locale self.current_locale = if I18n.backend.available_locales.include?(locale = locale.to_sym) locale else base end end # # set format if format = [:format] old_format = self.current_format self.current_format = format end # # adjust scope (from the remaining arguments) current_scope.push(*args) yield ensure current_scope.pop(*args) self.current_format = old_format if old_format self.current_locale = old_locale if old_locale end |