Class: I18n::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n/config.rb

Constant Summary

@@enforce_available_locales =

Whether or not to verify if locales are in the list of available locales. Defaults to true.

true

Instance Method Summary collapse

Instance Method Details

#available_localesObject

Returns an array of locales for which translations are available. Unless you explicitely set these through I18n.available_locales= the call will be delegated to the backend.



41
42
43
44
# File 'lib/i18n/config.rb', line 41

def available_locales
  @@available_locales ||= nil
  @@available_locales || backend.available_locales
end

#available_locales=(locales) ⇒ Object

Sets the available locales.



55
56
57
58
59
# File 'lib/i18n/config.rb', line 55

def available_locales=(locales)
  @@available_locales = Array(locales).map { |locale| locale.to_sym }
  @@available_locales = nil if @@available_locales.empty?
  @@available_locales_set = nil
end

#available_locales_setObject

Caches the available locales list as both strings and symbols in a Set, so that we can have faster lookups to do the available locales enforce check.



48
49
50
51
52
# File 'lib/i18n/config.rb', line 48

def available_locales_set #:nodoc:
  @@available_locales_set ||= available_locales.inject(Set.new) do |set, locale|
    set << locale.to_s << locale.to_sym
  end
end

#backendObject

Returns the current backend. Defaults to Backend::Simple.



18
19
20
# File 'lib/i18n/config.rb', line 18

def backend
  @@backend ||= Backend::Simple.new
end

#backend=(backend) ⇒ Object

Sets the current backend. Used to set a custom backend.



23
24
25
# File 'lib/i18n/config.rb', line 23

def backend=(backend)
  @@backend = backend
end

#clear_available_locales_setObject

Clears the available locales set so it can be recomputed again after I18n gets reloaded.



63
64
65
# File 'lib/i18n/config.rb', line 63

def clear_available_locales_set #:nodoc:
  @@available_locales_set = nil
end

#default_localeObject

Returns the current default locale. Defaults to :'en'



28
29
30
# File 'lib/i18n/config.rb', line 28

def default_locale
  @@default_locale ||= :en
end

#default_locale=(locale) ⇒ Object

Sets the current default locale. Used to set a custom default locale.



33
34
35
36
# File 'lib/i18n/config.rb', line 33

def default_locale=(locale)
  I18n.enforce_available_locales!(locale)
  @@default_locale = locale && locale.to_sym
end

#default_separatorObject

Returns the current default scope separator. Defaults to '.'



68
69
70
# File 'lib/i18n/config.rb', line 68

def default_separator
  @@default_separator ||= '.'
end

#default_separator=(separator) ⇒ Object

Sets the current default scope separator.



73
74
75
# File 'lib/i18n/config.rb', line 73

def default_separator=(separator)
  @@default_separator = separator
end

#enforce_available_localesObject



132
133
134
# File 'lib/i18n/config.rb', line 132

def enforce_available_locales
  @@enforce_available_locales
end

#enforce_available_locales=(enforce_available_locales) ⇒ Object



136
137
138
# File 'lib/i18n/config.rb', line 136

def enforce_available_locales=(enforce_available_locales)
  @@enforce_available_locales = enforce_available_locales
end

#exception_handlerObject

Returns the current exception handler. Defaults to an instance of I18n::ExceptionHandler.



79
80
81
# File 'lib/i18n/config.rb', line 79

def exception_handler
  @@exception_handler ||= ExceptionHandler.new
end

#exception_handler=(exception_handler) ⇒ Object

Sets the exception handler.



84
85
86
# File 'lib/i18n/config.rb', line 84

def exception_handler=(exception_handler)
  @@exception_handler = exception_handler
end

#load_pathObject

Allow clients to register paths providing translation data sources. The backend defines acceptable sources.

E.g. the provided SimpleBackend accepts a list of paths to translation files which are either named *.rb and contain plain Ruby Hashes or are named *.yml and contain YAML data. So for the SimpleBackend clients may register translation files like this:

I18n.load_path << 'path/to/locale/en.yml'


119
120
121
# File 'lib/i18n/config.rb', line 119

def load_path
  @@load_path ||= []
end

#load_path=(load_path) ⇒ Object

Sets the load path instance. Custom implementations are expected to behave like a Ruby Array.



125
126
127
# File 'lib/i18n/config.rb', line 125

def load_path=(load_path)
  @@load_path = load_path
end

#localeObject

The only configuration value that is not global and scoped to thread is :locale. It defaults to the default_locale.



7
8
9
# File 'lib/i18n/config.rb', line 7

def locale
  @locale ||= default_locale
end

#locale=(locale) ⇒ Object

Sets the current locale pseudo-globally, i.e. in the Thread.current hash.



12
13
14
15
# File 'lib/i18n/config.rb', line 12

def locale=(locale)
  I18n.enforce_available_locales!(locale)
  @locale = locale && locale.to_sym
end

#missing_interpolation_argument_handlerObject

Returns the current handler for situations when interpolation argument is missing. MissingInterpolationArgument will be raised by default.



90
91
92
93
94
# File 'lib/i18n/config.rb', line 90

def missing_interpolation_argument_handler
  @@missing_interpolation_argument_handler ||= lambda do |missing_key, provided_hash, string|
    raise MissingInterpolationArgument.new(missing_key, provided_hash, string)
  end
end

#missing_interpolation_argument_handler=(exception_handler) ⇒ Object

Sets the missing interpolation argument handler. It can be any object that responds to #call. The arguments that will be passed to #call are the same as for MissingInterpolationArgument initializer. Use Proc.new if you don't care about arity.

Example:

You can supress raising an exception and return string instead:

I18n.config.missing_interpolation_argument_handler = Proc.new do |key|
  "#{key} is missing"
end


107
108
109
# File 'lib/i18n/config.rb', line 107

def missing_interpolation_argument_handler=(exception_handler)
  @@missing_interpolation_argument_handler = exception_handler
end