From 583558f7b8c9822cc94f7a01571e07dc2020acae Mon Sep 17 00:00:00 2001 From: Ali <> Date: Thu, 22 Jul 2021 12:13:46 +0200 Subject: [PATCH] Fix localization --- .../Telegram-iOS/en.lproj/Localizable.strings | 42 ------------------- .../GenerateStrings/GenerateStrings.py | 40 ++++++++++++++---- .../Sources/NumberPluralizationForm.m | 14 ++++--- 3 files changed, 41 insertions(+), 55 deletions(-) diff --git a/Telegram/Telegram-iOS/en.lproj/Localizable.strings b/Telegram/Telegram-iOS/en.lproj/Localizable.strings index e173067741..779b363a2b 100644 --- a/Telegram/Telegram-iOS/en.lproj/Localizable.strings +++ b/Telegram/Telegram-iOS/en.lproj/Localizable.strings @@ -89,31 +89,19 @@ "PUSH_MESSAGE_GAME" = "%1$@|invited you to play %2$@"; "PUSH_MESSAGE_INVOICE" = "%1$@|sent you an invoice for %2$@"; "PUSH_MESSAGE_FWD" = "%1$@|forwarded you a message"; -"PUSH_MESSAGE_FWDS_1" = "%1$@|forwarded you a message"; -"PUSH_MESSAGE_FWDS_any" = "%1$@|forwarded you %2$d messages"; "PUSH_MESSAGE_FWDS_TEXT_1" = "forwarded you a message"; "PUSH_MESSAGE_FWDS_TEXT_any" = "forwarded you %d messages"; "PUSH_MESSAGE_PHOTO" = "%1$@|sent you a photo"; -"PUSH_MESSAGE_PHOTOS_1" = "%1$@|sent you a photo"; -"PUSH_MESSAGE_PHOTOS_any" = "%1$@|sent you %2$d photos"; "PUSH_MESSAGE_PHOTOS_TEXT_1" = "sent you a photo"; "PUSH_MESSAGE_PHOTOS_TEXT_any" = "sent you %d photos"; "PUSH_MESSAGE_VIDEO" = "%1$@|sent you a video"; -"PUSH_MESSAGE_VIDEOS_1" = "%1$@|sent you a video"; -"PUSH_MESSAGE_VIDEOS_any" = "%1$@|sent you %2$d videos"; "PUSH_MESSAGE_VIDEOS_TEXT_1" = "sent you a video"; "PUSH_MESSAGE_VIDEOS_TEXT_any" = "sent you %d videos"; "PUSH_MESSAGE_ROUND" = "%1$@|sent you a video message"; -"PUSH_MESSAGE_ROUNDS_1" = "%1$@|sent you a video message"; -"PUSH_MESSAGE_ROUNDS_any" = "%1$@|sent you %2$d video messages"; "PUSH_MESSAGE" = "%1$@|sent you a message"; -"PUSH_MESSAGES_1" = "%1$@|sent you a message"; -"PUSH_MESSAGES_any" = "%1$@|sent you %2$d messages"; "PUSH_MESSAGES_TEXT_1" = "sent you a message"; "PUSH_MESSAGES_TEXT_any" = "sent you %2$d messages"; "PUSH_ALBUM" = "%1$@|sent you an album"; -"PUSH_MESSAGE_FILES_1" = "%1$@|sent you a file"; -"PUSH_MESSAGE_FILES_any" = "%1$@|sent you %2$d files"; "PUSH_MESSAGE_FILES_TEXT_1" = "sent you a file"; "PUSH_MESSAGE_FILES_TEXT_any" = "sent you %d files"; @@ -133,29 +121,17 @@ "PUSH_CHANNEL_MESSAGE_GIF" = "%1$@|posted a GIF"; "PUSH_CHANNEL_MESSAGE_GAME" = "%1$@|invited you to play %2$@"; "PUSH_CHANNEL_MESSAGE_FWD" = "%1$@|posted a forwarded message"; -"PUSH_CHANNEL_MESSAGE_FWDS_1" = "%1$@|posted a forwarded message"; -"PUSH_CHANNEL_MESSAGE_FWDS_any" = "%1$@|posted %2$d forwarded messages"; "PUSH_CHANNEL_MESSAGE_PHOTO" = "%1$@|posted a photo"; -"PUSH_CHANNEL_MESSAGE_PHOTOS_1" = "%1$@|posted a photo"; -"PUSH_CHANNEL_MESSAGE_PHOTOS_any" = "%1$@|posted %2$d photos"; "PUSH_CHANNEL_MESSAGE_PHOTOS_TEXT_1" = "posted a photo"; "PUSH_CHANNEL_MESSAGE_PHOTOS_TEXT_any" = "posted %d photos"; "PUSH_CHANNEL_MESSAGE_VIDEO" = "%1$@|posted a video"; -"PUSH_CHANNEL_MESSAGE_VIDEOS_1" = "%1$@|posted a video"; -"PUSH_CHANNEL_MESSAGE_VIDEOS_any" = "%1$@|posted %2$d videos"; "PUSH_CHANNEL_MESSAGE_VIDEOS_TEXT_1" = "posted a video"; "PUSH_CHANNEL_MESSAGE_VIDEOS_TEXT_any" = "posted %d videos"; "PUSH_CHANNEL_MESSAGE_ROUND" = "%1$@|posted a video message"; -"PUSH_CHANNEL_MESSAGE_ROUNDS_1" = "%1$@|posted a video message"; -"PUSH_CHANNEL_MESSAGE_ROUNDS_any" = "%1$@|posted %2$d video messages"; "PUSH_CHANNEL_MESSAGE" = "%1$@|posted a message"; -"PUSH_CHANNEL_MESSAGES_1" = "%1$@|posted a message"; -"PUSH_CHANNEL_MESSAGES_any" = "%1$@|posted %2$d messages"; "PUSH_CHANNEL_MESSAGES_TEXT_1" = "posted a message"; "PUSH_CHANNEL_MESSAGES_TEXT_any" = "posted %d messages"; "PUSH_CHANNEL_ALBUM" = "%1$@|posted an album"; -"PUSH_CHANNEL_MESSAGE_DOCS_1" = "%1$@|posted a file"; -"PUSH_CHANNEL_MESSAGE_DOCS_any" = "%1$@|posted %2$d files"; "PUSH_CHANNEL_MESSAGE_DOCS_TEXT_1" = "posted a file"; "PUSH_CHANNEL_MESSAGE_DOCS_TEXT_any" = "posted %d files"; @@ -185,31 +161,19 @@ "PUSH_CHAT_RETURNED" = "%2$@|%1$@ has returned to the group"; "PUSH_CHAT_JOINED" = "%2$@|%1$@ has joined the group"; "PUSH_CHAT_MESSAGE_FWD" = "%2$@|%1$@ forwarded a message"; -"PUSH_CHAT_MESSAGE_FWDS_1" = "%2$@|%1$@ forwarded a message"; -"PUSH_CHAT_MESSAGE_FWDS_any" = "%2$@|%1$@ forwarded %3$d messages"; "PUSH_CHAT_MESSAGE_FWDS_TEXT_1" = "{author} forwarded a message"; "PUSH_CHAT_MESSAGE_FWDS_TEXT_any" = "{author} forwarded %d messages"; "PUSH_CHAT_MESSAGE_PHOTO" = "%2$@|%1$@ sent a photo"; -"PUSH_CHAT_MESSAGE_PHOTOS_1" = "%2$@|%1$@ sent a photo"; -"PUSH_CHAT_MESSAGE_PHOTOS_any" = "%2$@|%1$@ sent %3$d photos"; "PUSH_CHAT_MESSAGE_PHOTOS_TEXT_1" = "{author} sent a photo"; "PUSH_CHAT_MESSAGE_PHOTOS_TEXT_any" = "{author} sent %d photos"; "PUSH_CHAT_MESSAGE_VIDEO" = "%2$@|%1$@ sent a video"; -"PUSH_CHAT_MESSAGE_VIDEOS_1" = "%2$@|%1$@ sent a video"; -"PUSH_CHAT_MESSAGE_VIDEOS_any" = "%2$@|%1$@ sent %3$d videos"; "PUSH_CHAT_MESSAGE_VIDEOS_TEXT_1" = "{author} sent a video"; "PUSH_CHAT_MESSAGE_VIDEOS_TEXT_any" = "{text} sent %d videos"; "PUSH_CHAT_MESSAGE_ROUND" = "%2$@|%1$@ sent a video message"; -"PUSH_CHAT_MESSAGE_ROUNDS_1" = "%2$@|%1$@ sent a video message"; -"PUSH_CHAT_MESSAGE_ROUNDS_any" = "%2$@|%1$@ sent %3$d video messages"; "PUSH_CHAT_MESSAGE" = "%2$@|%1$@ sent a message"; -"PUSH_CHAT_MESSAGES_1" = "%2$@|%1$@ sent a message"; -"PUSH_CHAT_MESSAGES_any" = "%2$@|%1$@ sent %3$d messages"; "PUSH_CHAT_MESSAGES_TEXT_1" = "{author} sent a message"; "PUSH_CHAT_MESSAGES_TEXT_any" = "{author} sent %d messages"; "PUSH_CHAT_ALBUM" = "%2$@|%1$@ sent an album"; -"PUSH_CHAT_MESSAGE_DOCS_FIX1_1" = "%2$@|%1$@ sent a file"; -"PUSH_CHAT_MESSAGE_DOCS_FIX1_any" = "%2$@|%1$@ sent %3$d files"; "PUSH_CHAT_MESSAGE_DOCS_TEXT_1" = "{author} sent a file"; "PUSH_CHAT_MESSAGE_DOCS_TEXT_any" = "{author} sent %d files"; @@ -1311,12 +1275,6 @@ "ForwardedContacts_0" = "%@ forwarded contacts"; "ForwardedAuthors2" = "%@, %@"; -"ForwardedAuthorsOthers_1" = "%@ and 1 other"; -"ForwardedAuthorsOthers_2" = "%@ and 2 others"; -"ForwardedAuthorsOthers_3_10" = "%@ and %@ others"; -"ForwardedAuthorsOthers_any" = "%@ and %@ others"; -"ForwardedAuthorsOthers_many" = "%@ and %@ others"; -"ForwardedAuthorsOthers_0" = "%@ and %@ others"; "PrivacySettings.TwoStepAuth" = "Two-Step Verification"; "TwoStepAuth.Title" = "Two-Step Verification"; diff --git a/build-system/GenerateStrings/GenerateStrings.py b/build-system/GenerateStrings/GenerateStrings.py index 92b35f1de5..1bd55e673c 100644 --- a/build-system/GenerateStrings/GenerateStrings.py +++ b/build-system/GenerateStrings/GenerateStrings.py @@ -8,6 +8,8 @@ import re import codecs import struct +from typing import Dict, List + def _unescape_key(s): return s.replace('\\\n', '') @@ -106,7 +108,7 @@ def parse_positional_arguments(string: str) -> [PositionalArgument]: def parse_entries(strings: [dict]) -> [Entry]: - entries = [] + entries: List[Entry] = [] pluralized = re.compile(r'^(.*?)_(0|1|2|3_10|many|any)$', re.U) processed_entries = set() for string in strings: @@ -115,14 +117,20 @@ def parse_entries(strings: [dict]) -> [Entry]: if m is not None: raw_key = m.group(1) + positional_arguments = parse_positional_arguments(string['value']) + if raw_key in processed_entries: + for i in range(0, len(entries)): + if entries[i].name == raw_key: + if len(entries[i].positional_arguments) < len(positional_arguments): + entries[i].positional_arguments = positional_arguments continue processed_entries.add(raw_key) entries.append(Entry( name=raw_key, is_pluralized=True, - positional_arguments=[] + positional_arguments=positional_arguments )) else: if key in processed_entries: @@ -134,6 +142,18 @@ def parse_entries(strings: [dict]) -> [Entry]: is_pluralized=False, positional_arguments=parse_positional_arguments(string['value']) )) + + had_error = False + for entry in entries: + if entry.is_pluralized: + if len(entry.positional_arguments) > 1: + print('Pluralized key "{}" needs to contain at most 1 positional argument, {} were provided' + .format(entry.name, len(entry.positional_arguments))) + had_error = True + + if had_error: + sys.exit(1) + entries.sort(key=lambda x: x.name) return entries @@ -216,7 +236,7 @@ def generate(header_path: str, implementation_path: str, data_path: str, entries arguments_string = '' arguments_array = '' for i in range(0, num_arguments): - arguments_string += ', id arg{}'.format(i) + arguments_string += ', id _Nonnull arg{}'.format(i) if i != 0: arguments_array += ', ' arguments_array += '[[NSString alloc] initWithFormat:@"%@", arg{}]'.format(i) @@ -458,8 +478,8 @@ static NSString * _Nonnull getPluralized(_PresentationStrings * _Nonnull strings NSString *parsedKey = [[NSString alloc] initWithFormat:@"%@%@", key, getPluralizationSuffix(strings, value)]; NSString *formatString = getSingle(strings, parsedKey); NSString *stringValue = formatNumberWithGroupingSeparator(strings.groupingSeparator, value); - NSString *result = [[NSString alloc] initWithFormat:formatString, stringValue]; - return result; + NSArray<_FormattedStringRange *> *argumentRanges = extractArgumentRanges(formatString); + return formatWithArgumentRanges(formatString, argumentRanges, @[stringValue]).string; } static NSString * _Nonnull getPluralizedIndirect(_PresentationStrings * _Nonnull strings, uint32_t keyId, @@ -581,8 +601,7 @@ static NSString * _Nonnull getPluralizedIndirect(_PresentationStrings * _Nonnull format_arguments_array += ', ' if argument.kind == 'd': function_arguments += ', NSInteger _{}'.format(argument_index) - format_arguments_array += '@(_{})'\ - .format(argument_index) + format_arguments_array += '@(_{})'.format(argument_index) elif argument.kind == '@': function_arguments += ', NSString * _Nonnull _{}'.format(argument_index) format_arguments_array += '_{}'.format(argument_index) @@ -602,6 +621,13 @@ static NSString * _Nonnull getPluralizedIndirect(_PresentationStrings * _Nonnull function_spec=function_spec, swift_spec=swift_spec)) if entry.is_pluralized: + argument_format_type = '' + if entry.positional_arguments[0].kind == 'd': + argument_format_type = '0' + elif entry.positional_arguments[0].kind == '@': + argument_format_type = '1' + else: + raise Exception('Unsupported argument type {}'.format(argument.kind)) write_string(source_file, function_spec + ''' {{ return getPluralizedIndirect(_self, {entry_key_id}, value); }}'''.format(key=entry.name, entry_key_id=entry_key_id)) diff --git a/submodules/NumberPluralizationForm/Sources/NumberPluralizationForm.m b/submodules/NumberPluralizationForm/Sources/NumberPluralizationForm.m index b30286833c..da54b6e72e 100644 --- a/submodules/NumberPluralizationForm/Sources/NumberPluralizationForm.m +++ b/submodules/NumberPluralizationForm/Sources/NumberPluralizationForm.m @@ -356,19 +356,21 @@ NumberPluralizationForm numberPluralizationForm(unsigned int lc, int n) { NSString * _Nonnull formatNumberWithGroupingSeparator(NSString * _Nonnull groupingSeparator, int32_t value) { NSString *string = [[NSString alloc] initWithFormat:@"%d", (int)value]; + if (ABS(value) < 1000 || groupingSeparator.length == 0) { return string; } else { NSMutableString *groupedString = [[NSMutableString alloc] init]; - int n = (int)ceil(((double)(string.length)) / 3.0); - for (int i = 0; i < n; i++) { - int index = ((int)string.length) - (i + 1) * 3; - if (groupedString.length != 0) { + + int numberOfPlaces = 0; + for (int i = ((int)string.length) - 1; i >= 0; i--) { + if (numberOfPlaces != 0 && numberOfPlaces % 3 == 0) { [groupedString insertString:groupingSeparator atIndex:0]; } - NSString *section = [string substringWithRange:NSMakeRange(MAX(0, index), 3)]; - [groupedString insertString:section atIndex:0]; + [groupedString insertString:[string substringWithRange:NSMakeRange(i, 1)] atIndex:0]; + numberOfPlaces++; } + return groupedString; } }