Class: GetText::OneSky::SimpleClient

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext-one_sky/simple_client.rb

Overview

This class is the bridge between the OneSky service and the gettext file storgage.

It takes the phrases defined in gettext's default locale and uploads them to OneSky for translation.
Then it downloads available translations and saves them as .po files.

A regular workflow would then look like:
  initialize -> load_phrases -> upload_phrases -> download_translations

* Initilaize *

When you initialize a client inside a Rails project, 
it will take the OneSky configuration variables supplied when you called rails generate one_sky:init.

When you use this client outside of Rails,
credentials are expected to come from environment variables: ONESKY_API_KEY, ONESKY_API_SECRET, ONESKY_PROJECT.
You can override these defaults by providing a hash of options:
  {:api_key => ..., :api_secret => ..., :project => ...}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SimpleClient

Returns a new instance of SimpleClient.



31
32
33
34
35
36
# File 'lib/gettext-one_sky/simple_client.rb', line 31

def initialize(options = {})
  options = default_options.merge!(options)
  @project = ::OneSky::Project.new(options[:api_key], options[:api_secret], options[:project])
  @one_sky_locale = @project.details["base_locale"]
  @one_sky_languages = @project.languages
end

Instance Attribute Details

#phrases_flatObject (readonly)

Returns the value of attribute phrases_flat.



27
28
29
# File 'lib/gettext-one_sky/simple_client.rb', line 27

def phrases_flat
  @phrases_flat
end

#phrases_nestedObject (readonly)

Returns the value of attribute phrases_nested.



27
28
29
# File 'lib/gettext-one_sky/simple_client.rb', line 27

def phrases_nested
  @phrases_nested
end

#projectObject (readonly)

The base OneSky project. Gives you low-level access to the API gem.



29
30
31
# File 'lib/gettext-one_sky/simple_client.rb', line 29

def project
  @project
end

#translations_flatObject (readonly)

Returns the value of attribute translations_flat.



27
28
29
# File 'lib/gettext-one_sky/simple_client.rb', line 27

def translations_flat
  @translations_flat
end

#translations_from_oneskyObject (readonly)

Returns the value of attribute translations_from_onesky.



27
28
29
# File 'lib/gettext-one_sky/simple_client.rb', line 27

def translations_from_onesky
  @translations_from_onesky
end

Instance Method Details

#download_translationsObject

Download all available translations from Onesky server



77
78
79
# File 'lib/gettext-one_sky/simple_client.rb', line 77

def download_translations
  @translations_from_onesky = @project.output
end

#load_phrases(path = nil) ⇒ Object

Parse and load phrases from .pot file. If not a Rails project, manually supply the path where the .pot file located.



40
41
42
43
44
45
46
# File 'lib/gettext-one_sky/simple_client.rb', line 40

def load_phrases(path=nil)
  @phrases_flat = []
  
  Dir.glob("#{po_dir_path(path)}/**/*.pot").each do |file_path|
    @phrases_flat += parse_phrase_file(file_path)
  end
end

#load_translations(path = nil) ⇒ Object

Parse and load translated phrases from .po files If not a Rails project, manually supply the path where the .po file path located.



57
58
59
60
61
62
63
64
65
# File 'lib/gettext-one_sky/simple_client.rb', line 57

def load_translations(path=nil)
  @translations_flat = []
  
  Dir.glob("#{po_dir_path(path)}/**/*.po").each do |file_path|
    lang_code = File.dirname(file_path).split("/").last
    
    @translations_flat += parse_phrase_file(file_path, lang_code)
  end
end

#save_translations(path = nil) ⇒ Object

Download all available translations from Onesky server and save them as *.po files.



82
83
84
85
86
# File 'lib/gettext-one_sky/simple_client.rb', line 82

def save_translations(path=nil)
  download_translations unless @translations_from_onesky
  
  update_translation_files_from_onesky(po_dir_path(path), @translations_from_onesky)
end

#upload_phrases(path = nil) ⇒ Object

Upload phrases to Onesky server



49
50
51
52
53
# File 'lib/gettext-one_sky/simple_client.rb', line 49

def upload_phrases(path = nil)
  load_phrases(path) unless @phrases_flat

  @project.input_bulk(@phrases_flat, :page => @phrases_flat.first[:page])
end

#upload_translations(path = nil) ⇒ Object

Upload translated phrases to Onesky server



68
69
70
71
72
73
74
# File 'lib/gettext-one_sky/simple_client.rb', line 68

def upload_translations(path=nil)
  load_translations(path) unless @translations_flat

  @translations_flat.each do |values|
    @project.send(:post, "/string/translate", values)
  end
end