Class: Langulator::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/langulator/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Loader

Returns a new instance of Loader.



5
6
7
8
9
# File 'lib/langulator/loader.rb', line 5

def initialize(options = {})
  @base_path = options[:base_path]
  @origin = options[:origin]
  @alternates = options[:alternates]
end

Instance Attribute Details

#alternatesObject (readonly)

Returns the value of attribute alternates.



4
5
6
# File 'lib/langulator/loader.rb', line 4

def alternates
  @alternates
end

#base_pathObject (readonly)

Returns the value of attribute base_path.



4
5
6
# File 'lib/langulator/loader.rb', line 4

def base_path
  @base_path
end

#originObject (readonly)

Returns the value of attribute origin.



4
5
6
# File 'lib/langulator/loader.rb', line 4

def origin
  @origin
end

Instance Method Details

#destination_translationsObject



19
20
21
22
23
24
25
# File 'lib/langulator/loader.rb', line 19

def destination_translations
  translations = {}
  alternates.each do |language|
    translations[language] = load_translations(language)
  end
  translations
end

#load_translations(language) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/langulator/loader.rb', line 27

def load_translations(language)
  translations = {}
  paths.each do |path|
    translations[path] = read_translations(path, language)
  end
  translations
end

#pathsObject



11
12
13
# File 'lib/langulator/loader.rb', line 11

def paths
  @paths ||= Dir.glob("#{base_path}#{origin}.yml").map {|file| file.gsub("#{origin}.yml", '') }.sort
end

#read_translations(path, language) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/langulator/loader.rb', line 35

def read_translations(path, language)
  file = "#{path}#{language}.yml"
  if File.exists?(file)
    YAML.load(File.read(file))
  else
    {}
  end
end

#source_translationsObject



15
16
17
# File 'lib/langulator/loader.rb', line 15

def source_translations
  translations = load_translations(origin)
end