Class: WebTranslateIt::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/web_translate_it/configuration.rb

Overview

Handles the configuration of your project, both via the the configuration file and via the API. Implementation example, assuming you have a valid config/translation.yml file:

configuration = WebTranslateIt::Configuration.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path = RAILS_ROOT, path_to_config = "config/translation.yml") ⇒ Configuration

Load configuration file from the path.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/web_translate_it/configuration.rb', line 15

def initialize(root_path=RAILS_ROOT, path_to_config="config/translation.yml")
  self.path           = root_path
  configuration       = YAML.load_file(File.join(root_path, path_to_config))
  self.logger         = logger
  self.api_key        = configuration['api_key']
  self.files          = []
  self.ignore_locales = configuration['ignore_locales'].split.map{ |locale| locale.to_s }
  configuration['files'].each do |file_id, file_path|
    self.files.push(TranslationFile.new(file_id, root_path + '/' + file_path, api_key))
  end
  set_locales
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



12
13
14
# File 'lib/web_translate_it/configuration.rb', line 12

def api_key
  @api_key
end

#filesObject

Returns the value of attribute files.



12
13
14
# File 'lib/web_translate_it/configuration.rb', line 12

def files
  @files
end

#ignore_localesObject

Returns the value of attribute ignore_locales.



12
13
14
# File 'lib/web_translate_it/configuration.rb', line 12

def ignore_locales
  @ignore_locales
end

#loggerObject

Returns a logger. If RAILS_DEFAULT_LOGGER is defined, use it, else, define a new logger.



46
47
48
# File 'lib/web_translate_it/configuration.rb', line 46

def logger
  @logger
end

#pathObject

Returns the value of attribute path.



12
13
14
# File 'lib/web_translate_it/configuration.rb', line 12

def path
  @path
end

#source_localeObject

Returns the value of attribute source_locale.



12
13
14
# File 'lib/web_translate_it/configuration.rb', line 12

def source_locale
  @source_locale
end

#target_localesObject

Returns the value of attribute target_locales.



12
13
14
# File 'lib/web_translate_it/configuration.rb', line 12

def target_locales
  @target_locales
end

Instance Method Details

#api_urlObject

Convenience method which returns the endpoint for fetching a list of locales for a project.



41
42
43
# File 'lib/web_translate_it/configuration.rb', line 41

def api_url
  "/api/projects/#{api_key}.yaml"
end

#set_localesObject

Makes an API request to fetch the list of the different locales for a project. Implementation example:

configuration = WebTranslateIt::Configuration.new
locales = configuration.locales # returns an array of locales: ['en', 'fr', 'es', ...]


33
34
35
36
37
38
# File 'lib/web_translate_it/configuration.rb', line 33

def set_locales
  project_info = YAML.load WebTranslateIt::Project.fetch_info(api_key)
  project = project_info['project']
  self.source_locale  = project['source_locale']['code']
  self.target_locales = project['target_locales'].map{|locale| locale['code']}
end