make_images: support fallback English translations ourselves

Grit has changed behavior where it won't write out the fallback
English translations to the non-English messages.  That means we
have to write out the English database ourselves, and then load
the missing content from it on the fly.

This makes it easy to issue warnings when translations are missing.

BUG=chromium:757792
TEST=generating screens with new & old grit are the same

Change-Id: I4061d04377c18ba6d9eb7946591a6a9728d4f314
Reviewed-on: https://chromium-review.googlesource.com/1319409
Commit-Ready: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-by: Amin Hassani <ahassani@chromium.org>
diff --git a/recovery/localized_text/cros_recovery.grd b/recovery/localized_text/cros_recovery.grd
index f5d735c..67f5d6a 100644
--- a/recovery/localized_text/cros_recovery.grd
+++ b/recovery/localized_text/cros_recovery.grd
@@ -16,6 +16,7 @@
     <output filename="de.json" type="chrome_messages_json" lang="de" />
     <output filename="el.json" type="chrome_messages_json" lang="el" />
     <output filename="en-GB.json" type="chrome_messages_json" lang="en-GB" />
+    <output filename="en.json" type="chrome_messages_json" lang="en" />
     <output filename="en-US.json" type="chrome_messages_json" lang="en-US" />
     <output filename="es.json" type="chrome_messages_json" lang="es" />
     <output filename="es-419.json" type="chrome_messages_json" lang="es-419" />
diff --git a/recovery/make_images b/recovery/make_images
index 0d956b8..bcd2ba9 100755
--- a/recovery/make_images
+++ b/recovery/make_images
@@ -368,6 +368,12 @@
   print "wrote %s" % os.path.basename(filename)
 
 
+def _LoadMessages(filename):
+  """Load messaages from |filename|."""
+  with io.open(filename, encoding='utf-8-sig') as fp:
+    return json.load(fp)
+
+
 def CreateLocales(textdir, screendir, locale_list):
   """Create message images for all locales.
 
@@ -399,6 +405,9 @@
         '-o', os.path.join(tmpdir)
     ])
 
+    # Newer grit will omit fallback messages, so we have to provide them.
+    en_translations = _LoadMessages(os.path.join(tmpdir, 'en.json'))
+
     for locale in locale_list:
       print 'Locale %s' % locale
       locale_screen = os.path.join(screendir, locale)
@@ -409,9 +418,15 @@
         os.link(empty_file, locale_empty)
 
       # Read the JSON file to obtain translated messages.
-      msgfilename = os.path.join(tmpdir, locale + '.json')
-      with io.open(msgfilename, encoding='utf-8-sig') as msgfile:
-        translations = json.load(msgfile)
+      translations = _LoadMessages(os.path.join(tmpdir, '%s.json' % (locale,)))
+      # Overlay the English fallbacks.
+      missing = set()
+      for msgid in en_translations.keys():
+        if msgid not in translations:
+          missing.add(str(msgid))
+          translations[msgid] = en_translations[msgid]
+      if missing and locale not in {'en-US'}:
+        print('  missing translations: %s' % (' '.join(sorted(missing)),))
 
       # Generate an image file for translated message.
       for (tag, msgdict) in translations.iteritems():