Reviewers: Nico, Jamie
CL:
https://codereview.chromium.org/1410023007/
Description:
Skip missing translations when generating JSON resources.
It doesn't make sense to replace missing translations with english
strings because Chrome can do this automatically when getting
resources.
BUG=369572
Base URL:
https://chromium.googlesource.com/external/grit-i18n.git@master
Affected files (+44, -5 lines):
M grit/clique.py
M grit/format/chrome_messages_json.py
M grit/format/chrome_messages_json_unittest.py
Index: grit/clique.py
diff --git a/grit/clique.py b/grit/clique.py
index
3a979890b8eab9c8aa4d2046f73b896d6766b934..f86aadfac5e9136e8b8433486cc1afffeb5c37a8
100644
--- a/grit/clique.py
+++ b/grit/clique.py
@@ -365,7 +365,8 @@ class MessageClique(object):
if custom_type and not custom_type.Validate(self.GetMessage()):
raise exception.InvalidMessage(self.GetMessage().GetRealContent())
- def MessageForLanguage(self, lang, pseudo_if_no_match=True,
fallback_to_english=False):
+ def MessageForLanguage(self, lang, pseudo_if_no_match=True,
+ fallback_to_english=False,
none_if_no_match=False):
'''Returns the message/translation for the specified language,
providing
a pseudotranslation if there is no available translation and a pseudo-
translation is requested.
@@ -377,6 +378,7 @@ class MessageClique(object):
lang: 'en'
pseudo_if_no_match: True
fallback_to_english: False
+ none_if_no_match: False
Return:
tclib.BaseMessage
@@ -401,9 +403,13 @@ class MessageClique(object):
# If we're not supposed to generate pseudotranslations, we add an error
# report to a list of errors, then fail at a higher level, so that we
# get a list of all messages that are missing translations.
- if not pseudo_if_no_match:
- self.uber_clique._AddMissingTranslation(lang, self, is_error=True)
+ if pseudo_if_no_match:
+ return pseudo.PseudoMessage(self.GetMessage())
+ if none_if_no_match:
+ return None
+
+ self.uber_clique._AddMissingTranslation(lang, self, is_error=True)
return pseudo.PseudoMessage(self.GetMessage())
def AllMessagesThatMatch(self, lang_re, include_pseudo = True):
Index: grit/format/chrome_messages_json.py
diff --git a/grit/format/chrome_messages_json.py
b/grit/format/chrome_messages_json.py
index
be934ab1175924657a79796dbf0def4c2464ec5f..4c3a473a573afc7daa3452cd224ee5db23beb344
100644
--- a/grit/format/chrome_messages_json.py
+++ b/grit/format/chrome_messages_json.py
@@ -28,8 +28,18 @@ def Format(root, lang='en', output_dir='.'):
if id.startswith('IDR_') or id.startswith('IDS_'):
id = id[4:]
- loc_message = encoder.encode(child.ws_at_start +
child.Translate(lang) +
- child.ws_at_end)
+ cliques = child.GetCliques()
+ assert cliques
+ translation = cliques[0].MessageForLanguage(
+ lang, pseudo_if_no_match=child.PseudoIsAllowed(),
+ fallback_to_english=False, none_if_no_match=True)
+
+ # Skip the string if it's not translated.
+ if translation == None:
+ continue;
+
+ loc_message = encoder.encode(
+ child.ws_at_start + translation.GetRealContent() +
child.ws_at_end)
if not first:
yield ',\n'
Index: grit/format/chrome_messages_json_unittest.py
diff --git a/grit/format/chrome_messages_json_unittest.py
b/grit/format/chrome_messages_json_unittest.py
index
373751eadc868c4ca5a7cf5fa7bb2eb826e35cea..4386247885e8949c24ca8f8c555be2349c9a24b1
100644
--- a/grit/format/chrome_messages_json_unittest.py
+++ b/grit/format/chrome_messages_json_unittest.py
@@ -107,6 +107,29 @@ class
ChromeMessagesJsonFormatUnittest(unittest.TestCase):
"""
self.assertEqual(test.strip(), output.strip())
+ def testSkipMissingTranslations(self):
+ grd = """<?xml version="1.0" encoding="UTF-8"?>
+<grit latest_public_release="2" current_release="3" source_lang_id="en"
base_dir="%s">
+ <outputs>
+ </outputs>
+ <release seq="3" allow_pseudo="False">
+ <messages>
+ <message name="ID_HELLO_NO_TRANSLATION">Hello not
translated</message>
+ </messages>
+ </release>
+</grit>"""
+ root = grd_reader.Parse(StringIO.StringIO(grd), dir=".")
+
+ buf = StringIO.StringIO()
+ build.RcBuilder.ProcessNode(root,
DummyOutput('chrome_messages_json', 'fr'), buf)
+ output = buf.getvalue()
+ test = u"""
+{
+
+}
+"""
+ self.assertEqual(test.strip(), output.strip())
+
class DummyOutput(object):