-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[twine] Plural localizations #8390
Conversation
@@ -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, String.valueOf(size))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too long string. The max values is 100 symbols. Please transfer 'String.valueOf(size)' on the next line.
mSize.setText(mSize.getResources().getQuantityString(R.plurals.bookmarks_places, size,
String.valueOf(size)));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String.valueOf(size) was replaced with size (because of %d usage). Thus, fixed :)
7e281f9
to
010eb30
Compare
Меня немного смущает, что ты меняешь код twine. Это означает, что мы не сможем его обновить. Сейчас смотрю форки и тикеты в twine, чтобы узнать, насколько это всё стандартно. |
@@ -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? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Сюда передаются уже is_plural?
, незачем проверять второй раз. Это даже хуже, потому что возникают вопросы, почему все остальные проверки и вызов format_key_value
не перенести.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
if definition.is_plural? | ||
plural_hash = definition.plural_translation_for_lang(lang) | ||
if plural_hash | ||
format_plural_keys(definition.key.dup, plural_hash) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ты вызываешь с двумя параметрами, а в сигнатуре ниже — один.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Также, почему в format_key_value
вызывается одним способом, а тут — другим?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Сигнатуру починил! Мне кажется проще в данном случае использовать функцию, а не подстановку в паттерн, потому что плюральная строка гораздо сложнее выглядит.
Android:
<string name=""bookmarks_places">%d меток</string>
vs
<plurals name="bookmarks_places">
<item quantity="one">%d метка</item>
<item quantity="few">%d метки</item>
<item quantity="other">%d меток</item>
</plurals>
@@ -46,11 +47,11 @@ def set_translation_for_key(key, lang, value) | |||
end | |||
current_definition = TwineDefinition.new(key) | |||
current_section.definitions << current_definition | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я понимаю, что неаккуратненько, но давай косяки исходного кода не исправлять, чтобы проще было обновлять?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
tools/twine/lib/twine/twine_file.rb
Outdated
@@ -45,11 +50,29 @@ def matches_tags?(tags, include_untagged) | |||
|
|||
def translation_for_lang(lang) | |||
translation = [lang].flatten.map { |l| @translations[l] }.first | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Здесь тоже незачем убирать пустые строки.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
tools/twine/lib/twine/twine_file.rb
Outdated
end | ||
|
||
def add_plural_translation(lang, plural, value) | ||
if PLURAL_KEYS.include? plural |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Эту проверку нужно перенести ниже, где вызывается метод, и писать Warning, если plural
не входит в массив.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
tools/twine/lib/twine/twine_file.rb
Outdated
@@ -137,11 +160,12 @@ def read(path) | |||
parsed = true | |||
end | |||
else | |||
match = /^([^=]+)=(.*)$/.match(line) | |||
match = /^([^:=]+):?([^:=]+)=(.*)$/.match(line) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Если я правильно понимаю, этот регэксп откусывает по символу от всех ключей. Может, сделать проще?
/^([^:=]+)(?::([^=]+))?=(.*)$/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Спасибо, поправил :)
tools/twine/lib/twine/twine_file.rb
Outdated
current_definition.translations[key] = value | ||
# Providing backward compatibility | ||
# for formatters without plural support | ||
if plural_key.empty? || plural_key == 'other' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Что-то мне не очень это нравится. В format_definition
это не запихнуть?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Можно, но тогда это решение не будет работать для форматеров с переопределенным format_definition
. В данном случае мы на этапе создания объекта definition
обеспечиваем наличие ключа в @translations
, если вдруг пользователь указал en:other
, но не указал en
.
tools/twine/lib/twine/twine_file.rb
Outdated
|
||
def plural_translation_for_lang(lang) | ||
if @plural_translations.has_key? lang | ||
@plural_translations[lang].clone |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Почему .clone
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Поменял на .dup :)
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Что-то меня смущает, что выполнение до сюда не дойдёт, если для языка есть только plural. См. выше next unless value
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А, понял, other
же добавляем безусловно. Странно это. Видимо, нужно потом проверять на is_plural
и это значение запихивать в правильный ключ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Даже если только plural, но указан other
, то по идее должно дойти благодаря вот этой строчке:
if plural_key.empty? || plural_key == 'other'
current_definition.translations[key] = value
end
Логика запутанная, конечно, получилась :(
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Здесь, видимо, нужен цикл по current_lang_plural
. А зачем other
исключать?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
current_lang_plural
это словарик вида:
{"one"=>"%d bookmark", "other"=>"%d bookmarks"}
,
Соответственно блок кода:
elsif !current_lang_plural.key? 'other'
new_definition.plural_translations[language]['other'] = value
end
проверяет, есть ли для данного языка other
и если нет, то насильно запихивает value
из обычной локализации.
b341d14
to
442b5a6
Compare
442b5a6
to
c656125
Compare
Usage example:
Generating rules:
%d
.language_code:other = value
:other
key:other
key is not set, but:one, :two
or any other is present,:other
is getting from default language translation. Example:Please, don't forget set
en
oren:other
.