Class: I18n::Config

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

Constant Summary collapse

@@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

Constructor Details

#initializeConfig

Returns a new instance of Config.



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

def initialize
  @owner = Fiber.current
end

Instance Method Details

#available_localesObject

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



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

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

#available_locales=(locales) ⇒ Object

Sets the available locales.



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

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_initialized?Boolean

Returns true if the available_locales have been initialized

Returns:

  • (Boolean)


98
99
100
# File 'lib/i18n/config.rb', line 98

def available_locales_initialized?
  ( !!defined?(@@available_locales) && !!@@available_locales )
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.



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

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.



54
55
56
# File 'lib/i18n/config.rb', line 54

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

#backend=(backend) ⇒ Object

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



59
60
61
# File 'lib/i18n/config.rb', line 59

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.



104
105
106
# File 'lib/i18n/config.rb', line 104

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

#default_localeObject

Returns the current default locale. Defaults to :‘en’



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

def default_locale
  @@default_locale ||= :en
end

#default_locale=(locale) ⇒ Object

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



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

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 ‘.’



109
110
111
# File 'lib/i18n/config.rb', line 109

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

#default_separator=(separator) ⇒ Object

Sets the current default scope separator.



114
115
116
# File 'lib/i18n/config.rb', line 114

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

#enforce_available_localesObject



175
176
177
# File 'lib/i18n/config.rb', line 175

def enforce_available_locales
  @@enforce_available_locales
end

#enforce_available_locales=(enforce_available_locales) ⇒ Object



179
180
181
# File 'lib/i18n/config.rb', line 179

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.



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

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

#exception_handler=(exception_handler) ⇒ Object

Sets the exception handler.



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

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

#initialize_copy(other) ⇒ Object



25
26
27
# File 'lib/i18n/config.rb', line 25

def initialize_copy(other)
  @owner = Fiber.current
end

#interpolation_patternsObject

Returns the current interpolation patterns. Defaults to I18n::DEFAULT_INTERPOLATION_PATTERNS.



185
186
187
# File 'lib/i18n/config.rb', line 185

def interpolation_patterns
  @@interpolation_patterns ||= I18n::DEFAULT_INTERPOLATION_PATTERNS.dup
end

#interpolation_patterns=(interpolation_patterns) ⇒ Object

Sets the current interpolation patterns. Used to set a interpolation patterns.

E.g. using {} as a placeholder like “{hello}, world!”:

I18n.config.interpolation_patterns << /\{\{(\w+)\}\}/


195
196
197
# File 'lib/i18n/config.rb', line 195

def interpolation_patterns=(interpolation_patterns)
  @@interpolation_patterns = interpolation_patterns
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'


160
161
162
# File 'lib/i18n/config.rb', line 160

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.



166
167
168
169
170
# File 'lib/i18n/config.rb', line 166

def load_path=(load_path)
  @@load_path = load_path
  @@available_locales_set = nil
  backend.reload!
end

#localeObject

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



9
10
11
# File 'lib/i18n/config.rb', line 9

def locale
  defined?(@locale) && @locale != nil ? @locale : default_locale
end

#locale=(locale) ⇒ Object

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



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

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.



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

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 suppress raising an exception and return string instead:

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


148
149
150
# File 'lib/i18n/config.rb', line 148

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

#owned_by?(fiber) ⇒ Boolean

Returns:

  • (Boolean)


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

def owned_by?(fiber)
  @owner == fiber
end

#owner=(fiber) ⇒ Object



21
22
23
# File 'lib/i18n/config.rb', line 21

def owner=(fiber)
  @owner = fiber
end

#set!Object

Sets this configuration as the current one for the active execution context. The stored configuration is frozen to avoid sharing mutable state between fibers.



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

def set!
  self.owner = Fiber.current unless frozen?
  freeze
  I18n.config = self
  self
end

#with(**attrs) ⇒ Object

Returns a copied configuration with the provided attributes set.



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

def with(**attrs)
  dup.tap do |copy|
    attrs.each do |name, value|
      copy.public_send("#{name}=", value)
    end
  end
end