icapps-translations 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/icapps/translations.rb +16 -1
- data/lib/icapps/translations/cli.rb +6 -0
- data/lib/icapps/translations/configuration.rb +71 -0
- data/lib/icapps/translations/http.rb +32 -0
- data/lib/icapps/translations/strings.rb +55 -0
- data/lib/icapps/translations/version.rb +1 -1
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f2789aad0fad6e9661943abe943e73e9c041108
|
4
|
+
data.tar.gz: 76ead344d1b65d67a4063e4dd965cdfd50d1d80e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a9588daa950ca58aadf98aa03cc887537f8beba74db635116b1cab8a3c0d0d0790c9e25b8144706873ccc576f69060b2a00ff2c76d0d96cc7782571ee9c33aa
|
7
|
+
data.tar.gz: 623874001ad08991d3bb1d36a550f72fd17453a914c54dabfd0e580887029e986d624b8293129e0ee5d09774a2479a30912bbe46140780be44c860ea2f293ff8
|
data/lib/icapps/translations.rb
CHANGED
@@ -1,8 +1,23 @@
|
|
1
1
|
require "icapps/translations/version"
|
2
2
|
require "icapps/translations/cli"
|
3
|
+
require "icapps/translations/strings"
|
3
4
|
|
4
5
|
module Icapps
|
5
6
|
module Translations
|
6
|
-
|
7
|
+
class << self
|
8
|
+
attr_accessor :options
|
9
|
+
|
10
|
+
def config
|
11
|
+
@config ||= Configuration.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def import
|
15
|
+
# Validate the configuration file. Abort when invalid.
|
16
|
+
config.validate
|
17
|
+
|
18
|
+
# Import the strings files.
|
19
|
+
Strings.import
|
20
|
+
end
|
21
|
+
end
|
7
22
|
end
|
8
23
|
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'thor'
|
2
2
|
require 'colorize'
|
3
3
|
|
4
|
+
require "icapps/translations/configuration"
|
5
|
+
|
4
6
|
module Icapps
|
5
7
|
module Translations
|
6
8
|
class CLI < Thor
|
@@ -17,6 +19,8 @@ module Icapps
|
|
17
19
|
option :verbose, type: :boolean
|
18
20
|
def init
|
19
21
|
puts "[VERBOSE] Running the 'translations init command'.".colorize(:white) if options[:verbose]
|
22
|
+
::Icapps::Translations.options = options
|
23
|
+
::Icapps::Translations::Configuration.create
|
20
24
|
end
|
21
25
|
|
22
26
|
desc 'import', 'Import the translations into your project\'s .string files.'
|
@@ -28,6 +32,8 @@ module Icapps
|
|
28
32
|
option :verbose, type: :boolean
|
29
33
|
def import
|
30
34
|
puts "[VERBOSE] Running the 'translations import command'.".colorize(:white) if options[:verbose]
|
35
|
+
::Icapps::Translations.options = options
|
36
|
+
::Icapps::Translations.import
|
31
37
|
end
|
32
38
|
end
|
33
39
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'colorize'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
module Icapps
|
5
|
+
module Translations
|
6
|
+
class Configuration
|
7
|
+
attr :url
|
8
|
+
attr :filename
|
9
|
+
attr :project_key
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def create
|
13
|
+
if exists?
|
14
|
+
puts "[WARNING] Configuration already exists at '#{path}'.".colorize(:yellow)
|
15
|
+
return
|
16
|
+
end
|
17
|
+
|
18
|
+
File.open(path, "w") { |f| f.write(initial_content.to_yaml) }
|
19
|
+
puts "[MESSAGE] Configuration created at '#{path}'.".colorize(:green)
|
20
|
+
end
|
21
|
+
|
22
|
+
def exists?
|
23
|
+
File.exists?(path)
|
24
|
+
end
|
25
|
+
|
26
|
+
def path
|
27
|
+
File.join(Dir.pwd, filename)
|
28
|
+
end
|
29
|
+
|
30
|
+
def filename
|
31
|
+
'.translations'
|
32
|
+
end
|
33
|
+
|
34
|
+
def options
|
35
|
+
::Icapps::Translations.options
|
36
|
+
end
|
37
|
+
|
38
|
+
def initial_content
|
39
|
+
{
|
40
|
+
url: 'http://your_url.com',
|
41
|
+
filename: 'Localizable.strings',
|
42
|
+
project_key: 'YourProjectKey'
|
43
|
+
}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def initialize
|
48
|
+
read_config if Configuration.exists?
|
49
|
+
end
|
50
|
+
|
51
|
+
def validate
|
52
|
+
abort '[ERROR] You need to provide a project key in the .translations configuration file.'.colorize(:red) unless @project_key
|
53
|
+
abort '[ERROR] You need to provide an url in the .translations configuration file.'.colorize(:red) unless @url
|
54
|
+
abort '[ERROR] You need to provide a default filename in the .translations configuration file.'.colorize(:red) unless @filename
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def read_config
|
60
|
+
puts "[VERBOSE] Reading the config file at '#{Configuration.path}'.".colorize(:white) if Configuration.options[:verbose]
|
61
|
+
|
62
|
+
params = YAML::load File.open(Configuration.path)
|
63
|
+
if params
|
64
|
+
@filename = params[:filename]
|
65
|
+
@url = params[:url]
|
66
|
+
@project_key = params[:project_key]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'colorize'
|
2
|
+
require 'net/http'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Icapps
|
6
|
+
module Translations
|
7
|
+
class Http
|
8
|
+
class << self
|
9
|
+
def authenticated_response(path, is_json=false)
|
10
|
+
uri = URI("#{config.url}/#{path}")
|
11
|
+
puts "[VERBOSE] Connecting to url '#{uri}'.".colorize(:white) if options[:verbose]
|
12
|
+
|
13
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
14
|
+
request = Net::HTTP::Get.new(uri)
|
15
|
+
request.add_field "Authorization", "Token token=#{config.project_key}"
|
16
|
+
response = http.request(request)
|
17
|
+
is_json ? JSON.parse(response.body) : response.body
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def options
|
23
|
+
::Icapps::Translations.options
|
24
|
+
end
|
25
|
+
|
26
|
+
def config
|
27
|
+
::Icapps::Translations.config
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'colorize'
|
2
|
+
|
3
|
+
require "icapps/translations/http"
|
4
|
+
|
5
|
+
module Icapps
|
6
|
+
module Translations
|
7
|
+
class Strings
|
8
|
+
class << self
|
9
|
+
def import
|
10
|
+
puts "[VERBOSE] Importing translations from project with key #{options.project_key}".colorize(:white) if options[:verbose]
|
11
|
+
|
12
|
+
languages_json = fetch_languages
|
13
|
+
puts "[VERBOSE] There are currently #{languages_json.count} language(s) for this project.".colorize(:white) if options[:verbose]
|
14
|
+
|
15
|
+
languages_json.each { |language| fetch_language_strings_file language }
|
16
|
+
puts "[MESSAGE] Finished importing translation.".colorize(:green)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def options
|
22
|
+
::Icapps::Translations.options
|
23
|
+
end
|
24
|
+
|
25
|
+
def config
|
26
|
+
::Icapps::Translations.config
|
27
|
+
end
|
28
|
+
|
29
|
+
def fetch_languages
|
30
|
+
::Icapps::Translations::Http.authenticated_response('languages.json', true)
|
31
|
+
end
|
32
|
+
|
33
|
+
def fetch_language_strings_file(language)
|
34
|
+
short_name = language['short_name']
|
35
|
+
puts "[VERBOSE] Fetching #{short_name} translations.".colorize(:white) if options[:verbose]
|
36
|
+
string_files = Dir.glob("**/#{short_name}.lproj/#{config.filename}")
|
37
|
+
if string_files.count == 0
|
38
|
+
puts "[WARNING] No 'Localizable.string' file found for the #{short_name} language.".colorize(:yellow)
|
39
|
+
elsif string_files.count > 1
|
40
|
+
puts "[WARNING] Multiple 'Localizable.string' files found for the #{short_name} language.".colorize(:yellow)
|
41
|
+
else
|
42
|
+
strings = ::Icapps::Translations::Http.authenticated_response("translations/#{language['id']}.strings")
|
43
|
+
write_to_strings_file strings, string_files, language
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def write_to_strings_file(strings, string_files, language)
|
48
|
+
path = string_files.first
|
49
|
+
File.open(path, 'w+') { |file| file.puts strings }
|
50
|
+
puts "[VERBOSE] Written #{language['short_name']} translations to #{path}.".colorize(:green) if options[:verbose]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: icapps-translations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jelle Vandebeeck
|
@@ -83,6 +83,9 @@ files:
|
|
83
83
|
- icapps-translations.gemspec
|
84
84
|
- lib/icapps/translations.rb
|
85
85
|
- lib/icapps/translations/cli.rb
|
86
|
+
- lib/icapps/translations/configuration.rb
|
87
|
+
- lib/icapps/translations/http.rb
|
88
|
+
- lib/icapps/translations/strings.rb
|
86
89
|
- lib/icapps/translations/version.rb
|
87
90
|
homepage: https://github.com/fousa/icapps-translations
|
88
91
|
licenses:
|