Skip to content

Commit

Permalink
Merge pull request #18 from felginep/feature/adaptive_strings
Browse files Browse the repository at this point in the history
Feature/adaptive strings
  • Loading branch information
jvigne authored Dec 10, 2019
2 parents f637e71 + ccfb533 commit 5efd736
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 37 deletions.
1 change: 1 addition & 0 deletions lib/ad_localize/constant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module AdLocalize
module Constant
SUPPORTED_PLATFORMS = %w(ios android yml json)
PLURAL_KEY_SYMBOL = :plural
ADAPTIVE_KEY_SYMBOL = :adaptive
SINGULAR_KEY_SYMBOL = :singular
COMMENT_KEY_SYMBOL = :comment
INFO_PLIST_KEY_SYMBOL = :info_plist
Expand Down
33 changes: 24 additions & 9 deletions lib/ad_localize/csv_parser.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module AdLocalize
class CsvParser
CSV_WORDING_KEYS_COLUMN = "key"
PLURAL_KEY_REGEXP = /\#\#\{(\w+)\}/
PLURAL_KEY_REGEXP = /\#\#\{([A-Za-z]+)\}/
ADAPTIVE_KEY_REGEXP = /\#\#\{(\d+)\}/
INFO_PLIST_KEY_REGEXP = /(NS.+UsageDescription)|(CF.+Name)/ # see https://developer.apple.com/documentation/bundleresources/information_property_list

attr_accessor :locales
Expand Down Expand Up @@ -78,6 +79,11 @@ def parse_row(row)
"[#{locale.upcase}] Plural key ---> plural_identifier : #{key_infos.dig(:plural_identifier)}, key : #{current_key}",
"[#{locale.upcase}] Missing translation for #{current_key} (#{key_infos.dig(:plural_identifier)})")
value = { key_infos.dig(:plural_identifier) => row[locale] || default_wording(locale, "#{current_key} (#{key_infos.dig(:plural_identifier)})") }
elsif key_infos.dig(:numeral_key) == Constant::ADAPTIVE_KEY_SYMBOL
trace_wording(row[locale],
"[#{locale.upcase}] Adaptive key ---> adaptive_identifier : #{key_infos.dig(:adaptive_identifier)}, key : #{current_key}",
"[#{locale.upcase}] Missing translation for #{current_key} (#{key_infos.dig(:adaptive_identifier)})")
value = { key_infos.dig(:adaptive_identifier) => row[locale] || default_wording(locale, "#{current_key} (#{key_infos.dig(:adaptive_identifier)})") }
elsif key_infos.dig(:numeral_key) == Constant::INFO_PLIST_KEY_SYMBOL
trace_wording(row[locale], "[#{locale.upcase}] Info.plist key ---> #{current_key}", "[#{locale.upcase}] Missing translation for #{current_key}")
value = row[locale] || default_wording(locale, current_key)
Expand All @@ -98,22 +104,31 @@ def parse_key(row)
plural_identifier = nil
invalid_plural = false

if plural_prefix.nil?
if key.match(INFO_PLIST_KEY_REGEXP).nil?
numeral_key = Constant::SINGULAR_KEY_SYMBOL
else
numeral_key = Constant::INFO_PLIST_KEY_SYMBOL
end
else
adaptive_prefix = key.match(ADAPTIVE_KEY_REGEXP)
adaptive_identifier = nil
invalid_adaptive = false

if plural_prefix != nil
numeral_key = Constant::PLURAL_KEY_SYMBOL
key = plural_prefix.pre_match
plural_identifier = plural_prefix.captures&.first
LOGGER.log(:debug, :red, "Invalid key #{key}") if key.nil?
LOGGER.log(:debug, :red, "Empty plural prefix!") if plural_identifier.nil?
invalid_plural = plural_identifier.nil?
elsif adaptive_prefix != nil
numeral_key = Constant::ADAPTIVE_KEY_SYMBOL
key = adaptive_prefix.pre_match
adaptive_identifier = adaptive_prefix.captures&.first
LOGGER.log(:debug, :red, "Invalid key #{key}") if key.nil?
LOGGER.log(:debug, :red, "Empty adaptive prefix!") if adaptive_identifier.nil?
invalid_adaptive = adaptive_identifier.nil?
elsif key.match(INFO_PLIST_KEY_REGEXP) != nil
numeral_key = Constant::INFO_PLIST_KEY_SYMBOL
else
numeral_key = Constant::SINGULAR_KEY_SYMBOL
end

(key.nil? or invalid_plural) ? nil : { key: key.to_sym, numeral_key: numeral_key, plural_identifier: plural_identifier&.to_sym }
(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 }
end

def default_wording(locale, key)
Expand Down
101 changes: 73 additions & 28 deletions lib/ad_localize/platform/ios_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ def platform

def export(locale, data, export_extension = nil, substitution_format = nil)
create_locale_dir(locale)
[AdLocalize::Constant::PLURAL_KEY_SYMBOL, AdLocalize::Constant::SINGULAR_KEY_SYMBOL, AdLocalize::Constant::INFO_PLIST_KEY_SYMBOL].each do |numeral_key|
all_symbols = [
AdLocalize::Constant::PLURAL_KEY_SYMBOL,
AdLocalize::Constant::ADAPTIVE_KEY_SYMBOL,
AdLocalize::Constant::SINGULAR_KEY_SYMBOL,
AdLocalize::Constant::INFO_PLIST_KEY_SYMBOL
]
all_symbols.each do |numeral_key|
numeral_data = data.select {|key, wording| wording.dig(locale.to_sym)&.key? numeral_key}
if numeral_data.empty?
AdLocalize::LOGGER.log(:info, :black, "[#{locale.upcase}] no #{numeral_key.to_s} keys were found to generate the file")
Expand Down Expand Up @@ -36,6 +42,54 @@ def write_info_plist(locale, singulars)
)
end

def write_plural(locale, plurals)
locale = locale.to_sym

append_to_localizable_plist(locale) do |xml|
plurals.each do |wording_key, translations|
xml.key wording_key
xml.dict {
xml.key "NSStringLocalizedFormatKey"
xml.string "%\#@key@"
xml.key "key"
xml.dict {
xml.key "NSStringFormatSpecTypeKey"
xml.string "NSStringPluralRuleType"
xml.key "NSStringFormatValueTypeKey"
xml.string "d"
translations[locale][AdLocalize::Constant::PLURAL_KEY_SYMBOL].each do |wording_type, wording_value|
xml.key wording_type
xml.string wording_value
end
}
}
end
end
AdLocalize::LOGGER.log(:debug, :black, "iOS plural [#{locale}] ---> DONE!")
end

def write_adaptive(locale, adaptives)
locale = locale.to_sym

append_to_localizable_plist(locale) do |xml|
adaptives.each do |wording_key, translations|
xml.key wording_key
xml.dict {
xml.key "NSStringVariableWidthRuleType"
xml.dict {
translations[locale][AdLocalize::Constant::ADAPTIVE_KEY_SYMBOL].each do |wording_type, wording_value|
xml.key wording_type
xml.string wording_value
end
}
}
end
end
AdLocalize::LOGGER.log(:debug, :black, "iOS adaptive [#{locale}] ---> DONE!")
end

private

def write_localizable(locale:, singulars:, wording_type:, filename:)
locale = locale.to_sym

Expand All @@ -57,37 +111,28 @@ def write_localizable(locale:, singulars:, wording_type:, filename:)
AdLocalize::LOGGER.log(:debug, :black, "iOS #{wording_type} [#{locale}] ---> DONE!")
end

def write_plural(locale, plurals)
locale = locale.to_sym

xml_doc = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
xml.plist {
xml.dict {
plurals.each do |wording_key, translations|
xml.key wording_key
xml.dict {
xml.key "NSStringLocalizedFormatKey"
xml.string "%\#@key@"
xml.key "key"
xml.dict {
xml.key "NSStringFormatSpecTypeKey"
xml.string "NSStringPluralRuleType"
xml.key "NSStringFormatValueTypeKey"
xml.string "d"
translations[locale][AdLocalize::Constant::PLURAL_KEY_SYMBOL].each do |wording_type, wording_value|
xml.key wording_type
xml.string wording_value
end
}
}
end
def append_to_localizable_plist(locale)
filename = export_dir(locale).join(AdLocalize::Constant::IOS_PLURAL_EXPORT_FILENAME)
xml_doc = nil
if File.exist?(filename)
xml_content = Nokogiri::XML(open(filename)) do |config|
config.default_xml.noblanks # minimify xml
end
xml_doc = Nokogiri::XML::Builder.with(xml_content.css('plist>dict').first) do |xml|
yield(xml)
end
else
xml_doc = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
xml.plist {
xml.dict {
yield(xml)
}
}
}
end
end
export_dir(locale).join(AdLocalize::Constant::IOS_PLURAL_EXPORT_FILENAME).open("w") do |file|
filename.open("w") do |file|
file.puts xml_doc.to_xml(indent: 4)
end
AdLocalize::LOGGER.log(:debug, :black, "iOS plural [#{locale}] ---> DONE!")
end
end
end
12 changes: 12 additions & 0 deletions test/exports_reference/ios/en.lproj/Localizable.stringsdict
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,17 @@
<string>Rate %1$@ stars</string>
</dict>
</dict>
<key>start_countdown</key>
<dict>
<key>NSStringVariableWidthRuleType</key>
<dict>
<key>20</key>
<string>Start</string>
<key>25</key>
<string>Start countdown</string>
<key>50</key>
<string>Start countdown</string>
</dict>
</dict>
</dict>
</plist>
12 changes: 12 additions & 0 deletions test/exports_reference/ios/fr.lproj/Localizable.stringsdict
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,17 @@
<string>Attribuer %1$@ étoiles</string>
</dict>
</dict>
<key>start_countdown</key>
<dict>
<key>NSStringVariableWidthRuleType</key>
<dict>
<key>20</key>
<string>Commencer</string>
<key>25</key>
<string>Commencer</string>
<key>50</key>
<string>Commencer le compte à rebours</string>
</dict>
</dict>
</dict>
</plist>
4 changes: 4 additions & 0 deletions test/reference.csv
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
,assess_rate_trip_voiceover##{other},Attribuer %1$@ étoiles,Si %1$@<=3 : prononcer également assess_comment_placeholder. Si %1$@>3 : prononcer également assess_optional_comment_placeholder,Rate %1$@ stars,
#InfoPlist,,,,,
,NSCameraUsageDescription,Camera utilisé pour blabla,,Camera used for blabla,
#Variadic width strings,,,,,
,start_countdown##{20},Commencer,,Start,
,start_countdown##{25},Commencer,,Start countdown,
,start_countdown##{50},Commencer le compte à rebours,,Start countdown,

0 comments on commit 5efd736

Please sign in to comment.