From dfff815da01d864e3b79bb49a86830df7070dd8b Mon Sep 17 00:00:00 2001 From: "thakis@chromium.org" Date: Mon, 5 Nov 2012 18:01:44 +0000 Subject: Revert 165041 (caused http://crbug.com/158995, which is apparently somewhat tricky to fix -- this can reland with the fix) - Move Chromoting strings to string_resources.grd. Previously all strings were in messages.json files. Moving them to GRD file will allow to share them with native components. Also added script that I used to convert translations - to make it easier to review this CL and just in case we need it again in the future. Review URL: https://chromiumcodereview.appspot.com/11150008 TBR=sergeyu@chromium.org Review URL: https://codereview.chromium.org/11312087 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@165953 0039d316-1c4b-4281-b951-d872f2087c98 --- remoting/tools/json_to_grd.py | 190 ----------------------------- remoting/tools/remove_official_branding.py | 14 +-- 2 files changed, 4 insertions(+), 200 deletions(-) delete mode 100755 remoting/tools/json_to_grd.py (limited to 'remoting/tools') diff --git a/remoting/tools/json_to_grd.py b/remoting/tools/json_to_grd.py deleted file mode 100755 index 36a26c9..0000000 --- a/remoting/tools/json_to_grd.py +++ /dev/null @@ -1,190 +0,0 @@ -#!/usr/bin/env python -# Copyright (c) 2012 The Chromium Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. -# -# This script can be used to convert messages.json to grd translations. - -import codecs -import json -import optparse -import os -import string -import sys -import xml.dom.minidom as dom - -import sys -sys.path.append('../../tools/grit') -sys.path.append('../../tools/grit/grit') -import tclib - -def camelCase2ALL_CAPS(s): - for c in string.ascii_uppercase: - s = s.replace(c, '_' + c) - return s.upper() - -def load_messages(filename): - import collections - def create_ordered_dict(list): - return collections.OrderedDict(list) - messages = json.load(file(filename), object_pairs_hook=create_ordered_dict) - ids = messages.keys() - - result = {} - for id in ids: - text = messages[id]['message'] - placeholders = [] - if messages[id].has_key('placeholders'): - for name, p in messages[id]['placeholders'].items(): - index = p['content'] - caps_name = camelCase2ALL_CAPS(name) - placeholders.append(tclib.Placeholder(caps_name, index, p['example'])) - text = text.replace('$' + name + '$', caps_name) - - msg = tclib.Message(text, placeholders, - messages[id]['description']) - result[id] = msg - - return ids, result - -def json_to_grd(input_dir, output_dir): - # load list of string IDs and placeholder names from the untranslated file - # because order of messages and placeholder names are not persisted in - # translations. - message_ids, messages = load_messages( - os.path.join(input_dir, '_locales/en', 'messages.json')) - - grd_name = os.path.join(output_dir, - 'string_resources.grd') - grd_xml = dom.parse(grd_name) - grd_translations = grd_xml.getElementsByTagName('translations')[0] - grd_outputs = grd_xml.getElementsByTagName('outputs')[0] - grd_messages = grd_xml.getElementsByTagName('messages')[0] - - for id in message_ids: - grit_id = 'IDR_' + id - message = grd_xml.createElement('message') - grd_messages.appendChild(grd_xml.createTextNode(' ')) - grd_messages.appendChild(message) - grd_messages.appendChild(grd_xml.createTextNode('\n ')) - message.setAttribute('name', grit_id) - message.setAttribute('desc', messages[id].description) - message.appendChild(grd_xml.createTextNode('\n ')) - for part in messages[id].parts: - if isinstance(part, tclib.Placeholder): - ph = grd_xml.createElement('ph') - message.appendChild(ph) - ph.setAttribute('name', part.presentation) - ph.appendChild(grd_xml.createTextNode(part.original)) - ex = grd_xml.createElement('ex') - ph.appendChild(ex) - ex.appendChild(grd_xml.createTextNode(part.example)) - else: - message.appendChild(grd_xml.createTextNode(part)) - message.appendChild(grd_xml.createTextNode('\n ')) - - translations_dir = os.path.join(input_dir, '_locales.official') - lang_list = os.listdir(translations_dir) - lang_list.sort() - for lang in lang_list: - # Convert locale name representation to the Grit format. - grit_lang = lang.replace('_', '-') if lang != 'en' else 'en_US' - - # Load translations - translations_file = file( - os.path.join(translations_dir, lang, 'messages.json')) - translations = json.load(translations_file) - translations_file.close() - - # Uppercase all string name. - translations = dict(zip(map(lambda x: x.upper(), translations.iterkeys()), - translations.values())) - - doc = dom.Document() - - bundle = doc.createElement('translationbundle') - doc.appendChild(bundle) - bundle.setAttribute('lang', grit_lang) - written_translations = {} - for id in message_ids: - bundle.appendChild(doc.createTextNode('\n')) - - if not translations.has_key(id): - # Skip not translated messages. - continue - grit_id = 'IDR_' + id - - placeholders = messages[id].placeholders - text = translations[id]['message'] - for p in placeholders: - text = text.replace(p.original, p.presentation) - - msg_id = messages[id].GetId() - if written_translations.has_key(msg_id): - # We've already written translation for this message. - continue - parsed_translation = tclib.Translation(text, msg_id, placeholders) - written_translations[msg_id] = 1 - - translation = doc.createElement('translation') - bundle.appendChild(translation) - translation.setAttribute('id', parsed_translation.GetId()) - for part in parsed_translation.parts: - if isinstance(part, tclib.Placeholder): - ph = doc.createElement('ph') - translation.appendChild(ph) - ph.setAttribute('name', part.presentation) - else: - translation.appendChild(doc.createTextNode(part)) - bundle.appendChild(doc.createTextNode('\n')) - - out_name = os.path.join(output_dir, - 'string_resources_' + grit_lang + '.xtb') - out = codecs.open(out_name, mode='w', encoding = 'UTF-8') - out.write('\n' - '\n') - bundle.writexml(out) - out.close() - - if grit_lang == 'en_US': - continue - - t = grd_xml.createElement('file') - grd_translations.appendChild(grd_xml.createTextNode(' ')) - grd_translations.appendChild(t) - grd_translations.appendChild(grd_xml.createTextNode('\n ')) - t.setAttribute('path', 'string_resources_' + grit_lang + '.xtb') - t.setAttribute('lang', grit_lang) - - t = grd_xml.createElement('output') - grd_outputs.appendChild(grd_xml.createTextNode(' ')) - grd_outputs.appendChild(t) - grd_outputs.appendChild(grd_xml.createTextNode('\n ')) - t.setAttribute('filename', 'remoting/resources/' + grit_lang + '.pak') - t.setAttribute('type', 'data_package') - t.setAttribute('lang', grit_lang) - - t = grd_xml.createElement('output') - grd_outputs.appendChild(grd_xml.createTextNode(' ')) - grd_outputs.appendChild(t) - grd_outputs.appendChild(grd_xml.createTextNode('\n ')) - t.setAttribute('filename', - 'remoting/webapp/_locales/' + lang + '/messages.json') - t.setAttribute('type', 'chrome_messages_json') - t.setAttribute('lang', grit_lang) - - grd_out = codecs.open(grd_name + '_new', mode='w', encoding = 'UTF-8') - grd_xml.writexml(grd_out, encoding = 'UTF-8') - grd_out.close() - -def main(): - usage = 'Usage: json_to_grd ' - parser = optparse.OptionParser(usage=usage) - options, args = parser.parse_args() - if len(args) != 2: - parser.error('two positional arguments expected') - - return json_to_grd(args[0], args[1]) - -if __name__ == '__main__': - sys.exit(main()) diff --git a/remoting/tools/remove_official_branding.py b/remoting/tools/remove_official_branding.py index 9d9d4cb..06f9f81 100755 --- a/remoting/tools/remove_official_branding.py +++ b/remoting/tools/remove_official_branding.py @@ -17,24 +17,18 @@ def update_xml_node(element): child.replaceWholeText( child.data.replace("Chrome Remote Desktop", "Chromoting")) -def remove_official_branding(input_file, output_file): - xml = minidom.parse(input_file) +def remove_official_branding(input, output): + xml = minidom.parse(input) # Remove all translations. for translations in xml.getElementsByTagName("translations"): - for translation in translations.getElementsByTagName("file"): + for translation in xml.getElementsByTagName("translation"): translations.removeChild(translation) - for outputs in xml.getElementsByTagName("outputs"): - for output in outputs.getElementsByTagName("output"): - lang = output.getAttribute("lang") - if lang and lang != "en": - outputs.removeChild(output) - # Update branding. update_xml_node(xml) - out = file(output_file, "w") + out = file(output, "w") out.write(xml.toxml(encoding = "UTF-8")) out.close() -- cgit v1.1