Class: I18n::OneSky::SimpleClient

Inherits:
ClientBase show all
Includes:
Backend::Flatten
Defined in:
lib/i18n/one_sky/simple_client.rb

Overview

A class to deal with the OneSky apis it encapsulates the logic of I18n::Backend::ActiveRecord

Constant Summary

Constants inherited from ClientBase

ClientBase::SKIP_KEYS_REGEXP

Instance Attribute Summary

Attributes inherited from ClientBase

#client, #platform, #project

Instance Method Summary collapse

Methods inherited from ClientBase

config_exists?, from_config, from_env, #initialize, load_config, #platform_base_locale, #platform_details, #platform_locale_codes, #platform_locales, #verify!

Constructor Details

This class inherits a constructor from I18n::OneSky::ClientBase

Instance Method Details

#all_phrases(yaml_path) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/i18n/one_sky/simple_client.rb', line 46

def all_phrases(yaml_path)
  phrases = {}
  Dir.glob("#{yaml_path}/**/*.yml").each do |path|
    hash = YAML::load(File.read(path))
    phrases.merge!(hash[I18n.default_locale.to_s]) if hash.has_key?(I18n.default_locale.to_s)
  end
  flatten_phrases(phrases)
end

#download(yaml_path) ⇒ Object

Store all translations from OneSky in YAMl files.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/i18n/one_sky/simple_client.rb', line 12

def download(yaml_path)
  puts "Downloading translations for I18n Simple Backend:"

  platform_locales.each do |locale|
    locale_code  = locale["locale"]
    local_name   = locale["name"]["local"]
    english_name = locale["name"]["eng"]

    if locale_code == platform_base_locale
      # We skip the base locale.
      next
    else
      yaml = platform.translation.download_yaml(locale_code)

      yaml_file_name = "#{locale_code}_one_sky.yml"
      puts "  locale: #{locale_code}, file: #{yaml_file_name}"

      File.open(File.join(yaml_path, yaml_file_name), "w") do |f|
        f.puts "# PLEASE DO NOT EDIT THIS FILE."
        f.puts "# This was downloaded from OneSky. Log in to your OneSky account to manage translations on their website."
        f.puts "# Language code: #{locale_code}"
        f.puts "# Language name: #{local_name}"
        f.puts "# Language English name: #{english_name}"
        f.write yaml
      end
    end
  end
end

#upload(yaml_path) ⇒ Object

Scan all yaml files for keys in the default locale, and push them to OneSky.



42
43
44
# File 'lib/i18n/one_sky/simple_client.rb', line 42

def upload(yaml_path)
  upload_phrases(all_phrases(yaml_path))
end