From 0b6e1ca4884bdbeda25624c02640902b749fd268 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 13 Dec 2018 14:50:33 +0100 Subject: [PATCH] i18n: update list of translations from Transifex - Add automatic tooling to retrieve translation stats and output new CMake variable settings. - If there are i18n language selection warnings, stop CMake. --- CMakeLists.txt | 19 +++++++---- ci/txstats.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 ci/txstats.py diff --git a/CMakeLists.txt b/CMakeLists.txt index faaab2283..47054bd8c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,12 +103,12 @@ set( CALAMARES_VERSION_RC 1 ) # checks for new languages and misspelled ones are done (that is, # copy these four lines to four backup lines, add "p", and then update # the original four lines with the current translations). -set( _tx_complete ca zh_TW hr cs_CZ da et fr id it_IT lt pl pt_PT es_MX tr_TR ) -set( _tx_good sq de pt_BR zh_CN ja ro es sk ) -set( _tx_ok hu ru he nl bg uk ast is ko ar sv el gl en_GB - th fi_FI hi eu nb sr sl sr@latin mr es_PR kn kk be ) -set( _tx_bad fr_CH gu lo fa ur uz eo ) - +set( _tx_complete zh_TW tr_TR sk pt_PT pt_BR pl lt ja id hr gl fr + et de da cs_CZ ca ) +set( _tx_good es sq es_MX zh_CN it_IT he en_GB ru ro hi bg hu ) +set( _tx_ok uk nl ast ko is ar sv el th fi_FI eu sr nb sl + sr@latin mr es_PR ) +set( _tx_bad eo kk kn uz ur mk lo gu fr_CH fa be ) ### Required versions # @@ -316,12 +316,14 @@ endif() # set( prev_tx ${p_tx_complete} ${p_tx_good} ${p_tx_ok} ${p_tx_bad} ) set( curr_tx ${_tx_complete} ${_tx_good} ${_tx_ok} ${_tx_bad} ) +set( tx_errors OFF ) if ( prev_tx ) # Gone in new list foreach( l ${prev_tx} ) list( FIND curr_tx ${l} p_l ) if( p_l EQUAL -1 ) message(WARNING "Language ${l} was present in previous translations and is now absent.") + set( tx_errors ON ) endif() endforeach() @@ -330,10 +332,12 @@ if ( prev_tx ) list( FIND prev_tx ${l} p_l ) if( p_l EQUAL -1 ) message(WARNING "Language ${l} is new.") + set( tx_errors ON ) endif() set( p_l "lang/calamares_${l}.ts" ) if( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${p_l} ) message(WARNING "Language ${l} has no .ts file yet.") + set( tx_errors ON ) endif() endforeach() @@ -342,6 +346,9 @@ if ( prev_tx ) endif() unset( prev_tx ) unset( curr_tx ) +if( tx_errors ) + message( FATAL_ERROR "Translation warnings, see above." ) +endif() set( CALAMARES_TRANSLATION_LANGUAGES en ${_tx_complete} ${_tx_good} ${_tx_ok} ) list( SORT CALAMARES_TRANSLATION_LANGUAGES ) diff --git a/ci/txstats.py b/ci/txstats.py new file mode 100644 index 000000000..368ce503e --- /dev/null +++ b/ci/txstats.py @@ -0,0 +1,87 @@ +#! /usr/bin/env python +# +# Uses the Transifex API to get a list of enabled languages, +# and outputs CMake settings for inclusion into CMakeLists.txt. +import sys + +def get_tx_credentials(): + """ + Gets the API token out of the user's .transifexrc (this is supposed + to be secure). + """ + import configparser + import os + txconfig_name = os.path.expanduser("~/.transifexrc") + try: + with open(txconfig_name, "r") as f: + parser = configparser.SafeConfigParser() + parser.readfp(f) + + return parser.get("https://www.transifex.com", "password") + except IOError as e: + return None + +def output_langs(all_langs, label, filterfunc): + """ + Output (via print) all of the languages in @p all_langs + that satisfy the translation-percentage filter @p filterfunc. + Prints a CMake set() command with the @p label as part + of the variable name. + + Performs line-wrapping. + """ + these_langs = [l for s, l in all_langs if filterfunc(s)] + out = " ".join(["set( _tx_%s" % label, " ".join(these_langs), ")"]) + width = 68 + prefix = "" + + while len(out) > width - len(prefix): + chunk = out[:out[:width].rfind(" ")] + print("%s%s" % (prefix, chunk)) + out = out[len(chunk)+1:] + prefix = " " + print("%s%s" % (prefix, out)) + +def get_tx_stats(token): + """ + Does an API request to Transifex with the given API @p token, getting + the translation statistics for the main body of texts. Then prints + out CMake settings to replace the _tx_* variables in CMakeLists.txt + according to standard criteria. + """ + import requests + + r = requests.get("https://api.transifex.com/organizations/calamares/projects/calamares/resources/calamares-master/", auth=("api", token)) + if r.status_code != 200: + return 1 + + all_langs = [] + + j = r.json() + languages = j["stats"] + print("# Total %d languages" % len(languages)) + for lang_name in languages: + stats = languages[lang_name]["translated"]["percentage"] + all_langs.append((stats, lang_name)) + + all_langs.sort(reverse=True) + + output_langs(all_langs, "complete", lambda s : s == 1.0) + output_langs(all_langs, "good", lambda s : 1.0 > s >= 0.75) + output_langs(all_langs, "ok", lambda s : 0.75 > s >= 0.05) + output_langs(all_langs, "bad", lambda s : 0.05 > s) + + return 0 + + +def main(): + cred = get_tx_credentials() + if cred: + return get_tx_stats(cred) + else: + print("! Could not find API token in ~/.transifexrc") + return 1 + return 0 + +if __name__ == "__main__": + sys.exit(main())