Module: I18n::Backend::Base
- Includes:
- Transliterator
- Included in:
- Chain::Implementation, KeyValue::Implementation, Simple::Implementation
- Defined in:
- lib/i18n/backend/base.rb
Constant Summary
Constant Summary
Constants included from Transliterator
Transliterator::DEFAULT_REPLACEMENT_CHAR
Instance Method Summary (collapse)
-
- (Object) available_locales
Returns an array of locales for which translations are available ignoring the reserved translation meta data key :i18n.
-
- (Object) load_translations(*filenames)
Accepts a list of paths to translation files.
-
- (Object) localize(locale, object, format = :default, options = {})
Acts the same as strftime, but uses a localized version of the format string.
- - (Object) reload!
-
- (Object) store_translations(locale, data, options = {})
This method receives a locale, a data hash and options for storing translations.
- - (Object) translate(locale, key, options = {})
Methods included from Transliterator
Instance Method Details
- (Object) available_locales
Returns an array of locales for which translations are available ignoring the reserved translation meta data key :i18n.
75 76 77 |
# File 'lib/i18n/backend/base.rb', line 75 def available_locales raise NotImplementedError end |
- (Object) load_translations(*filenames)
Accepts a list of paths to translation files. Loads translations from plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml for details.
13 14 15 16 |
# File 'lib/i18n/backend/base.rb', line 13 def load_translations(*filenames) filenames = I18n.load_path if filenames.empty? filenames.flatten.each { |filename| load_file(filename) } end |
- (Object) localize(locale, object, format = :default, options = {})
Acts the same as strftime, but uses a localized version of the format string. Takes a key from the date/time formats translations as a format argument (e.g., :short in :'date.formats').
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/i18n/backend/base.rb', line 48 def localize(locale, object, format = :default, = {}) raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime) if Symbol === format key = format type = object.respond_to?(:sec) ? 'time' : 'date' = .merge(:raise => true, :object => object, :locale => locale) format = I18n.t(:#{type}.formats.#{key}", ) end # format = resolve(locale, object, format, options) format = format.to_s.gsub(/%[aAbBpP]/) do |match| case match when '%a' then I18n.t(:date.abbr_day_names", :locale => locale, :format => format)[object.wday] when '%A' then I18n.t(:date.day_names", :locale => locale, :format => format)[object.wday] when '%b' then I18n.t(:date.abbr_month_names", :locale => locale, :format => format)[object.mon] when '%B' then I18n.t(:date.month_names", :locale => locale, :format => format)[object.mon] when '%p' then I18n.t(:time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).upcase if object.respond_to? :hour when '%P' then I18n.t(:time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).downcase if object.respond_to? :hour end end object.strftime(format) end |
- (Object) reload!
79 80 81 |
# File 'lib/i18n/backend/base.rb', line 79 def reload! @skip_syntax_deprecation = false end |
- (Object) store_translations(locale, data, options = {})
This method receives a locale, a data hash and options for storing translations. Should be implemented
20 21 22 |
# File 'lib/i18n/backend/base.rb', line 20 def store_translations(locale, data, = {}) raise NotImplementedError end |
- (Object) translate(locale, key, options = {})
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/i18n/backend/base.rb', line 24 def translate(locale, key, = {}) raise InvalidLocale.new(locale) unless locale entry = key && lookup(locale, key, [:scope], ) if .empty? entry = resolve(locale, key, entry, ) else count, default = .values_at(:count, :default) values = .except(*RESERVED_KEYS) entry = entry.nil? && default ? default(locale, key, default, ) : resolve(locale, key, entry, ) end throw(:exception, I18n::MissingTranslation.new(locale, key, )) if entry.nil? entry = entry.dup if entry.is_a?(String) entry = pluralize(locale, entry, count) if count entry = interpolate(locale, entry, values) if values entry end |