Class: I18nLanguageSelect::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_language_select/import.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locales = nil, namespace = nil) ⇒ Import

Returns a new instance of Import.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/i18n_language_select/import.rb', line 20

def initialize(locales = nil, namespace=nil)
  @namespace = namespace || I18nLanguageSelect.configuration.namespace.to_s
  @locales = if locales
    locales.split(",")
  else
    fetch_locales
  end
  if @locales && @locales.length
    puts "Gathering languages from unicode.org using the namespace '#{@namespace}' and the locales: #{@locales.join(', ')}"
  else
    puts "Unable to gather languages. No locales specified. Either specifiy locales using the environment variable LOCALES, or install the i18n gem."
  end
end

Instance Attribute Details

#localesObject (readonly)

Returns the value of attribute locales.



18
19
20
# File 'lib/i18n_language_select/import.rb', line 18

def locales
  @locales
end

#output_dirObject

Returns the value of attribute output_dir.



17
18
19
# File 'lib/i18n_language_select/import.rb', line 17

def output_dir
  @output_dir
end

Instance Method Details

#processObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/i18n_language_select/import.rb', line 34

def process
  @locales.threach(4, :each_with_index) do |locale, index|
    puts "Processing: #{locale}"
    # ----- Get the CLDR HTML     --------------------------------------------------
    begin
      doc = Nokogiri::HTML(open("http://www.unicode.org/cldr/charts/latest/summary/#{locale}.html"))
    rescue => e
      puts "[!] Invalid locale name '#{locale}'! Not found in CLDR (#{e})"
      return if index == @locales.length - 1
    end

    languages = search_for_languages(doc)
    if languages.keys.length
      # ----- Parse the HTML with Nokogiri ----------------------------------------
      output = { locale => { @namespace => languages } }
      # ----- Write the parsed values into file      ---------------------------------
      write_for(locale, output)
    end
  end
end