mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 09:56:06 +00:00
Added KnownTranslationFactory and use it in as many places as possible
this makes translation usage much more statically analysable. The only places this isn't used are: - places that prefix translations with colours (those are still a problem) - places where server/client translations don't match (e.g. gameMode.changed accepts different parameters in vanilla than in PM)
This commit is contained in:
178
build/generate-known-translation-apis.php
Normal file
178
build/generate-known-translation-apis.php
Normal file
@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\build\generate_known_translation_apis;
|
||||
|
||||
use Webmozart\PathUtil\Path;
|
||||
use function array_map;
|
||||
use function count;
|
||||
use function dirname;
|
||||
use function file_put_contents;
|
||||
use function fwrite;
|
||||
use function implode;
|
||||
use function is_numeric;
|
||||
use function ob_get_clean;
|
||||
use function ob_start;
|
||||
use function parse_ini_file;
|
||||
use function str_replace;
|
||||
use function strtoupper;
|
||||
use const INI_SCANNER_RAW;
|
||||
use const SORT_STRING;
|
||||
use const STDERR;
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
function constantify(string $permissionName) : string{
|
||||
return strtoupper(str_replace([".", "-"], "_", $permissionName));
|
||||
}
|
||||
|
||||
function functionify(string $permissionName) : string{
|
||||
return str_replace([".", "-"], "_", $permissionName);
|
||||
}
|
||||
|
||||
const SHARED_HEADER = <<<'HEADER'
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\lang;
|
||||
|
||||
|
||||
HEADER;
|
||||
|
||||
/**
|
||||
* @param string[] $languageDefinitions
|
||||
* @phpstan-param array<string, string> $languageDefinitions
|
||||
*/
|
||||
function generate_known_translation_keys(array $languageDefinitions) : void{
|
||||
ob_start();
|
||||
|
||||
echo SHARED_HEADER;
|
||||
echo <<<'HEADER'
|
||||
/**
|
||||
* This class contains constants for all the translations known to PocketMine-MP as per the used version of pmmp/Language.
|
||||
* This class is generated automatically, do NOT modify it by hand.
|
||||
*/
|
||||
final class KnownTranslationKeys{
|
||||
|
||||
HEADER;
|
||||
|
||||
ksort($languageDefinitions, SORT_STRING);
|
||||
foreach($languageDefinitions as $k => $_){
|
||||
echo "\tpublic const ";
|
||||
echo constantify($k);
|
||||
echo " = \"" . $k . "\";\n";
|
||||
}
|
||||
|
||||
echo "}\n";
|
||||
|
||||
file_put_contents(dirname(__DIR__) . '/src/lang/KnownTranslationKeys.php', ob_get_clean());
|
||||
|
||||
echo "Done generating KnownTranslationKeys.\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $languageDefinitions
|
||||
* @phpstan-param array<string, string> $languageDefinitions
|
||||
*/
|
||||
function generate_known_translation_factory(array $languageDefinitions) : void{
|
||||
ob_start();
|
||||
|
||||
echo SHARED_HEADER;
|
||||
echo <<<'HEADER'
|
||||
/**
|
||||
* This class contains factory methods for all the translations known to PocketMine-MP as per the used version of
|
||||
* pmmp/Language.
|
||||
* This class is generated automatically, do NOT modify it by hand.
|
||||
*/
|
||||
final class KnownTranslationFactory{
|
||||
|
||||
HEADER;
|
||||
ksort($languageDefinitions, SORT_STRING);
|
||||
|
||||
$parameterRegex = '/{%(.+?)}/';
|
||||
foreach($languageDefinitions as $key => $value){
|
||||
$parameters = [];
|
||||
if(preg_match_all($parameterRegex, $value, $matches) > 0){
|
||||
foreach($matches[1] as $parameterName){
|
||||
if(is_numeric($parameterName)){
|
||||
$parameters[$parameterName] = "param$parameterName";
|
||||
}else{
|
||||
$parameters[$parameterName] = $parameterName;
|
||||
}
|
||||
}
|
||||
}
|
||||
echo "\tpublic static function " .
|
||||
functionify($key) .
|
||||
"(" . implode(", ", array_map(fn(string $paramName) => "string \$$paramName", $parameters)) . ") : TranslationContainer{\n";
|
||||
echo "\t\treturn new TranslationContainer(KnownTranslationKeys::" . constantify($key) . ", [";
|
||||
foreach($parameters as $parameterKey => $parameterName){
|
||||
echo "\n\t\t\t";
|
||||
if(!is_numeric($parameterKey)){
|
||||
echo "\"$parameterKey\"";
|
||||
}else{
|
||||
echo $parameterKey;
|
||||
}
|
||||
echo " => \$$parameterName,";
|
||||
}
|
||||
if(count($parameters) !== 0){
|
||||
echo "\n\t\t";
|
||||
}
|
||||
echo "]);\n\t}\n\n";
|
||||
}
|
||||
|
||||
echo "}\n";
|
||||
|
||||
file_put_contents(dirname(__DIR__) . '/src/lang/KnownTranslationFactory.php', ob_get_clean());
|
||||
|
||||
echo "Done generating KnownTranslationFactory.\n";
|
||||
}
|
||||
|
||||
|
||||
$lang = parse_ini_file(Path::join(\pocketmine\RESOURCE_PATH, "locale", "eng.ini"), false, INI_SCANNER_RAW);
|
||||
if($lang === false){
|
||||
fwrite(STDERR, "Missing language files!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
generate_known_translation_keys($lang);
|
||||
generate_known_translation_factory($lang);
|
@ -1,93 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\build\generate_known_translation_keys;
|
||||
|
||||
use Webmozart\PathUtil\Path;
|
||||
use function dirname;
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
function constantify(string $permissionName) : string{
|
||||
return strtoupper(str_replace([".", "-"], "_", $permissionName));
|
||||
}
|
||||
ob_start();
|
||||
|
||||
echo <<<'HEADER'
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\lang;
|
||||
|
||||
/**
|
||||
* This class contains constants for all the translations known to PocketMine-MP as per the used version of pmmp/Language.
|
||||
* This class is generated automatically, do NOT modify it by hand.
|
||||
*/
|
||||
final class KnownTranslationKeys{
|
||||
|
||||
HEADER;
|
||||
|
||||
$perms = [];
|
||||
|
||||
$lang = parse_ini_file(Path::join(\pocketmine\RESOURCE_PATH, "locale", "eng.ini"), false, INI_SCANNER_RAW);
|
||||
if($lang === false){
|
||||
fwrite(STDERR, "Missing language files!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
foreach($lang as $k => $v){
|
||||
$perms[] = $k;
|
||||
}
|
||||
|
||||
sort($perms, SORT_STRING);
|
||||
foreach($perms as $perm){
|
||||
echo "\tpublic const ";
|
||||
echo constantify($perm);
|
||||
echo " = \"" . $perm . "\";\n";
|
||||
}
|
||||
|
||||
echo "}\n";
|
||||
|
||||
file_put_contents(dirname(__DIR__) . '/src/lang/KnownTranslationKeys.php', ob_get_clean());
|
||||
|
||||
echo "Done.\n";
|
Reference in New Issue
Block a user