Skip to content

Commit

Permalink
[twine] Add plural strings support
Browse files Browse the repository at this point in the history
  • Loading branch information
greshilov committed Mar 27, 2018
1 parent a900c98 commit 010eb30
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 50 deletions.
2 changes: 1 addition & 1 deletion android/src/com/mapswithme/maps/bookmarks/Holders.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void setName(@NonNull String name)

void setSize(int size)
{
mSize.setText(mSize.getResources().getString(R.string.bookmarks_places, String.valueOf(size)));
mSize.setText(mSize.getResources().getQuantityString(R.plurals.bookmarks_places, size, size));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class BMCCategoryCell: MWMTableViewCell {
extension BMCCategoryCell: BMCCategoryObserver {
func categoryUpdated() {
title.text = category.title
count.text = String(coreFormat: L("bookmarks_places"), arguments: [category.count])
count.text = String(format: L("bookmarks_places"), category.count)

if category.isVisible {
visibility.tintColor = .linkBlue()
Expand Down
77 changes: 40 additions & 37 deletions strings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29500,37 +29500,40 @@ fa = دوباره نپرس
zh-Hant = 顯示全部

[bookmarks_places]
en = %s bookmarks
ru = %s меток
ar = %s من الإشارات المرجعية
cs = %s záložek
da = %s bogmærker
nl = %s bladwijzers
fi = %s kirjanmerkkiä
fr = %s signets
de = %s Lesezeichen
hu = könyvjelzők %s
id = %s markah
it = %s preferiti
ja = %s のブックマーク
ko = %s 북마크
nb = %s bokmerker
pl = Zakładki: %s
pt = %s favoritos
pt-BR = %s favoritos
ro = %s semne de carte
es = %s marcadores
sv = %s bokmärken
th = %s บุ๊กมาร์ก
tr = %s yer imi
uk = %s закладок
vi = %s dấu trang
el = %s αγαπημένα
es_MX = %s marcadores
sk = %s záložiek
sw = Alamisho %s
zh-Hans = %s 個書籤
zh-Hant = %s個書籤
en:one = %d bookmark
en:other = %d bookmarks
ru:one = %d метка
ru:few = %d метки
ru:other = %d меток
ar = %d من الإشارات المرجعية
cs = %d záložek
da = %d bogmærker
nl = %d bladwijzers
fi = %d kirjanmerkkiä
fr = %d signets
de = %d Lesezeichen
hu = könyvjelzők %d
id = %d markah
it = %d preferiti
ja = %d のブックマーク
ko = %d 북마크
nb = %d bokmerker
pl = Zakładki: %d
pt = %d favoritos
pt-BR = %d favoritos
ro = %d semne de carte
es = %d marcadores
sv = %d bokmärken
th = %d บุ๊กมาร์ก
tr = %d yer imi
uk = %d закладок
vi = %d dấu trang
el = %d αγαπημένα
es_MX = %d marcadores
sk = %d záložiek
sw = Alamisho %d
zh-Hans = %d 個書籤
zh-Hant = %d個書籤

[bookmarks_create_new_group]
en = Create new list
Expand Down Expand Up @@ -29798,7 +29801,7 @@ fa = دوباره نپرس
zh-Hant = 預訂在您所選日期有空的賓館!用入住和離店篩選

[whats_new_title_ugc_travel]
en = Share your experience with other travellers!
en = Share your experience with other travellers!
ru = Делитесь вашими впечатлениями с другими путешественниками!
ar = شارك تجربتك مع المسافرين الآخرين!
cs = Podělte se o své zkušenosti s dalšími cestovateli!
Expand All @@ -29815,7 +29818,7 @@ fa = دوباره نپرس
nb = Del dine opplevelser med andre reisende!
pl = Podziel się swoimi wrażeniami z innymi podróżnymi!
pt = Partilhe a sua experiência com outros viajantes!
pt-BR = Compartilhe sua experiência com outros viajantes!
pt-BR = Compartilhe sua experiência com outros viajantes!
ro = Comunicați-vă experiențele altor călători!
es = ¡Comparta su experiencia con otros viajeros!
sv = Dela din upplevelse med andra resenärer!
Expand All @@ -29824,7 +29827,7 @@ fa = دوباره نپرس
uk = Поділіться вашими враженнями з іншими мандрівниками!
vi = Chia sẻ trải nghiệm của bạn với những du khách khác!
el = Μοιραστείτε την εμπειρία σας με άλλους ταξιδιώτες!
es_MX = Comparte tu experiencia con otros viajeros!
es_MX = Comparte tu experiencia con otros viajeros!
sk = Podeľte sa o svoje skúsenosti s ďalšími cestovateľmi!
sw = Shiriki hisia zako na wasafiri wengine!
zh-Hans = 與其他旅客分享您的體驗!
Expand Down Expand Up @@ -29862,7 +29865,7 @@ fa = دوباره نپرس
sw = Kadiria na utoe maoni kuhusu maeneo uliyotembelea!
zh-Hans = 評價您參觀過的地方!
zh-Hant = 給您訪問過的地方打分和評價!

[dialog_pedestrian_route_is_long]
tags = ios,android
en = The pedestrian route is too long
Expand Down Expand Up @@ -30103,7 +30106,7 @@ fa = دوباره نپرس
[bookmarks_error_title_empty_list_name]
en = The name couldn't be empty
ru = Имя списка не может быть пустым

[bookmarks_error_message_empty_list_name]
en = Please enter the list name
ru = Введите имя списка, пожалуйста
Expand All @@ -30126,4 +30129,4 @@ fa = دوباره نپرس

[bookmarks_error_message_list_name_too_long]
en = Please choose another name
ru = Выберите, пожалуйста, другое имя
ru = Выберите, пожалуйста, другое имя
1 change: 1 addition & 0 deletions tools/twine/lib/twine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Error < StandardError
require 'twine/formatters/abstract'
require 'twine/formatters/android'
require 'twine/formatters/apple'
require 'twine/formatters/apple_plural'
require 'twine/formatters/django'
require 'twine/formatters/flash'
require 'twine/formatters/gettext'
Expand Down
28 changes: 24 additions & 4 deletions tools/twine/lib/twine/formatters/abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Twine
module Formatters
class Abstract
SUPPORTS_PLURAL = false
attr_accessor :twine_file
attr_accessor :options

Expand Down Expand Up @@ -46,11 +47,11 @@ def set_translation_for_key(key, lang, value)
end
current_definition = TwineDefinition.new(key)
current_section.definitions << current_definition

if @options[:tags] && @options[:tags].length > 0
current_definition.tags = @options[:tags]
current_definition.tags = @options[:tags]
end

@twine_file.definitions_by_key[key] = current_definition
@twine_file.definitions_by_key[key].translations[lang] = value
else
Expand Down Expand Up @@ -132,7 +133,13 @@ def format_section(section, lang)
end

def format_definition(definition, lang)
[format_comment(definition, lang), format_key_value(definition, lang)].compact.join
formatted_definition = [format_comment(definition, lang)]
if self.class::SUPPORTS_PLURAL && definition.is_plural?
formatted_definition << format_plural(definition, lang)
else
formatted_definition << format_key_value(definition, lang)
end
formatted_definition.compact.join
end

def format_comment(definition, lang)
Expand All @@ -143,10 +150,23 @@ def format_key_value(definition, lang)
key_value_pattern % { key: format_key(definition.key.dup), value: format_value(value.dup) }
end

def format_plural(definition, lang)
if definition.is_plural?
plural_hash = definition.plural_translation_for_lang(lang)
if plural_hash
format_plural_keys(definition.key.dup, plural_hash)
end
end
end

def key_value_pattern
raise NotImplementedError.new("You must implement key_value_pattern in your formatter class.")
end

def format_plural_keys(plural_hash)
raise NotImplementedError.new("You must implement format_plural_keys in your formatter class.")
end

def format_key(key)
key
end
Expand Down
8 changes: 8 additions & 0 deletions tools/twine/lib/twine/formatters/android.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Formatters
class Android < Abstract
include Twine::Placeholders

SUPPORTS_PLURAL = true

LANG_CODES = Hash[
'zh' => 'zh-Hans',
'zh-CN' => 'zh-Hans',
Expand Down Expand Up @@ -110,6 +112,12 @@ def key_value_pattern
"\t<string name=\"%{key}\">%{value}</string>"
end

def format_plural_keys(key, plural_hash)
result = "\t<plurals name=\"#{key}\">\n"
result += plural_hash.map{|quantity,value| "\t#{' ' * 2}<item quantity=\"#{quantity}\">#{value}</item>"}.join("\n")
result += "\n\t</plurals>"
end

def escape_value(value)
# escape double and single quotes, & signs and tags
value = escape_quotes(value)
Expand Down
4 changes: 4 additions & 0 deletions tools/twine/lib/twine/formatters/apple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def format_key(key)
def format_value(value)
escape_quotes(value)
end

def should_include_definition(definition, lang)
!definition.is_plural? && super
end
end
end
end
Expand Down
69 changes: 69 additions & 0 deletions tools/twine/lib/twine/formatters/apple_plural.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module Twine
module Formatters
class ApplePlural < Apple
SUPPORTS_PLURAL = true

def format_name
'apple-plural'
end

def extension
'.stringsdict'
end

def default_file_name
'Localizable.stringsdict'
end

def format_footer(lang)
footer = "</dict>\n</plist>"
end

def format_file(lang)
result = super
result += format_footer(lang)
end

def format_header(lang)
header = "<!--\n * Apple Stringsdict File\n * Generated by Twine #{Twine::VERSION}\n * Language: #{lang} -->\n"
header += "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>\n"
header += "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
header += "<plist version=\"1.0\">\n<dict>"
end

def format_section_header(section)
"<!-- ********** #{section.name} **********/ -->\n"
end

def format_plural_keys(key, plural_hash)
result = "#{tab(2)}<key>#{key}</key>\n"
result += "#{tab(2)}<dict>\n"
result += "#{tab(4)}<key>NSStringLocalizedFormatKey</key>\n#{tab(4)}<string>\%\#@value@</string>\n"
result += "#{tab(4)}<key>value</key>\n#{tab(4)}<dict>\n"
result += "#{tab(6)}<key>NSStringFormatSpecTypeKey</key>\n#{tab(6)}<string>NSStringPluralRuleType</string>\n"
result += "#{tab(6)}<key>NSStringFormatValueTypeKey</key>\n#{tab(6)}<string>d</string>\n"
result += plural_hash.map{|quantity,value| "#{tab(6)}<key>#{quantity}</key>\n#{tab(6)}<string>#{value}</string>"}.join("\n")
result += "\n#{tab(4)}</dict>\n#{tab(2)}</dict>\n"
end

def format_comment(definition, lang)
"<!-- #{definition.comment.gsub('--', '—')} -->\n" if definition.comment
end

def read(io, lang)
raise NotImplementedError.new("Reading \".stringdict\" files not implemented yet")
end

def tab(level)
' ' * level
end

def should_include_definition(definition, lang)
return definition.is_plural? && definition.plural_translation_for_lang(lang)
end

end
end
end

Twine::Formatters.formatters << Twine::Formatters::ApplePlural.new
12 changes: 12 additions & 0 deletions tools/twine/lib/twine/output_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ def process(language)
new_definition = definition.dup
new_definition.translations[language] = value

current_lang_plural = definition.plural_translation_for_lang(language)

if definition.is_plural?
# When no plural value set for current language
if current_lang_plural.nil?
new_definition.plural_translations[language] = {'other' => value }
# When user forget set 'other' key
elsif !current_lang_plural.key? 'other'
new_definition.plural_translations[language]['other'] = value
end
end

new_section.definitions << new_definition
result.definitions_by_key[new_definition.key] = new_definition
end
Expand Down
Loading

0 comments on commit 010eb30

Please sign in to comment.