def __ReadSourceAndTranslationsFrom(self, string_id_set, grd_file, xtb_files): """Reads source strings and translations for a .grd file. Reads the source strings and all available translations for the messages identified by string_id_set. The source string is used where translations are missing. Args: string_id_set: The identifiers of the strings to read. grd_file: Path to a .grd file. xtb_files: List of paths to .xtb files. Returns: An unsorted list of __TranslationData instances. """ sax_parser = sax.make_parser() # Read the source (en-US) string from the .grd file. grd_handler = GrdHandler(string_id_set) sax_parser.setContentHandler(grd_handler) # A base name is calculated from grd_file so that's why we don't # just change that from chrome/installer/util/BUILD.gn sax_parser.parse("../../chrome/app/brave_strings.grd") source_strings = grd_handler.messages # Manually put the source strings as en-US in the list of translated # strings. translated_strings = [] for string_id, message_text in source_strings.iteritems(): translated_strings.append(self.__TranslationData(string_id, 'EN_US', message_text)) # Generate the message ID for each source string to correlate it with its # translations in the .xtb files. Multiple source strings may have the same # message text; hence the message id is mapped to a list of string ids # instead of a single value. translation_ids = {} for (string_id, message_text) in source_strings.iteritems(): message_id = tclib.GenerateMessageId(message_text) translation_ids.setdefault(message_id, []).append(string_id); # Gather the translated strings from the .xtb files. Use the en-US string # for any message lacking a translation. xtb_handler = XtbHandler(translation_ids) sax_parser.setContentHandler(xtb_handler) for xtb_filename in xtb_files: sax_parser.parse(xtb_filename) for string_id, message_text in source_strings.iteritems(): translated_string = xtb_handler.translations.get(string_id, message_text) translated_strings.append(self.__TranslationData(string_id, xtb_handler.lang, translated_string)) return translated_strings
def __ReadSourceAndTranslationsFrom(self, grd_file, xtb_files): """Reads source strings and translations for a .grd file. Reads the source strings and all available translations for the messages identified by STRING_IDS. The source string is used where translations are missing. Args: grd_file: Path to a .grd file. xtb_files: List of paths to .xtb files. Returns: An unsorted list of __TranslationData instances. """ sax_parser = sax.make_parser() # Read the source (en-US) string from the .grd file. grd_handler = GrdHandler(STRING_IDS) sax_parser.setContentHandler(grd_handler) sax_parser.parse(grd_file) source_strings = grd_handler.messages # Manually put the source strings as en-US in the list of translated # strings. translated_strings = [] for string_id, message_text in source_strings.iteritems(): translated_strings.append(self.__TranslationData(string_id, 'EN_US', message_text)) # Generate the message ID for each source string to correlate it with its # translations in the .xtb files. translation_ids = { tclib.GenerateMessageId(message_text): string_id for (string_id, message_text) in source_strings.iteritems() } # Gather the translated strings from the .xtb files. Use the en-US string # for any message lacking a translation. xtb_handler = XtbHandler(translation_ids) sax_parser.setContentHandler(xtb_handler) for xtb_filename in xtb_files: sax_parser.parse(xtb_filename) for string_id, message_text in source_strings.iteritems(): translated_string = xtb_handler.translations.get(string_id, message_text) translated_strings.append(self.__TranslationData(string_id, xtb_handler.lang, translated_string)) return translated_strings
def CollectTranslatedStrings(branding): """Collects all the translations for all the strings specified by kStringIds. Returns a list of tuples of (string_id, language, translated string). The list is sorted by language codes.""" strings_file = 'app/chromium_strings.grd' translation_files = 'chromium_strings*.xtb' if branding == 'Chrome': strings_file = 'app/google_chrome_strings.grd' translation_files = 'google_chrome_strings*.xtb' kGeneratedResourcesPath = os.path.join(path_utils.ScriptDir(), '..', '..', '..', strings_file) kTranslationDirectory = os.path.join(path_utils.ScriptDir(), '..', '..', '..', 'app', 'resources') kTranslationFiles = glob.glob( os.path.join(kTranslationDirectory, translation_files)) # Get the strings out of generated_resources.grd. dom = minidom.parse(kGeneratedResourcesPath) # message_nodes is a list of message dom nodes corresponding to the string # ids we care about. We want to make sure that this list is in the same # order as kStringIds so we can associate them together. message_nodes = [] all_message_nodes = dom.getElementsByTagName('message') for string_id in kStringIds: message_nodes.append([ x for x in all_message_nodes if x.getAttribute('name') == string_id ][0]) message_texts = [ node.firstChild.nodeValue.strip() for node in message_nodes ] # Generate the message ID of the string to correlate it with its translations # in the xtb files. translation_ids = [tclib.GenerateMessageId(text) for text in message_texts] # Manually put _EN_US in the list of translated strings because it doesn't # have a .xtb file. translated_strings = [] for string_id, message_text in zip(kStringIds, message_texts): translated_strings.append( TranslationStruct(string_id, 'EN_US', message_text)) # Gather the translated strings from the .xtb files. If an .xtb file doesn't # have the string we want, use the en-US string. for xtb_filename in kTranslationFiles: dom = minidom.parse(xtb_filename) language = dom.documentElement.getAttribute('lang') language = language.replace('-', '_').upper() translation_nodes = {} for translation_node in dom.getElementsByTagName('translation'): translation_id = translation_node.getAttribute('id') if translation_id in translation_ids: translation_nodes[translation_id] = ( translation_node.firstChild.nodeValue.strip()) for i, string_id in enumerate(kStringIds): translated_string = translation_nodes.get(translation_ids[i], message_texts[i]) translated_strings.append( TranslationStruct(string_id, language, translated_string)) translated_strings.sort() return translated_strings
def __ReadSourceAndTranslationsFrom(self, grd_file, xtb_files): """Reads source strings and translations for a .grd file. Reads the source strings and all available translations for the messages identified by self.string_id_set (or all the messages if self.string_id_set is empty). The source string is used where translations are missing. Args: grd_file: Path to a .grd file. xtb_files: List of paths to .xtb files. Returns: An unsorted list of __TranslationData instances. """ sax_parser = sax.make_parser() # Read the source (en-US) string from the .grd file. grd_handler = GrdHandler(self.string_id_set) sax_parser.setContentHandler(grd_handler) sax_parser.parse(grd_file) source_strings = grd_handler.messages grd_file_path = os.path.dirname(grd_file) source_xtb_files = [] for xtb_file in grd_handler.referenced_xtb_files: relative_xtb_file_path = (os.path.join(grd_file_path, xtb_file).replace( '\\', '/')) source_xtb_files.append(relative_xtb_file_path) missing_xtb_files = list(set(source_xtb_files) - set(xtb_files)) # Manually put the source strings as en-US in the list of translated # strings. translated_strings = [] for string_id, message_text in source_strings.iteritems(): translated_strings.append( self.__TranslationData(string_id, 'EN_US', message_text)) # Generate the message ID for each source string to correlate it with its # translations in the .xtb files. Multiple source strings may have the same # message text; hence the message id is mapped to a list of string ids # instead of a single value. translation_ids = {} for (string_id, message_text) in source_strings.iteritems(): message_id = tclib.GenerateMessageId(message_text) translation_ids.setdefault(message_id, []).append(string_id) # Track any xtb files that appear in the xtb folder but are not present in # the grd file. extra_xtb_files = [] # Gather the translated strings from the .xtb files. Use the en-US string # for any message lacking a translation. xtb_handler = XtbHandler(translation_ids) sax_parser.setContentHandler(xtb_handler) for xtb_filename in xtb_files: if not xtb_filename in source_xtb_files: extra_xtb_files.append(xtb_filename) sax_parser.parse(xtb_filename) for string_id, message_text in source_strings.iteritems(): translated_string = xtb_handler.translations.get( string_id, message_text) translated_strings.append( self.__TranslationData(string_id, xtb_handler.lang, translated_string)) if missing_xtb_files or extra_xtb_files: if missing_xtb_files: missing_error = ( "There were files that were found in the .grd file " "'{}' but do not exist on disk:\n{}") print missing_error.format(grd_file, '\n'.join(missing_xtb_files)) if extra_xtb_files: extra_error = ( "There were files that exist on disk but were not found " "in the .grd file '{}':\n{}") print extra_error.format(grd_file, '\n'.join(extra_xtb_files)) sys.exit(1) return translated_strings