Module: Lola

Defined in:
lib/lola.rb,
lib/lola/railtie.rb

Defined Under Namespace

Modules: Rails Classes: UnavailableLocale

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_languageObject

Returns the value of attribute default_language.



5
6
7
# File 'lib/lola.rb', line 5

def default_language
  @default_language
end

.default_localeObject

Returns the value of attribute default_locale.



5
6
7
# File 'lib/lola.rb', line 5

def default_locale
  @default_locale
end

.excluded_languagesObject

Returns the value of attribute excluded_languages.



5
6
7
# File 'lib/lola.rb', line 5

def excluded_languages
  @excluded_languages
end

.priority_languagesObject

Returns the value of attribute priority_languages.



5
6
7
# File 'lib/lola.rb', line 5

def priority_languages
  @priority_languages
end

Class Method Details

.language_code(language_name, options = {}) ⇒ Object

Returns the language code corresponding to the supplied language name

Carmen::language_code('English') => 'EN'


51
52
53
# File 'lib/lola.rb', line 51

def self.language_code(language_name, options={})
  search_collection(languages(options), language_name, 0, 1)
end

.language_codesObject

Returns an array of all language codes

Carmen::language_codes => ['AA', 'AB', 'AE', ... ]


57
58
59
# File 'lib/lola.rb', line 57

def self.language_codes
  languages.map {|c| c[1] }
end

.language_name(language_code, options = {}) ⇒ Object

Returns the language name corresponding to the supplied language code, optionally using the specified locale.

Carmen::language_name('EN') => 'English'
Carmen::language_name('ES', :locale => :es) => 'Español'


45
46
47
# File 'lib/lola.rb', line 45

def self.language_name(language_code, options={})
  search_collection(languages(options), language_code, 1, 0)
end

.language_names(options = {}) ⇒ Object

Returns an array of all language names, optionally using the specified locale.

Carmen::language_names => ['Afar', 'Abkhazian', Avestan', ... ]
Carmen::language_names(:locale => :es) => ['Afrikaans', 'Árabe', 'Bengalí', ... ]


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

def self.language_names(options={})
  languages(options).map {|c| c[0] }
end

.languages(options = {}) ⇒ Object

Returns a list of all languages



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lola.rb', line 19

def self.languages(options={})
  # Use specified locale or fall back to default locale
  locale = options.delete(:locale) || @default_locale
  locale = available_locales.include?(locale) ? locale.to_s : @default_locale.to_s

  # Load the country list for the specified locale
  @languages ||= {}
  unless @languages[locale]
    # Check if data in the specified locale is available
    localized_data = File.join(@data_path, "#{locale}.yml")
    unless File.exists?(localized_data)
      raise(UnavailableLocale, "Could not load languages for '#{locale}' locale")
    end

    # As the data exists, load it
    @languages[locale] = YAML.load_file(localized_data)
  end

  # Return data after filtering excluded languages and prepending prepended languages
  result = @languages[locale].reject { |c| excluded_languages.include?( c[1] ) }
  priority_languages.map { |code| [ search_collection(result, code, 1, 0), code ] } + result
end