Module | ArkanisDevelopment::SimpleLocalization::LocalizedApplication::Language |
In: |
lib/features/localized_application.rb
|
This module will extend the ArkanisDevelopment::SimpleLocalization::Language class with all necessary class methods.
This class method is used to access entries used by the localized application feature. Since the app section of the language file is reserved for this feature this method restricts the scope of the entries available to the app section. The method should only be used for application localization and therefor there is no need to access other sections of the language file with this method.
app_default_value: No translation available app: index: title: Welcome to XYZ subtitle: Have a nice day... Language.app_not_scoped(:index, :subtitle) # => "Have a nice day..."
If the specified entry does not exists a default value is returned. If the last argument specified is a string this string is returned as default value. Assume the same language file data as above:
Language.app_not_scoped(:index, "Welcome to my app") # => "Welcome to my app"
The "Welcome to my app" entry doesn‘t exists in the language file. Because the last argument is a string it will returned as a default value. If the last argument isn‘t a string the method will return the app_default_value entry of the language file. Again, same language file data as above:
Language.app_not_scoped(:index, :welcome) # => "No translation available"
The :welcome entry does not exists. The last argument isn‘t a string and therefore the value of the app_default_value entry is returned. If this fall back entry does not exists nil is returned.
This method does not respect the scope set by the with_app_scope method. This is done by the app_scoped method.
# File lib/features/localized_application.rb, line 136 136: def app_not_scoped(*keys) 137: self.entry(:app, *keys) || begin 138: substitution_args = if keys.last.kind_of?(Array) 139: keys.pop 140: elsif keys.last.kind_of?(Hash) 141: [keys.pop] 142: else 143: [] 144: end 145: if keys.last.kind_of?(String) 146: self.substitute_entry keys.last, *substitution_args 147: else 148: self.entry(:app_default_value) 149: end 150: end 151: end
A shortcut for creating a CachedLangSectionProxy object. Such a proxy is a object which redirects almost all messages to a specific entry of the currently selected language.
Assume German and English language files like this:
de.yml
app: title: Deutscher Test options: [dies, das, jenes]
en.yml
app: title: English test options: [this, that, other stuff]
Now we can create a proxy object for these entries and switch between languages:
@title = Language.app_proxy :title @options = Language.app_proxy :options, :orginal_receiver => [] # no language file loaded (this is what the <code>orginal_receiver</code> option is for, defaults to "") @title.inspect # => "" @options.inspect # => [] # now with switching Language.use :de @title.inspect # => "Deutscher Test" @options.inspect # => ["dies", "das", "jenes"] Language.use :en @title.inspect # => "English test" @options.inspect # => ["this", "that", "other stuff"]
This all happens without changing the actual @title or @options variable. So to speek a proxy fakes a simple variable but it‘s value is exchanged dependend on the current language.
This is actually very useful if a method expects just one variable at the application startup and thus doesn‘t support language switching, e.g. the message parameter of the validates_presence_of method (here the global l_proxy shortcut for Language.app_proxy is used):
class Something < ActiveRecord::Base validates_presence_of :name, :message => l_proxy(:messages, :name_required) end
Now the error message added by validates_presence_of will also be switched if the language is switched. This is a very efficient way to inject language switching code into methods not made for language switching and is used by many other features of this plugin.
# File lib/features/localized_application.rb, line 251 251: def app_proxy(*keys) 252: options = {:orginal_receiver => ''} 253: options.update(keys.pop) if keys.last.kind_of?(Hash) 254: options[:sections] = [:app] + keys 255: CachedLangSectionProxy.new options 256: end
Basically the same as the app_not_scoped method but app_scoped does respect the scope set by the app_with_scope method.
Assuming the following language file data:
app_default_value: No translation available app: index: title: Welcome to XYZ subtitle: Have a nice day...
The following code would output:
Language.app_with_scope :index do Language.app_scoped :title # => "Welcome to XYZ" Language.app_scoped :subtitle # => "Have a nice day..." Language.app_scoped "I don't exist" # => "I don't exist" end Language.app_scoped :index, :title # => "Welcome to XYZ" Language.app_scoped :not_existing_key # => "No translation available"
# File lib/features/localized_application.rb, line 97 97: def app_scoped(*keys) 98: self.app_not_scoped(*(@@app_scope_stack.flatten + keys)) 99: end
Narrows down the scope of the app_scoped method. Useful if you have a very nested language file and don‘t want to use the lc helpers:
app: layout: nav: main: home: Homepage contact: Contact about: About
Usually the calls to the app_scoped method would look like this:
Language.app_scoped :layout, :nav, :main, :home # => "Homepage" Language.app_scoped :layout, :nav, :main, :contact # => "Contact" Language.app_scoped :layout, :nav, :main, :about # => "About"
In this situation you can use with_app_scope to save some work:
Language.with_app_scope :layout, :nav, :main do Language.app_scoped :home # => "Homepage" Language.app_scoped :contact # => "Contact" Language.app_scoped :about # => "About" end
Every call to the app_scoped method inside the block will automatically be prefixed with the sections you specified to the with_app_scope method.
# File lib/features/localized_application.rb, line 181 181: def with_app_scope(*scope_sections, &block) 182: @@app_scope_stack.push scope_sections 183: begin 184: yield 185: ensure 186: @@app_scope_stack.pop 187: end 188: end