zendesk_apps_support 1.6.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,7 @@ module ZendeskAppsSupport
4
4
  require 'zendesk_apps_support/engine'
5
5
 
6
6
  autoload :AppFile, 'zendesk_apps_support/app_file'
7
+ autoload :BuildTranslation, 'zendesk_apps_support/build_translation'
7
8
  autoload :I18n, 'zendesk_apps_support/i18n'
8
9
  autoload :Package, 'zendesk_apps_support/package'
9
10
  autoload :AppVersion, 'zendesk_apps_support/app_version'
@@ -0,0 +1,58 @@
1
+ module ZendeskAppsSupport
2
+ module BuildTranslation
3
+
4
+ I18N_TITLE_KEY = 'title'
5
+ I18N_VALUE_KEY = 'value'
6
+ I18N_KEYS = [ I18N_TITLE_KEY, I18N_VALUE_KEY ]
7
+
8
+ def to_flattened_namespaced_hash(hash, prefix = nil)
9
+
10
+ hash.inject({}) do |result, (key, value)|
11
+ key = [ prefix, key ].compact.join('.')
12
+
13
+ if value.kind_of?(Hash)
14
+
15
+ if is_translation_hash?(value)
16
+ result[key] = value[I18N_VALUE_KEY]
17
+ else
18
+ result.update( to_flattened_namespaced_hash(value, key) )
19
+ end
20
+
21
+ else
22
+ result[key] = value
23
+ end
24
+ result
25
+ end
26
+
27
+ end
28
+
29
+ def remove_zendesk_keys(scope, translations = {})
30
+
31
+ scope.each_key do |key|
32
+ context = scope[key]
33
+
34
+ if context.is_a?(Hash)
35
+
36
+ if is_translation_hash?(context)
37
+ translations[key] = context[I18N_VALUE_KEY]
38
+ else
39
+ translations[key] ||= {}
40
+ translations[key] = remove_zendesk_keys(context, translations[key])
41
+ end
42
+
43
+ else
44
+ translations[key] = context
45
+ end
46
+ end
47
+
48
+ translations
49
+ end
50
+
51
+ private
52
+
53
+ def is_translation_hash?(hash)
54
+ hash.keys.sort == I18N_KEYS
55
+ end
56
+
57
+ end
58
+ end
@@ -4,6 +4,7 @@ require 'json'
4
4
 
5
5
  module ZendeskAppsSupport
6
6
  class Package
7
+ include ZendeskAppsSupport::BuildTranslation
7
8
 
8
9
  DEFAULT_LAYOUT = Erubis::Eruby.new( File.read(File.expand_path('../assets/default_template.html.erb', __FILE__)) )
9
10
  DEFAULT_SCSS = File.read(File.expand_path('../assets/default_styles.scss', __FILE__))
@@ -40,6 +41,14 @@ module ZendeskAppsSupport
40
41
  JSON.parse(File.read(File.join(root, "manifest.json")), :symbolize_names => true)
41
42
  end
42
43
 
44
+ def translations
45
+ JSON.parse(File.read(File.join(root, "translations/en.json")))
46
+ end
47
+
48
+ def app_translations
49
+ remove_zendesk_keys(translations)
50
+ end
51
+
43
52
  def readified_js(app_name, app_id, asset_url_prefix, settings={})
44
53
  manifest = manifest_json
45
54
  source = File.read(File.join(root, "app.js"))
@@ -47,7 +56,6 @@ module ZendeskAppsSupport
47
56
  location = manifest[:location]
48
57
  app_class_name = "app-#{app_id}"
49
58
  author = manifest[:author]
50
- translations = JSON.parse(File.read(File.join(root, "translations/en.json")))
51
59
  framework_version = manifest[:frameworkVersion]
52
60
  templates = manifest[:noTemplate] ? {} : compiled_templates(app_id, asset_url_prefix)
53
61
 
@@ -60,7 +68,7 @@ module ZendeskAppsSupport
60
68
  :asset_url_prefix => asset_url_prefix,
61
69
  :app_class_name => app_class_name,
62
70
  :author => author,
63
- :translations => translations,
71
+ :translations => app_translations,
64
72
  :framework_version => framework_version,
65
73
  :templates => templates,
66
74
  :settings => settings,
@@ -0,0 +1,104 @@
1
+ require 'zendesk_apps_support'
2
+ include ZendeskAppsSupport::BuildTranslation
3
+
4
+ describe ZendeskAppsSupport::BuildTranslation do
5
+
6
+ let(:en_json) {
7
+ {
8
+ "app" => {
9
+ "description" => "Shows related tickets",
10
+ "title" => "Related tickets",
11
+ "parameters" => {
12
+ "disable_tooltip" => {
13
+ "label" => "Disable tooltip"
14
+ }
15
+ }
16
+ },
17
+ "parent_without_child" => "Foo",
18
+ "parent_with_child" => {
19
+ "child" => "Foo"
20
+ },
21
+ "parent_with_child_title" => {
22
+ "title" => "Foo"
23
+ },
24
+ "parent_with_zendesk_tranlation" => {
25
+ "title" => "Bar",
26
+ "value" => "Foo"
27
+ },
28
+ "parent_with_child_title_nested" => {
29
+ "title" => {
30
+ "value" => "Foo"
31
+ }
32
+ },
33
+ "parent_with_nested_invalid_zendesk_translation" => {
34
+ "title" => {
35
+ "title" => "Bar",
36
+ "desc" => "Foo"
37
+ }
38
+ },
39
+ "parent_with_nested_zendesk_translation" => {
40
+ "title" => {
41
+ "title" => "Bar",
42
+ "value" => "Foo"
43
+ }
44
+ }
45
+ }
46
+ }
47
+
48
+ describe '#market_translations' do
49
+
50
+ context "flatten translations" do
51
+
52
+ before do
53
+ @market_translations = to_flattened_namespaced_hash(en_json)
54
+ end
55
+
56
+ it "value is correct parent with a child that has an valid nested zendesk translation" do
57
+ @market_translations['parent_with_nested_zendesk_translation.title'].should eq 'Foo'
58
+ end
59
+
60
+ it "value is correct parent with a child that has an invalid nested zendesk translation" do
61
+ @market_translations['parent_with_nested_invalid_zendesk_translation.title.title'].should eq 'Bar'
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+
68
+ describe '#app_translations' do
69
+
70
+ context "reformat translations" do
71
+
72
+ before do
73
+ @app_translations = remove_zendesk_keys(en_json)
74
+ end
75
+
76
+ it "value is correct parent without a child" do
77
+ @app_translations['parent_without_child'].should eq 'Foo'
78
+ end
79
+
80
+ it "value is correct parent with a child" do
81
+ @app_translations['parent_with_child']['child'].should eq 'Foo'
82
+ end
83
+
84
+ it "value is correct parent with a child with a title" do
85
+ @app_translations['parent_with_child_title']['title'].should eq 'Foo'
86
+ end
87
+
88
+ it "value is correct parent with a zendesk translation" do
89
+ @app_translations['parent_with_child_title_nested']['title']['value'].should eq 'Foo'
90
+ end
91
+
92
+ it "value is correct parent with a child that has a nested zendesk translation" do
93
+ @app_translations['parent_with_nested_zendesk_translation']['title'].should eq 'Foo'
94
+ end
95
+
96
+ it "value is correct parent with a child that has an invalid nested zendesk translation" do
97
+ @app_translations['parent_with_nested_invalid_zendesk_translation']['title'].should eq({ "title" => "Bar", "desc" => "Foo" })
98
+ end
99
+
100
+ end
101
+
102
+ end
103
+
104
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zendesk_apps_support
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-18 00:00:00.000000000 Z
15
+ date: 2013-10-28 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: i18n
@@ -154,6 +154,7 @@ files:
154
154
  - lib/zendesk_apps_support/assets/default_styles.scss
155
155
  - lib/zendesk_apps_support/assets/default_template.html.erb
156
156
  - lib/zendesk_apps_support/assets/src.js.erb
157
+ - lib/zendesk_apps_support/build_translation.rb
157
158
  - lib/zendesk_apps_support/engine.rb
158
159
  - lib/zendesk_apps_support/i18n.rb
159
160
  - lib/zendesk_apps_support/package.rb
@@ -179,6 +180,7 @@ files:
179
180
  - spec/app/translations/en.json
180
181
  - spec/app_file_spec.rb
181
182
  - spec/app_version_spec.rb
183
+ - spec/build_translation_spec.rb
182
184
  - spec/i18n_spec.rb
183
185
  - spec/package_spec.rb
184
186
  - spec/validations/jshint_error_spec.rb
@@ -223,6 +225,7 @@ test_files:
223
225
  - spec/app/translations/en.json
224
226
  - spec/app_file_spec.rb
225
227
  - spec/app_version_spec.rb
228
+ - spec/build_translation_spec.rb
226
229
  - spec/i18n_spec.rb
227
230
  - spec/package_spec.rb
228
231
  - spec/validations/jshint_error_spec.rb