zendesk_apps_tools 1.6.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,35 +2,58 @@ require 'thor'
2
2
  require 'json'
3
3
  require 'zendesk_apps_tools/common'
4
4
  require 'zendesk_apps_tools/locale_identifier'
5
+ require 'zendesk_apps_support'
6
+ require 'yaml'
5
7
 
6
8
  module ZendeskAppsTools
7
9
  class Translate < Thor
8
10
  include Thor::Shell
9
11
  include Thor::Actions
10
12
  include ZendeskAppsTools::Common
13
+ include ZendeskAppsSupport::BuildTranslation
11
14
 
12
- CHARACTERS_TO_ESCAPE = %w[ " ]
13
15
  LOCALE_ENDPOINT = "https://support.zendesk.com/api/v2/locales/agent.json"
14
16
 
15
- desc 'create', 'Create Zendesk translation file from en.json'
16
- def create
17
- manifest = JSON.parse(File.open('manifest.json').read)
17
+ desc 'to_yml', 'Create Zendesk translation file from en.json'
18
+ method_option :path, :default => './', :required => false
19
+ def to_yml
20
+ setup_path(options[:path]) if options[:path]
21
+ manifest = JSON.parse(File.open("#{destination_root}/manifest.json").read)
18
22
  app_name = manifest['name']
19
23
 
20
24
  unless app_name
21
25
  app_name = get_value_from_stdin('What is the name of this app?', :error_msg => "Invalid name, try again:")
22
26
  end
23
27
 
24
- package = get_value_from_stdin('What is the package name for this app?', :valid_regex => /^[a-z_]+$/, :error_msg => "Invalid package name, try again:")
28
+ en_json = JSON.parse(File.open("#{destination_root}/translations/en.json").read)
25
29
 
26
- write_yaml(app_name, package)
30
+ package = en_json["app"]["package"]
31
+ say('No package defined inside en.json! Abort.', :red) and exit 1 unless package
32
+ en_json["app"].delete("package")
33
+
34
+ write_yml(en_json, app_name, package)
35
+ end
36
+
37
+ desc 'to_json', 'Convert Zendesk translation yml to I18n formatted json'
38
+ method_option :path, :default => './', :required => false
39
+ def to_json
40
+ setup_path(options[:path]) if options[:path]
41
+ en_yml = YAML.load_file("#{destination_root}/translations/en.yml")
42
+ en_yml['parts'][0]['translation']['key'] =~ /^txt.apps.([^\.]+)/
43
+ package = $1
44
+ translations = en_yml['parts'].map { |part| part['translation'] }
45
+ en_json = array_to_nested_hash(translations)['txt']['apps'][package]
46
+ en_json['app']['package'] = package
47
+
48
+ write_json("translations/en.json", en_json)
27
49
  end
28
50
 
29
51
  desc 'update', 'Update translation files from Zendesk'
52
+ method_option :path, :default => './', :required => false
30
53
  def update(request_builder = Faraday.new)
54
+ setup_path(options[:path]) if options[:path]
31
55
  app_package = get_value_from_stdin("What is the package name for this app? (without app_)", :valid_regex => /^[a-z_]+$/, :error_msg => "Invalid package name, try again:")
32
56
 
33
- user = "#{user}/token"
34
57
  key_prefix = "txt.apps.#{app_package}."
35
58
 
36
59
  say("Fetching translations...")
@@ -40,12 +63,12 @@ module ZendeskAppsTools
40
63
  locales = JSON.parse(locale_response.body)["locales"]
41
64
 
42
65
  locales.each do |locale|
43
- locale_url = "#{locale["url"]}?include=translations&packages=app_#{app_package}"
66
+ locale_url = "#{locale["url"]}?include=translations&packages=app_#{app_package}"
44
67
  locale_response = api_request(locale_url, request_builder).body
45
- translations = JSON.parse(locale_response)['locale']['translations']
68
+ translations = JSON.parse(locale_response)['locale']['translations']
46
69
 
47
70
  locale_name = ZendeskAppsTools::LocaleIdentifier.new(locale['locale']).language_id
48
- write_json(locale_name, nest_translations_hash(translations, key_prefix))
71
+ write_json("#{destination_root}/translations/#{locale_name}.json", nest_translations_hash(translations, key_prefix))
49
72
  end
50
73
  say("Translations updated", :green)
51
74
 
@@ -60,17 +83,21 @@ module ZendeskAppsTools
60
83
 
61
84
  no_commands do
62
85
 
63
- def write_json(locale_name, translations_hash)
64
- create_file("translations/#{locale_name}.json", JSON.pretty_generate(translations_hash))
86
+ def setup_path(path)
87
+ @destination_stack << relative_to_original_destination_root(path) unless @destination_stack.last == path
88
+ end
89
+
90
+ def write_json(filename, translations_hash)
91
+ create_file(filename, JSON.pretty_generate(translations_hash))
65
92
  end
66
93
 
67
94
  def nest_translations_hash(translations_hash, key_prefix)
68
95
  result = {}
69
96
 
70
97
  translations_hash.each do |full_key, value|
71
- parts = full_key.gsub(key_prefix, '').split('.')
98
+ parts = full_key.gsub(key_prefix, '').split('.')
72
99
  parts_count = parts.size - 1
73
- context = result
100
+ context = result
74
101
 
75
102
  parts.each_with_index do |part, i|
76
103
 
@@ -86,52 +113,30 @@ module ZendeskAppsTools
86
113
  result
87
114
  end
88
115
 
89
- def write_yaml(app_name, package)
90
- user_translations = JSON.parse(File.open('translations/en.json').read)
91
- translations = user_translations.keys.inject({}) do |translations, key|
92
- translations.merge( get_translations_for(user_translations, key) )
93
- end
94
- @escaped_translations = escape_values(translations)
95
-
96
- @app_name = app_name
97
- @package_name = package
116
+ def write_yml(en_json, app_name, package_name)
117
+ titles = to_flattened_namespaced_hash(en_json, I18N_TITLE_KEY)
118
+ values = to_flattened_namespaced_hash(en_json, I18N_VALUE_KEY)
119
+ @translations = titles.each { |k, v| titles[k] = {"title" => v, "value" => escape_special_characters(values[k]) }}
120
+ @app_name = app_name
121
+ @package_name = package_name
98
122
  template(File.join(Translate.source_root, 'templates/translation.erb.tt'), "translations/en.yml")
99
123
  end
100
124
 
101
- def escape_values(translations)
102
- result = {}
103
- translations.each do |key, value|
104
- CHARACTERS_TO_ESCAPE.each do |char|
105
- result[key] = value.gsub(char, "\\#{char}")
106
- end
107
- end
108
-
109
- result
125
+ def escape_special_characters(v)
126
+ v.gsub('"', '\"')
110
127
  end
111
128
 
112
- def get_translations_for(scope, scope_key, keys = [], translations = {})
113
- hash_or_value = scope[scope_key]
114
-
115
- if hash_or_value.is_a?(Hash)
116
- keys << scope_key
117
- hash_or_value.each_key do |key|
118
-
119
- if hash_or_value[key].is_a?(Hash)
120
- get_translations_for(hash_or_value, key, keys, translations)
121
- keys = keys[0...-1]
122
- else
123
- translation_key = (keys + [key]).join('.')
124
- translations[translation_key] = hash_or_value[key]
125
- end
126
-
129
+ def array_to_nested_hash(array)
130
+ array.inject({}) do |result, item|
131
+ keys = item['key'].split('.')
132
+ current = result
133
+ keys[0..-2].each do |key|
134
+ current = (current[key] ||= {})
127
135
  end
128
- else
129
- translations[scope_key] = hash_or_value
136
+ current[keys[-1]] = {'title' => item['title'], 'value' => item['value']}
137
+ result
130
138
  end
131
-
132
- translations
133
139
  end
134
-
135
140
  end
136
141
  end
137
142
  end
@@ -5,9 +5,9 @@ packages:
5
5
  - app_<%= @package_name %>
6
6
 
7
7
  parts:
8
- <% @escaped_translations.each do |key, value| -%>
8
+ <% @translations.each do |key, title_value| -%>
9
9
  - translation:
10
10
  key: "txt.apps.<%= @package_name %>.<%= key %>"
11
- title: ""
12
- value: "<%= value %>"
13
- <% end %>
11
+ title: "<%= title_value["title"] %>"
12
+ value: "<%= title_value["value"] %>"
13
+ <% end %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zendesk_apps_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2013-10-28 00:00:00.000000000 Z
15
+ date: 2013-11-28 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: thor
@@ -85,7 +85,7 @@ dependencies:
85
85
  requirements:
86
86
  - - ~>
87
87
  - !ruby/object:Gem::Version
88
- version: 1.7.0
88
+ version: 1.7.1
89
89
  type: :runtime
90
90
  prerelease: false
91
91
  version_requirements: !ruby/object:Gem::Requirement
@@ -93,7 +93,7 @@ dependencies:
93
93
  requirements:
94
94
  - - ~>
95
95
  - !ruby/object:Gem::Version
96
- version: 1.7.0
96
+ version: 1.7.1
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: cucumber
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -191,7 +191,6 @@ files:
191
191
  - features/package.feature
192
192
  - features/step_definitions/app_steps.rb
193
193
  - features/support/env.rb
194
- - features/translate.feature
195
194
  - features/validate.feature
196
195
  homepage: http://github.com/zendesk/zendesk_apps_tools
197
196
  licenses:
@@ -225,5 +224,4 @@ test_files:
225
224
  - features/package.feature
226
225
  - features/step_definitions/app_steps.rb
227
226
  - features/support/env.rb
228
- - features/translate.feature
229
227
  - features/validate.feature
@@ -1,36 +0,0 @@
1
- Feature: Translate app strings
2
- As a Zendesk admin
3
- To enable many languages
4
- I want to be able to map between json and yaml
5
-
6
- Scenario: Attempt to generate invalid package name
7
- Given an app is created in directory "tmp/aruba"
8
- When I run "cd tmp/aruba && zat translate create" command with the following details:
9
- | package name | This is wrong |
10
- Then the command output should contain "Invalid package name, try again:"
11
-
12
- Scenario: Generate template yaml from en.json
13
- Given an app is created in directory "tmp/aruba"
14
- And the fixture "quote_character_translation.json" is used for "translations/en.json"
15
- When I run "cd tmp/aruba && zat translate create" command with the following details:
16
- | package name | test_package |
17
- Then the app file "tmp/aruba/translations/en.yml" is created with:
18
- """
19
- ---
20
- title: "John Test App"
21
- packages:
22
- - default
23
- - app_test_package
24
-
25
- parts:
26
- - translation:
27
- key: "txt.apps.test_package.app.description"
28
- title: ""
29
- value: "Play the famous zen tunes in your help desk."
30
- - translation:
31
- key: "txt.apps.test_package.app.name"
32
- title: ""
33
- value: "Buddha \"Machine\""
34
-
35
- """
36
-