mirror of
https://github.com/fergalmoran/picard.git
synced 2025-12-22 09:18:18 +00:00
PICARD-2652: Script to fetch attributes / countries translations from Weblate
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -42,3 +42,4 @@ win-version-info.txt
|
||||
*~
|
||||
appledev.p12
|
||||
build.cfg
|
||||
.weblate.ini
|
||||
|
||||
@@ -4,3 +4,4 @@ flake8
|
||||
isort>=5.0
|
||||
pycodestyle
|
||||
pylint>=2.6.0
|
||||
wlc
|
||||
|
||||
125
scripts/tools/pull-shared-translations.py
Executable file
125
scripts/tools/pull-shared-translations.py
Executable file
@@ -0,0 +1,125 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Picard, the next-generation MusicBrainz tagger
|
||||
#
|
||||
# Copyright (C) 2023 Philipp Wolfer
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
import os
|
||||
import os.path
|
||||
import sys
|
||||
|
||||
from wlc import (
|
||||
Component,
|
||||
Weblate,
|
||||
)
|
||||
from wlc.config import WeblateConfig
|
||||
|
||||
|
||||
WEBLATE_URL = 'https://translations.metabrainz.org/api/'
|
||||
PROJECT_NAME = 'musicbrainz'
|
||||
PROJECT_COMPONENTS = (
|
||||
'attributes',
|
||||
'countries',
|
||||
)
|
||||
MIN_TRANSLATED_PERCENT = 10
|
||||
|
||||
|
||||
logging.basicConfig(
|
||||
force=True,
|
||||
format="%(asctime)s:%(levelname)s: %(message)s",
|
||||
level=logging.INFO,
|
||||
stream=sys.stderr,
|
||||
)
|
||||
|
||||
|
||||
def fetch_translations(component_name: str, user_key: str = '', config: WeblateConfig = None):
|
||||
weblate = Weblate(key=user_key, url=WEBLATE_URL, config=config)
|
||||
component = Component(weblate, f'components/{PROJECT_NAME}/{component_name}/')
|
||||
logging.info('Processing component %s...', component['name'])
|
||||
translations = component.list()
|
||||
source_language = component['source_language']['code']
|
||||
output_dir = get_output_dir(component_name)
|
||||
logging.info('Output dir: %s', output_dir)
|
||||
for translation in translations:
|
||||
# Skip source language and translation templates
|
||||
language_name = translation['language']['name']
|
||||
language_code = translation['language']['code']
|
||||
if (language_code == source_language
|
||||
or translation['translated_percent'] < MIN_TRANSLATED_PERCENT
|
||||
or translation['is_template']):
|
||||
logging.info('Skipping translation file for %s.', language_name)
|
||||
continue
|
||||
|
||||
logging.info('Downloading translation file for %s...', language_name)
|
||||
data = translation.download()
|
||||
output_path = os.path.join(output_dir, f'{language_code}.po')
|
||||
with open(output_path, 'bw') as output_file:
|
||||
output_file.write(data)
|
||||
|
||||
|
||||
def get_output_dir(component_name: str) -> str:
|
||||
path = os.path.join(os.path.dirname(__file__), '..', '..', 'po', component_name)
|
||||
os.makedirs(path, exist_ok=True)
|
||||
return path
|
||||
|
||||
|
||||
def load_config() -> WeblateConfig:
|
||||
config_path = os.path.join(os.path.dirname(__file__), '..', '..', '.weblate.ini')
|
||||
if os.path.exists:
|
||||
config = WeblateConfig()
|
||||
config.load(config_path)
|
||||
return config
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
prog='pull-shared-translations',
|
||||
description=(
|
||||
'Fetches the translations for attributes and countries from '
|
||||
'the MusicBrainz Server project on Weblate.'
|
||||
),
|
||||
epilog=(
|
||||
'Instead of passing the --key parameter the key can also be set in '
|
||||
'a file .weblate.ini in the repositories root directory. See '
|
||||
'po/README.md for details.'
|
||||
))
|
||||
parser.add_argument('-k', '--key', help='Weblate user key')
|
||||
args = parser.parse_args()
|
||||
|
||||
config = None
|
||||
if not args.key:
|
||||
config = load_config()
|
||||
if not config:
|
||||
parser.print_usage()
|
||||
parser.error('No Weblate user key specified. See po/README.md for details.')
|
||||
url, key = config.get_url_key()
|
||||
if not key or url != WEBLATE_URL:
|
||||
parser.print_usage()
|
||||
parser.error('Invalid .weblate.ini. See po/README.md for details.')
|
||||
|
||||
for component_name in PROJECT_COMPONENTS:
|
||||
fetch_translations(component_name, user_key=args.key, config=config)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.debug("Starting...")
|
||||
main()
|
||||
Reference in New Issue
Block a user