From c4327a940e30082a9ca6487246922b43fc61d8c1 Mon Sep 17 00:00:00 2001 From: Shaun McCance Date: Tue, 16 Jun 2015 10:24:41 -0400 Subject: [PATCH] Try to get language code from PO header instead of filename https://bugs.freedesktop.org/show_bug.cgi?id=90937 --- itstool.in | 17 +++++++++++++++-- 1 files changed, 15 insertions(+), 2 deletions(-) diff --git a/itstool.in b/itstool.in index d0e76a1..6c93e37 100755 --- a/itstool.in +++ b/itstool.in @@ -1393,6 +1393,19 @@ def convert_locale (locale): ret += '-' + variant[1:].lower() return ret +def get_po_language(translations, filename): + lang = None + # PO files usually store the language in the header, which is encoded + # in PO as the translation for the empty string. If it's not in the + # header, fall back to the file basename. + for line in (translations.ugettext('') or '').split('\n'): + if line.startswith('Language:'): + lang=line[9:].strip() + break + if lang is None: + lang = os.path.splitext(os.path.basename(filename))[0] + return convert_locale(lang) + if __name__ == '__main__': options = optparse.OptionParser() @@ -1497,7 +1510,7 @@ if __name__ == '__main__': sys.exit(1) translations.add_fallback(NoneTranslations()) if opts.lang is None: - opts.lang = convert_locale(os.path.splitext(os.path.basename(opts.merge))[0]) + opts.lang = get_po_language(translations, opts.merge) if opts.output is None: out = './' elif os.path.isdir(opts.output): @@ -1535,7 +1548,7 @@ if __name__ == '__main__': sys.stderr.write('Error: cannot open mo file %s\n' % filename) sys.exit(1) thistr.add_fallback(NoneTranslations()) - lang = convert_locale(os.path.splitext(os.path.basename(filename))[0]) + lang = get_po_language(thistr, filename) translations[lang] = thistr if opts.output is None: out = sys.stdout -- 1.7.3.2