localization script refactoring

This commit is contained in:
Ivan Smolin 2021-07-21 17:17:22 +03:00
parent 73abd1564f
commit cf2aa3f7ee
2 changed files with 35 additions and 15 deletions

View File

@ -1,14 +1,13 @@
<?php
$PRODUCT_NAME = $argv[1];
$LOCALIZATION_PATH = $argv[1];
$COMMON_STRINGS_PATH = $argv[2];
$BUNDLE = $argv[3];
function createFolder($path) {
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
}
$localization = './'.$PRODUCT_NAME.'/Resources/Localization/';
$baseFile = file_get_contents(array_pop(glob($COMMON_STRINGS_PATH.'/default*.json')));
$baseJson = json_decode($baseFile, true);
@ -31,13 +30,13 @@
}
$ios_strings = preg_replace('/(\\\\)(u)([0-9a-fA-F]{4})/', '$1U$3', $ios_strings);
$lproj = $localization.$languageName.'.lproj/';
$lproj = $LOCALIZATION_PATH.$languageName.'.lproj/';
createFolder($lproj);
file_put_contents($lproj.'Localizable.strings', $ios_strings);
if($isBase) {
createFolder($localization.'Base.lproj/');
file_put_contents($localization.'Base.lproj/Localizable.strings', $ios_strings);
createFolder($LOCALIZATION_PATH.'Base.lproj/');
file_put_contents($LOCALIZATION_PATH.'Base.lproj/Localizable.strings', $ios_strings);
$ios_swift_strings = 'import Foundation'.PHP_EOL.PHP_EOL.
'// swiftlint:disable superfluous_disable_command'.PHP_EOL.
'// swiftlint:disable line_length'.PHP_EOL.
@ -46,10 +45,11 @@
'public extension String {'.PHP_EOL;
foreach ($json as $key=>$value) {
$value_without_linefeed = preg_replace("/\r|\n/", " ", $value);
$ios_swift_strings .= "\t/// ".$value_without_linefeed."\n\t".'static let '.preg_replace_callback('/_(.?)/', function ($m) { return strtoupper($m[1]); }, $key).' = NSLocalizedString("'.$key.'", comment: "")'."\n".PHP_EOL;
$ios_swift_strings .= "\t/// ".$value_without_linefeed."\n\t".'static let '.preg_replace_callback('/_(.?)/', function ($m) { return strtoupper($m[1]); }, $key).' = NSLocalizedString("'.$key.'", bundle: '.$BUNDLE.', comment: "'.$value_without_linefeed.'")'."\n".PHP_EOL;
}
$ios_swift_strings .= '}'.PHP_EOL;
file_put_contents($localization.'String+Localization.swift', $ios_swift_strings);
echo $ios_swift_strings;
file_put_contents($LOCALIZATION_PATH.'String+Localization.swift', $ios_swift_strings);
}
}
?>

View File

@ -1,16 +1,36 @@
LOCALIZATION_PATH="${PRODUCT_NAME}/Resources/Localization"
#first argument set strings folder path
#!/bin/sh
# Description:
# Generates Localizeable.strings and String+Localization.swift files.
#
# Parameters:
# $1 - path to strings folder containing json files.
# $2 - path to Localization folder (output).
# $3 - Bundle for localization. Default is `.main`.
#
# Required environment variables:
# SCRIPT_DIR - directory of current script.
#
# Optional environment variables:
# PRODUCT_NAME - product name to produce path to localization folder (output).
#
# Examples of usage:
# . localization.sh
# . localization.sh common/strings Resources/Localization/ .main
#
STRINGS_FOLDER=${1:-"common/strings"}
LOCALIZATION_PATH=${2:-"${PRODUCT_NAME}/Resources/Localization/"}
BUNDLE=${3:-".main"}
if ! [ -e ${LOCALIZATION_PATH} ]; then
echo "${PROJECT_DIR}/${LOCALIZATION_PATH} path does not exist. Add these folders and try again."
echo "${LOCALIZATION_PATH} path does not exist. Add these folders and try again."
exit 1
fi
if ! [ -e "${PROJECT_DIR}/${STRINGS_FOLDER}" ]; then
echo "${PROJECT_DIR}/${STRINGS_FOLDER} path does not exist. Submodule with strings should be named common and contain strings folder."
if ! [ -e "${STRINGS_FOLDER}" ]; then
echo "${STRINGS_FOLDER} path does not exist. Submodule with strings should be named common and contain strings folder."
exit 1
fi
#second argument set strings script path
php ${2:-build-scripts/xcode/aux_scripts/import_strings.php} ${PRODUCT_NAME} ${STRINGS_FOLDER}
php ${SCRIPT_DIR}/../aux_scripts/import_strings.php ${LOCALIZATION_PATH} ${STRINGS_FOLDER} ${BUNDLE}