ad_localize 3.1.0 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9510eed71bfba2c01f047d80707eaaeffbe24d6fefc4f65927b1afe8e44c2a49
4
- data.tar.gz: 3aba4a56d7f7b5fd0da6056471c55cf025a9133dd3d5a815fe06ac89a662d645
3
+ metadata.gz: b67ba9ff725d7fbf76fd0d680b07573648ee51966fdbb8429e6a26a764fc5563
4
+ data.tar.gz: 7ce5a08e27d50cbedd1ca1f4ffac9af1817ee8c38e97eb088d6dd7bc6d8ec5d0
5
5
  SHA512:
6
- metadata.gz: b87614c092bd2c6fcee5deeba77add9035f01c4861d526081e91606e6613827f4199d2fccb1f69aa7149f6c7a2755536d7e7a2b5d50901455b519fc20a98d695
7
- data.tar.gz: 22b0cc2b7b3a6290017b7cdd9e8bb0a7bb30b662bdeada9669d9fd10a670af0f3d4e99919fca9023b0cd289d0ad254d45e14dc7d7fdc20f0b6fac25f92017eca
6
+ metadata.gz: 0eb0c873e0d27062cc4291baab1c330224997ef3085b7474424c50d0dff8f089c40ae9f668e91f5769ce6d7c69431892435ee65a5a3073815ddbbe9bcde539be
7
+ data.tar.gz: 7998404ef9a406c7421117d666fae0c3aa8bfa0f55a5fe4e5a207a7c6bc01d370de3158a47910d47205d73a266a35109ca819cf114b572fe848061454e5fad29
@@ -4,9 +4,19 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [3.2.0] - 2019-12-10
8
+ ### Added
9
+ - Add tests to compare reference exports by [@felginep](https://github.com/felginep)
10
+ - [iOS only] Handle [adaptive strings](https://developer.apple.com/documentation/foundation/nsstring/1413104-variantfittingpresentationwidth) by [@felginep](https://github.com/felginep)
11
+
12
+ ### Changed
13
+ - Fix issue with Info.plist format by [@felginep](https://github.com/felginep)
14
+ - Do not export plurals for android when there are no values by [@felginep](https://github.com/felginep)
15
+
7
16
  ## [3.1.0] - 2019-09-30
8
17
  ### Added
9
18
  - [iOS only] Info.plist generation support by [@felginep](https://github.com/felginep)
19
+
10
20
  ### Changed
11
21
  - No more warning log for empty lines by [@felginep](https://github.com/felginep)
12
22
 
@@ -27,4 +37,3 @@ TODO
27
37
 
28
38
  ## [0.1.0] - 2016-04-25
29
39
  TODO
30
-
@@ -42,6 +42,7 @@ Gem::Specification.new do |spec|
42
42
  spec.add_development_dependency 'minitest', '~> 5.11'
43
43
  spec.add_development_dependency 'byebug', '~> 11.0'
44
44
  spec.add_development_dependency 'minitest-reporters', '~> 1.3'
45
+ spec.add_development_dependency 'diffy', '~> 3.3'
45
46
 
46
47
  spec.add_dependency 'activesupport', '~> 4.2' ,'>= 4.2.10' # Fastlane does not support activesupport 5
47
48
  spec.add_dependency 'nokogiri', '~> 1.8', '>= 1.8.2'
@@ -2,6 +2,7 @@ module AdLocalize
2
2
  module Constant
3
3
  SUPPORTED_PLATFORMS = %w(ios android yml json)
4
4
  PLURAL_KEY_SYMBOL = :plural
5
+ ADAPTIVE_KEY_SYMBOL = :adaptive
5
6
  SINGULAR_KEY_SYMBOL = :singular
6
7
  COMMENT_KEY_SYMBOL = :comment
7
8
  INFO_PLIST_KEY_SYMBOL = :info_plist
@@ -1,7 +1,8 @@
1
1
  module AdLocalize
2
2
  class CsvParser
3
3
  CSV_WORDING_KEYS_COLUMN = "key"
4
- PLURAL_KEY_REGEXP = /\#\#\{(\w+)\}/
4
+ PLURAL_KEY_REGEXP = /\#\#\{([A-Za-z]+)\}/
5
+ ADAPTIVE_KEY_REGEXP = /\#\#\{(\d+)\}/
5
6
  INFO_PLIST_KEY_REGEXP = /(NS.+UsageDescription)|(CF.+Name)/ # see https://developer.apple.com/documentation/bundleresources/information_property_list
6
7
 
7
8
  attr_accessor :locales
@@ -78,6 +79,11 @@ module AdLocalize
78
79
  "[#{locale.upcase}] Plural key ---> plural_identifier : #{key_infos.dig(:plural_identifier)}, key : #{current_key}",
79
80
  "[#{locale.upcase}] Missing translation for #{current_key} (#{key_infos.dig(:plural_identifier)})")
80
81
  value = { key_infos.dig(:plural_identifier) => row[locale] || default_wording(locale, "#{current_key} (#{key_infos.dig(:plural_identifier)})") }
82
+ elsif key_infos.dig(:numeral_key) == Constant::ADAPTIVE_KEY_SYMBOL
83
+ trace_wording(row[locale],
84
+ "[#{locale.upcase}] Adaptive key ---> adaptive_identifier : #{key_infos.dig(:adaptive_identifier)}, key : #{current_key}",
85
+ "[#{locale.upcase}] Missing translation for #{current_key} (#{key_infos.dig(:adaptive_identifier)})")
86
+ value = { key_infos.dig(:adaptive_identifier) => row[locale] || default_wording(locale, "#{current_key} (#{key_infos.dig(:adaptive_identifier)})") }
81
87
  elsif key_infos.dig(:numeral_key) == Constant::INFO_PLIST_KEY_SYMBOL
82
88
  trace_wording(row[locale], "[#{locale.upcase}] Info.plist key ---> #{current_key}", "[#{locale.upcase}] Missing translation for #{current_key}")
83
89
  value = row[locale] || default_wording(locale, current_key)
@@ -98,22 +104,31 @@ module AdLocalize
98
104
  plural_identifier = nil
99
105
  invalid_plural = false
100
106
 
101
- if plural_prefix.nil?
102
- if key.match(INFO_PLIST_KEY_REGEXP).nil?
103
- numeral_key = Constant::SINGULAR_KEY_SYMBOL
104
- else
105
- numeral_key = Constant::INFO_PLIST_KEY_SYMBOL
106
- end
107
- else
107
+ adaptive_prefix = key.match(ADAPTIVE_KEY_REGEXP)
108
+ adaptive_identifier = nil
109
+ invalid_adaptive = false
110
+
111
+ if plural_prefix != nil
108
112
  numeral_key = Constant::PLURAL_KEY_SYMBOL
109
113
  key = plural_prefix.pre_match
110
114
  plural_identifier = plural_prefix.captures&.first
111
115
  LOGGER.log(:debug, :red, "Invalid key #{key}") if key.nil?
112
116
  LOGGER.log(:debug, :red, "Empty plural prefix!") if plural_identifier.nil?
113
117
  invalid_plural = plural_identifier.nil?
118
+ elsif adaptive_prefix != nil
119
+ numeral_key = Constant::ADAPTIVE_KEY_SYMBOL
120
+ key = adaptive_prefix.pre_match
121
+ adaptive_identifier = adaptive_prefix.captures&.first
122
+ LOGGER.log(:debug, :red, "Invalid key #{key}") if key.nil?
123
+ LOGGER.log(:debug, :red, "Empty adaptive prefix!") if adaptive_identifier.nil?
124
+ invalid_adaptive = adaptive_identifier.nil?
125
+ elsif key.match(INFO_PLIST_KEY_REGEXP) != nil
126
+ numeral_key = Constant::INFO_PLIST_KEY_SYMBOL
127
+ else
128
+ numeral_key = Constant::SINGULAR_KEY_SYMBOL
114
129
  end
115
130
 
116
- (key.nil? or invalid_plural) ? nil : { key: key.to_sym, numeral_key: numeral_key, plural_identifier: plural_identifier&.to_sym }
131
+ (key.nil? or invalid_plural or invalid_adaptive) ? nil : { key: key.to_sym, numeral_key: numeral_key, plural_identifier: plural_identifier&.to_sym, adaptive_identifier: adaptive_identifier&.to_sym }
117
132
  end
118
133
 
119
134
  def default_wording(locale, key)
@@ -57,7 +57,7 @@ module AdLocalize::Platform
57
57
  add_wording_text_to_xml(plural_text, xml)
58
58
  }
59
59
  end
60
- }
60
+ } unless plural_hash.values.all? &:empty?
61
61
  end
62
62
 
63
63
  def add_wording_text_to_xml(wording_text, xml)
@@ -6,7 +6,13 @@ module AdLocalize::Platform
6
6
 
7
7
  def export(locale, data, export_extension = nil, substitution_format = nil)
8
8
  create_locale_dir(locale)
9
- [AdLocalize::Constant::PLURAL_KEY_SYMBOL, AdLocalize::Constant::SINGULAR_KEY_SYMBOL, AdLocalize::Constant::INFO_PLIST_KEY_SYMBOL].each do |numeral_key|
9
+ all_symbols = [
10
+ AdLocalize::Constant::PLURAL_KEY_SYMBOL,
11
+ AdLocalize::Constant::ADAPTIVE_KEY_SYMBOL,
12
+ AdLocalize::Constant::SINGULAR_KEY_SYMBOL,
13
+ AdLocalize::Constant::INFO_PLIST_KEY_SYMBOL
14
+ ]
15
+ all_symbols.each do |numeral_key|
10
16
  numeral_data = data.select {|key, wording| wording.dig(locale.to_sym)&.key? numeral_key}
11
17
  if numeral_data.empty?
12
18
  AdLocalize::LOGGER.log(:info, :black, "[#{locale.upcase}] no #{numeral_key.to_s} keys were found to generate the file")
@@ -36,6 +42,54 @@ module AdLocalize::Platform
36
42
  )
37
43
  end
38
44
 
45
+ def write_plural(locale, plurals)
46
+ locale = locale.to_sym
47
+
48
+ append_to_localizable_plist(locale) do |xml|
49
+ plurals.each do |wording_key, translations|
50
+ xml.key wording_key
51
+ xml.dict {
52
+ xml.key "NSStringLocalizedFormatKey"
53
+ xml.string "%\#@key@"
54
+ xml.key "key"
55
+ xml.dict {
56
+ xml.key "NSStringFormatSpecTypeKey"
57
+ xml.string "NSStringPluralRuleType"
58
+ xml.key "NSStringFormatValueTypeKey"
59
+ xml.string "d"
60
+ translations[locale][AdLocalize::Constant::PLURAL_KEY_SYMBOL].each do |wording_type, wording_value|
61
+ xml.key wording_type
62
+ xml.string wording_value
63
+ end
64
+ }
65
+ }
66
+ end
67
+ end
68
+ AdLocalize::LOGGER.log(:debug, :black, "iOS plural [#{locale}] ---> DONE!")
69
+ end
70
+
71
+ def write_adaptive(locale, adaptives)
72
+ locale = locale.to_sym
73
+
74
+ append_to_localizable_plist(locale) do |xml|
75
+ adaptives.each do |wording_key, translations|
76
+ xml.key wording_key
77
+ xml.dict {
78
+ xml.key "NSStringVariableWidthRuleType"
79
+ xml.dict {
80
+ translations[locale][AdLocalize::Constant::ADAPTIVE_KEY_SYMBOL].each do |wording_type, wording_value|
81
+ xml.key wording_type
82
+ xml.string wording_value
83
+ end
84
+ }
85
+ }
86
+ end
87
+ end
88
+ AdLocalize::LOGGER.log(:debug, :black, "iOS adaptive [#{locale}] ---> DONE!")
89
+ end
90
+
91
+ private
92
+
39
93
  def write_localizable(locale:, singulars:, wording_type:, filename:)
40
94
  locale = locale.to_sym
41
95
 
@@ -43,7 +97,12 @@ module AdLocalize::Platform
43
97
  value = locales_wording.dig(locale, wording_type)
44
98
  comment = locales_wording.dig(locale, AdLocalize::Constant::COMMENT_KEY_SYMBOL)
45
99
  export_dir(locale).join(filename).open("a") do |file|
46
- line = "\"#{key}\" = \"#{value}\";"
100
+ line = ""
101
+ if wording_type == AdLocalize::Constant::INFO_PLIST_KEY_SYMBOL
102
+ line = %(#{key} = "#{value}";)
103
+ else
104
+ line = %("#{key}" = "#{value}";)
105
+ end
47
106
  line << " // #{comment}" unless comment.nil?
48
107
  line << "\n"
49
108
  file.puts line
@@ -52,37 +111,28 @@ module AdLocalize::Platform
52
111
  AdLocalize::LOGGER.log(:debug, :black, "iOS #{wording_type} [#{locale}] ---> DONE!")
53
112
  end
54
113
 
55
- def write_plural(locale, plurals)
56
- locale = locale.to_sym
57
-
58
- xml_doc = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
59
- xml.plist {
60
- xml.dict {
61
- plurals.each do |wording_key, translations|
62
- xml.key wording_key
63
- xml.dict {
64
- xml.key "NSStringLocalizedFormatKey"
65
- xml.string "%\#@key@"
66
- xml.key "key"
67
- xml.dict {
68
- xml.key "NSStringFormatSpecTypeKey"
69
- xml.string "NSStringPluralRuleType"
70
- xml.key "NSStringFormatValueTypeKey"
71
- xml.string "d"
72
- translations[locale][AdLocalize::Constant::PLURAL_KEY_SYMBOL].each do |wording_type, wording_value|
73
- xml.key wording_type
74
- xml.string wording_value
75
- end
76
- }
77
- }
78
- end
114
+ def append_to_localizable_plist(locale)
115
+ filename = export_dir(locale).join(AdLocalize::Constant::IOS_PLURAL_EXPORT_FILENAME)
116
+ xml_doc = nil
117
+ if File.exist?(filename)
118
+ xml_content = Nokogiri::XML(open(filename)) do |config|
119
+ config.default_xml.noblanks # minimify xml
120
+ end
121
+ xml_doc = Nokogiri::XML::Builder.with(xml_content.css('plist>dict').first) do |xml|
122
+ yield(xml)
123
+ end
124
+ else
125
+ xml_doc = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
126
+ xml.plist {
127
+ xml.dict {
128
+ yield(xml)
129
+ }
79
130
  }
80
- }
131
+ end
81
132
  end
82
- export_dir(locale).join(AdLocalize::Constant::IOS_PLURAL_EXPORT_FILENAME).open("w") do |file|
133
+ filename.open("w") do |file|
83
134
  file.puts xml_doc.to_xml(indent: 4)
84
135
  end
85
- AdLocalize::LOGGER.log(:debug, :black, "iOS plural [#{locale}] ---> DONE!")
86
136
  end
87
137
  end
88
138
  end
@@ -1,3 +1,3 @@
1
1
  module AdLocalize
2
- VERSION = "3.1.0"
2
+ VERSION = "3.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ad_localize
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edouard Siegel
@@ -96,6 +96,20 @@ dependencies:
96
96
  - - "~>"
97
97
  - !ruby/object:Gem::Version
98
98
  version: '1.3'
99
+ - !ruby/object:Gem::Dependency
100
+ name: diffy
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - "~>"
104
+ - !ruby/object:Gem::Version
105
+ version: '3.3'
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - "~>"
111
+ - !ruby/object:Gem::Version
112
+ version: '3.3'
99
113
  - !ruby/object:Gem::Dependency
100
114
  name: activesupport
101
115
  requirement: !ruby/object:Gem::Requirement