|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Picard, the next-generation MusicBrainz tagger |
| 5 | +# |
| 6 | +# Copyright (C) 2020 Philipp Wolfer |
| 7 | +# |
| 8 | +# This program is free software; you can redistribute it and/or |
| 9 | +# modify it under the terms of the GNU General Public License |
| 10 | +# as published by the Free Software Foundation; either version 2 |
| 11 | +# of the License, or (at your option) any later version. |
| 12 | +# |
| 13 | +# This program is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License |
| 19 | +# along with this program; if not, write to the Free Software |
| 20 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | + |
| 22 | +import glob |
| 23 | +import json |
| 24 | +import os.path |
| 25 | + |
| 26 | +import nshutil as nsh |
| 27 | + |
| 28 | + |
| 29 | +def language_from_filename(path): |
| 30 | + lang = os.path.splitext(os.path.basename(path))[0] |
| 31 | + return (nsh.code_to_language(lang), lang) |
| 32 | + |
| 33 | + |
| 34 | +def write_langstring(f, language, identifier, text): |
| 35 | + langstring = nsh.make_langstring(language, identifier, text) |
| 36 | + f.write(langstring) |
| 37 | + |
| 38 | + |
| 39 | +def merge_translations(*translations): |
| 40 | + merged = {} |
| 41 | + for trans in translations: |
| 42 | + for k, v in trans.items(): |
| 43 | + if v: |
| 44 | + merged[k] = v |
| 45 | + return merged |
| 46 | + |
| 47 | + |
| 48 | +def main(): |
| 49 | + scriptdir = os.path.dirname(os.path.abspath(__file__)) |
| 50 | + sourcesdir = os.path.join(scriptdir, 'sources') |
| 51 | + outdir = os.path.join(scriptdir, 'out') |
| 52 | + os.makedirs(outdir, exist_ok=True) |
| 53 | + |
| 54 | + # Read the english sources for defaults |
| 55 | + with open(os.path.join(sourcesdir, 'en.json'), 'r', encoding='utf-8') as infile: |
| 56 | + data_en = json.loads(infile.read()) |
| 57 | + |
| 58 | + for path in glob.glob(os.path.join(sourcesdir, '*.json')): |
| 59 | + language, language_code = language_from_filename(path) |
| 60 | + if not language: |
| 61 | + print(f'Unknown language code "{language_code}", skipping') |
| 62 | + continue |
| 63 | + target_file = os.path.join(outdir, f'{language}.nsh') |
| 64 | + print(f'{path} => {target_file}') |
| 65 | + with open(path, 'r', encoding='utf-8') as infile: |
| 66 | + data = json.loads(infile.read()) |
| 67 | + data = merge_translations(data_en, data) |
| 68 | + with open(target_file, 'w+', encoding='utf-8') as outfile: |
| 69 | + for identifier, text in data.items(): |
| 70 | + write_langstring(outfile, language, identifier, text) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + main() |
0 commit comments