Class: Locale

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/models/locale.rb

Constant Summary collapse

@@default_locale =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_localesObject



56
57
58
# File 'lib/models/locale.rb', line 56

def self.available_locales
  all.map(&:code).map(&:to_sym) rescue []
end

.default_localeObject



14
15
16
# File 'lib/models/locale.rb', line 14

def self.default_locale
  @@default_locale ||= self.find(:first, :conditions => {:code => I18n.default_locale.to_s})
end

.reset_default_localeObject



18
19
20
# File 'lib/models/locale.rb', line 18

def self.reset_default_locale
  @@default_locale = nil
end

Instance Method Details

#copy_from_default(key, pluralization_index) ⇒ Object



42
43
44
45
46
# File 'lib/models/locale.rb', line 42

def copy_from_default(key, pluralization_index)
  if !self.default_locale? && Locale.default_locale.has_translation?(key, pluralization_index)
    create_translation(key, key, pluralization_index)
  end
end

#create_translation(key, value, pluralization_index = 1) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/models/locale.rb', line 26

def create_translation(key, value, pluralization_index=1)
  conditions = {:key => key, :raw_key => key.to_s, :pluralization_index => pluralization_index}

  # set the key as the value if we're using the default locale and the key is a string
  conditions.merge!({:value => value}) if (self.code == I18n.default_locale.to_s && key.is_a?(String))
  translation = self.translations.create(conditions)

  # hackity hack.  bug #922 maybe?
  self.connection.commit_db_transaction unless RAILS_ENV['test']
  translation
end

#default_locale?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/models/locale.rb', line 60

def default_locale?
  self == Locale.default_locale
end

#find_translation_or_copy_from_default_locale(key, pluralization_index) ⇒ Object



38
39
40
# File 'lib/models/locale.rb', line 38

def find_translation_or_copy_from_default_locale(key, pluralization_index)
  self.translations.find_by_key_and_pluralization_index(Translation.hk(key), pluralization_index) || copy_from_default(key, pluralization_index)
end

#has_translation?(key, pluralization_index = 1) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/models/locale.rb', line 48

def has_translation?(key, pluralization_index=1)
  self.translations.exists?(:key => Translation.hk(key), :pluralization_index => pluralization_index)
end

#percentage_translatedObject



52
53
54
# File 'lib/models/locale.rb', line 52

def percentage_translated
  (self.translations.translated.count.to_f / self.translations.count.to_f * 100).round rescue 100
end

#to_paramObject



64
65
66
# File 'lib/models/locale.rb', line 64

def to_param
  self.code
end

#translation_from_key(key) ⇒ Object



22
23
24
# File 'lib/models/locale.rb', line 22

def translation_from_key(key)
  self.translations.find(:first, :conditions => {:key => Translation.hk(key)})
end