Class: Localite::Backend::Simple

Inherits:
I18n::Backend::Simple
  • Object
show all
Defined in:
lib/localite/storage.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Simple

Returns a new instance of Simple.



12
13
14
# File 'lib/localite/storage.rb', line 12

def initialize(*args)
  @locales = args.map(&:to_s) unless args.empty?
end

Instance Method Details

#available_localesObject



16
17
18
19
20
21
22
# File 'lib/localite/storage.rb', line 16

def available_locales
  if @locales 
    @locales.map(&:to_sym)
  else
    super
  end
end

#keysObject



67
68
69
70
71
72
73
# File 'lib/localite/storage.rb', line 67

def keys
  r = []
  translations.each do |k,v|
    r += v.keys
  end
  r.sort.uniq
end

#keys_for_locale(locale) ⇒ Object



63
64
65
# File 'lib/localite/storage.rb', line 63

def keys_for_locale(locale)
  (translations[locale.to_sym] || {}).keys.sort
end

#load_file(filename) ⇒ Object

Loads a single translations file by delegating to #load_rb or #load_yml depending on the file extension and directly merges the data to the existing translations. Raises I18n::UnknownFileType for all other file extensions.

Raises:

  • (I18n::Backend::Simple::UnknownFileType)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/localite/storage.rb', line 29

def load_file(filename)
  locale_from_file = File.basename(filename).sub(/\.[^\.]+$/, "")
  if @locales && !@locales.include?(locale_from_file)
    # dlog "Skipping translations from #{filename}"
    return
  end

  Localite.logger.warn "Loading translations from #{filename}"
  type = File.extname(filename).tr('.', '').downcase
  raise I18n::Backend::Simple::UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}")

  data = send :"load_#{type}", filename

  #
  # 
  if locale_from_file.length == 2 && data.keys.map(&:to_s) != [ locale_from_file ]
    merge_translations(locale_from_file, data) 
  else
    data.each { |locale, d| merge_translations(locale, d) }
  end
end

#load_tr(filename) ⇒ Object

The main differences to a yaml file are: “.tr” files support specific and lower level entries for the same key, and allows to reopen a key.

x:

y: "x.y translation"
  z: "x.y.z translation"
y: 
  a: "x.y.a translation"


129
130
131
# File 'lib/localite/storage.rb', line 129

def load_tr(filename)
  Localite::Backend::Tr.load(filename)
end

#lookup(locale, key, scope = [], options = {}) ⇒ Object

monkeypatches “I18n::Backend::Simple”

Looks up a translation from the translations hash. Returns nil if eiher key is nil, or locale, scope or key do not exist as a key in the nested translations hash. Splits keys or scopes containing dots into multiple keys, i.e. currency.format is regarded the same as %w(currency format).

We are doing this because the default simple storage does not support keys, that inself are part of other keys, i.e. “field” and “field.subfield”



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/localite/storage.rb', line 94

def lookup(locale, key, scope = [], options = {})
  return unless key
  init_translations unless initialized?

  keys = I18n.normalize_keys(locale, key, scope, options[:separator])
  
  #
  # the first key is the locale; all other keys make up the final key.
  lookup_localite_storage(*keys) || begin
    keys.inject(translations) do |result, key|
      key = key.to_sym
      return nil unless result.is_a?(Hash) && result.has_key?(key)
      result = result[key]
      result = resolve(locale, key, result, options) if result.is_a?(Symbol)
      String === result ? result.dup : result
    end
  end
end

#lookup_localite_storage(locale, *keys) ⇒ Object



113
114
115
116
# File 'lib/localite/storage.rb', line 113

def lookup_localite_storage(locale, *keys)
  return nil unless tr = translations_for_locale(locale)
  r = tr[keys.join(".")]
end

#merge_translations(locale, data) ⇒ Object

add an additional translation storage layer.



53
54
55
56
57
# File 'lib/localite/storage.rb', line 53

def merge_translations(locale, data)
  translations_for_locale!(locale).update data
  super
rescue IndexError
end

#translationsObject



59
60
61
# File 'lib/localite/storage.rb', line 59

def translations
  @translations ||= {}
end

#translations_for_locale(locale) ⇒ Object



75
76
77
# File 'lib/localite/storage.rb', line 75

def translations_for_locale(locale)
  translations[locale.to_sym]
end

#translations_for_locale!(locale) ⇒ Object



79
80
81
# File 'lib/localite/storage.rb', line 79

def translations_for_locale!(locale)
  translations[locale.to_sym] ||= {}
end