@ -4,7 +4,6 @@ import json
import os
import sys
# This allows for importing from the localization and util directories NOTE: Auto importing tools will also prepend the import paths with "tools." this will not work and needs to be removed from import paths
sys . path . append ( os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , " .. " ) ) )
@ -13,74 +12,78 @@ from util.time import ExecutionTimer;
timer = ExecutionTimer ( )
from dynamicVariables import (
extractVariablesFromDict ,
identifyLocaleDyanmicVariableDifferences ,
prettyPrintIssuesTable ,
identifyAndPrintOldDynamicVariables ,
extractVariablesFromDict ,
identifyLocaleDyanmicVariableDifferences ,
prettyPrintIssuesTable ,
identifyAndPrintOldDynamicVariables ,
)
from localization . localeTypes import generateLocalesType
from util . logger import console
from util . fileUtils import createMappedJsonFileDictionary , writeFile
# If the --throw-error-on-missing flag is passed, the script will exit with an error if there are any missing keys or dynamic variables
# This is useful for CI/CD pipelines to ensure that all translations are consistent
parser = argparse . ArgumentParser ( description = " Generate locale files " )
parser . add_argument (
" --error-on-problems " ,
action = " store_true " ,
help = " Exit with an error if there are any missing keys or dynamic variables " ,
" --error-on-problems " ,
action = " store_true " ,
help = " Exit with an error if there are any missing keys or dynamic variables " ,
)
parser . add_argument (
" --error-old-dynamic-variables " ,
action = " store_true " ,
help = " Exit with an error if there are any old dynamic variables " ,
)
parser . add_argument (
" --error-old-dynamic-variables " ,
action = " store_true " ,
help = " Exit with an error if there are any old dynamic variables " ,
" --print-problem s" ,
action = " store_true " ,
help = " Print the problems table " ,
)
parser . add_argument (
" --print-problem s" ,
action = " store_true " ,
help = " Print the problem s table " ,
" --print-problem -string s" ,
action = " store_true " ,
help = " Print the problem strings and which locales they are in " ,
)
parser . add_argument (
" --write-problems " , action = " store_true " , help = " Write the problems to a file "
" --write-problems " , action = " store_true " , help = " Write the problems to a file "
)
parser . add_argument (
" --problems-file " ,
default = " ./tools/localization/output/problems.json " ,
help = " The file to write the problems to " ,
" --problems-file " ,
default = " ./tools/localization/output/problems.json " ,
help = " The file to write the problems to " ,
)
parser . add_argument (
" --print-old-dynamic-variables " ,
action = " store_true " ,
help = " The file to write the problems to " ,
" --print-old-dynamic-variables " ,
action = " store_true " ,
help = " The file to write the problems to " ,
)
parser . add_argument ( " --en-only " , action = " store_true " , help = " Only check the en locale " )
parser . add_argument ( " --debug " , action = " store_true " , help = " Enable debug mode " )
parser . add_argument (
" --dict-dir " ,
type = str ,
default = " ./_locales "
" --dict-dir " ,
type = str ,
default = " ./_locales "
)
parser . add_argument (
" --dict-file-name " ,
type = str ,
default = " messages.json " ,
" --dict-file-name " ,
type = str ,
default = " messages.json " ,
)
parser . add_argument (
" --en-file-path " ,
type = str ,
default = " ./_locales/en/messages.json " ,
" --en-file-path " ,
type = str ,
default = " ./_locales/en/messages.json " ,
)
parser . add_argument (
" --generate-types " ,
action = " store_true " ,
help = " Generate the types file " ,
" --generate-types " ,
action = " store_true " ,
help = " Generate the types file " ,
)
args = parser . parse_args ( )
if args . debug :
console . enableDebug ( )
console . enableDebug ( )
GENERATE_TYPES = args . generate_types
OUTPUT_DIR = " ./ts/localization "
@ -91,7 +94,7 @@ INPUT_DIR = args.dict_dir
locales , localeFiles = createMappedJsonFileDictionary ( INPUT_DIR , args . dict_file_name )
if args . en_only :
locales = { " en " : locales [ " en " ] }
locales = { " en " : locales [ " en " ] }
# Generate the locales type and write it to a file
if GENERATE_TYPES :
@ -103,58 +106,73 @@ localeVariablesOld = dict()
# Extract the dynamic variables from each locale and store them in a dictionary
for locale , data in locales . items ( ) :
console . debug ( f " Extracting dynamic variables for { locale } " )
(
localeVariables [ locale ] ,
localeVariablesOld [ locale ] ,
) = extractVariablesFromDict ( data )
console . debug ( f " Extracting dynamic variables for { locale } " )
(
localeVariables [ locale ] ,
localeVariablesOld [ locale ] ,
) = extractVariablesFromDict ( data )
problems = identifyLocaleDyanmicVariableDifferences ( localeVariables )
found_old_dynamic_variables = identifyAndPrintOldDynamicVariables (
localeVariablesOld , args . print_old_dynamic_variables
localeVariablesOld , args . print_old_dynamic_variables
)
# Wrapping up the script and printing out the results
if problems :
message = " There are issues with the locales. "
if args . print_problems :
prettyPrintIssuesTable ( problems )
message + = " See above for details. "
if args . write_problems :
writeFile ( args . problems_file , json . dumps ( problems , indent = 2 ) )
console . info ( f " Problems written to { args . problems_file } " )
message + = f " Problems written to { args . problems_file } "
if not args . print_problems and not args . write_problems :
message + = " Run the script with --print-problems or --write-problems to see the problems. "
console . warn ( message )
message = " There are issues with the locales. "
if args . print_problem_strings :
string_to_locales = { }
for locale , locale_problems in problems . items ( ) :
if " additional_variables " in locale_problems :
for problem_string in locale_problems [ " additional_variables " ] . keys ( ) :
if problem_string not in string_to_locales :
string_to_locales [ problem_string ] = [ locale ]
else :
string_to_locales [ problem_string ] . append ( locale )
if " missing_variables " in locale_problems :
for problem_string in locale_problems [ " missing_variables " ] . keys ( ) :
if problem_string not in string_to_locales :
string_to_locales [ problem_string ] = [ locale ]
else :
string_to_locales [ problem_string ] . append ( locale )
console . info ( f " Problem strings: { json . dumps ( string_to_locales , indent = 2 ) } " )
message + = " See above for problem strings and which locales they are in. "
if args . print_problems :
prettyPrintIssuesTable ( problems )
message + = " See above for details. "
if args . write_problems :
writeFile ( args . problems_file , json . dumps ( problems , indent = 2 ) )
console . info ( f " Problems written to { args . problems_file } " )
message + = f " Problems written to { args . problems_file } "
if not args . print_problems and not args . write_problems :
message + = " Run the script with --print-problems or --write-problems to see the problems. "
console . warn ( message )
if found_old_dynamic_variables :
warning_message = (
warning_message = (
" Old dynamic variables were found in the locales. Please update the locales to use the new dynamic variables. "
)
if args . print_old_dynamic_variables :
if args . print_problems :
warning_message + = " See above for details (before the problems table). "
else :
warning_message + = " See above for details. "
)
if args . print_old_dynamic_variables :
if args . print_problems :
warning_message + = " See above for details (before the problems table). "
else :
warning_message + = " Run the script with --print-old-dynamic-variables to see the old dynamic variables. "
console . warn ( warning_message )
warning_message + = " See above for details. "
else :
warning_message + = " Run the script with --print-old-dynamic-variables to see the old dynamic variables. "
console . warn ( warning_message )
console . debug ( " Locales generation complete " )
timer . stop ( )
if ( args . error_on_problems and problems ) or (
args . error_old_dynamic_variables and found_old_dynamic_variables
args . error_old_dynamic_variables and found_old_dynamic_variables
) :
sys . exit ( 1 )
sys . exit ( 1 )