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



52
53
54
55
56
57
58
59
# File 'lib/i18n/one_sky/simple_client.rb', line 52

def all_phrases(yaml_path)
  phrases = {}
  Dir.glob("#{yaml_path}/**/*.yml").each do |path|
    hash = YAML::load(File.read(path))
    phrases.deep_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
40
41
42
43
44
45
# 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"
      yaml.force_encoding('utf-8')

      if yaml.empty?
        puts "  locale: #{locale_code} - not downloading because it is empty (the old one would be deleted)"
        File.delete(yaml_file_name) if File.exist?(yaml_file_name)
      else
        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
end

#upload(yaml_path) ⇒ Object

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



48
49
50
# File 'lib/i18n/one_sky/simple_client.rb', line 48

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