mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-14 20:40:49 +08:00
add lang: ja
This commit is contained in:
@@ -55,8 +55,7 @@ class Test extends Command
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$rep = new ExamRepository();
|
||||
$result = $rep->getUserExamProgress(10041, ExamUser::STATUS_NORMAL);
|
||||
$result = dirname(__FILE__);
|
||||
dd($result);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ use Stichoza\GoogleTranslate\GoogleTranslate;
|
||||
|
||||
class TranslateLang extends Command
|
||||
{
|
||||
protected $signature = 'lang:translate {source} {target}
|
||||
protected $signature = 'lang:translate {runEnv} {source} {target}
|
||||
{filename? : Optional file to translate (php file or json)}
|
||||
{--dry-run : Only print translations without writing files}
|
||||
{--ignore= : Comma-separated keys to ignore (e.g. key1,key2)}
|
||||
@@ -20,36 +20,61 @@ class TranslateLang extends Command
|
||||
protected $ignoreKeys = [];
|
||||
protected $cache = [];
|
||||
protected $cachePath;
|
||||
protected $langPath;
|
||||
|
||||
protected $runEnv;
|
||||
|
||||
const RUN_ENV_NEXUS = 'nexus';
|
||||
const RUN_ENV_LARAVEL = 'laravel';
|
||||
|
||||
private static array $runEnvToLangPathMaps = [
|
||||
self::RUN_ENV_NEXUS => ROOT_PATH . "lang",
|
||||
self::RUN_ENV_LARAVEL => ROOT_PATH . "resources/lang",
|
||||
];
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$source = $this->argument('source');
|
||||
$target = $this->argument('target');
|
||||
$filename = $this->argument('filename');
|
||||
$runEnv = $this->runEnv = $this->argument('runEnv');
|
||||
$this->ignoreKeys = array_filter(explode(',', $this->option('ignore')));
|
||||
$this->cachePath = storage_path("framework/lang-translate-cache.{$source}.{$target}.json");
|
||||
|
||||
$this->loadCache();
|
||||
|
||||
$this->tr = new GoogleTranslate();
|
||||
$supportLanguages = $this->tr->languages();
|
||||
if (!in_array($source, $supportLanguages)) {
|
||||
$this->error("source $source is not supported by Google Translate");
|
||||
return 1;
|
||||
}
|
||||
if (!in_array($target, $supportLanguages)) {
|
||||
$this->error("target $target is not supported by Google Translate");
|
||||
return 1;
|
||||
}
|
||||
$this->tr->setSource($source);
|
||||
$this->tr->setTarget($target);
|
||||
|
||||
$langPath = resource_path('lang');
|
||||
$langPath = $this->langPath = $this->getLangPath($runEnv);
|
||||
|
||||
//谷歌使用的是 -, 本地使用 _
|
||||
//谷歌使用的是 - , 本地按照 ISO 15897 标准使用 _
|
||||
$source = str_replace("-", "_", $source);
|
||||
$target = str_replace("-", "_", $target);
|
||||
|
||||
$dir = "{$langPath}/{$source}";
|
||||
$realSourceDir = $source;
|
||||
if ($runEnv == self::RUN_ENV_NEXUS) {
|
||||
$realSourceDir = $this->langToDirName($source);
|
||||
}
|
||||
$sourceDir = "{$langPath}/" . $realSourceDir;
|
||||
if ($filename) {
|
||||
// 👇 指定具体文件翻译
|
||||
$this->translateSpecificFile($filename, $source, $target);
|
||||
$this->translateSpecificFile($filename, $realSourceDir, $target);
|
||||
} else {
|
||||
// 👇 未指定时,用户确认是否翻译所有文件
|
||||
$answer = $this->ask("你没有指定文件名,是否翻译目录 $dir 下所有语言文件?请输入 yes 确认");
|
||||
$answer = $this->ask("你没有指定文件名,是否翻译目录 $sourceDir 下所有语言文件到 $target ?请输入 yes 确认");
|
||||
if (strtolower($answer) === 'yes') {
|
||||
foreach (File::files("{$langPath}/{$source}") as $file) {
|
||||
foreach (File::files($sourceDir) as $file) {
|
||||
if ($file->getExtension() === 'php') {
|
||||
$this->translatePhpFile($file->getPathname(), $source, $target);
|
||||
}
|
||||
@@ -75,9 +100,15 @@ class TranslateLang extends Command
|
||||
protected function translatePhpFile($sourceFile, $sourceLang, $targetLang)
|
||||
{
|
||||
$relativePath = basename($sourceFile);
|
||||
$targetFile = resource_path("lang/{$targetLang}/{$relativePath}");
|
||||
|
||||
$data = require $sourceFile;
|
||||
$targetFile = $this->langPath . "/{$targetLang}/{$relativePath}";
|
||||
if ($this->runEnv == self::RUN_ENV_LARAVEL) {
|
||||
$data = require $sourceFile;
|
||||
} else {
|
||||
require $sourceFile;
|
||||
$var = str_replace([".php", "lang_", "-"], "", $relativePath);
|
||||
$var = "lang_$var";
|
||||
$data = $$var;
|
||||
}
|
||||
$translated = $this->translateArray($data);
|
||||
|
||||
$export = var_export($translated, true);
|
||||
@@ -87,14 +118,20 @@ class TranslateLang extends Command
|
||||
if (!file_exists(dirname($targetFile))) {
|
||||
mkdir(dirname($targetFile), 0755, true);
|
||||
}
|
||||
file_put_contents($targetFile, "<?php\n\nreturn $export;\n");
|
||||
if ($this->runEnv == self::RUN_ENV_LARAVEL) {
|
||||
$contents = "<?php\n\nreturn $export;\n";
|
||||
} else {
|
||||
$contents = sprintf("<?php\n\n$%s = %s;\n", $var, $export);
|
||||
}
|
||||
file_put_contents($targetFile, $contents);
|
||||
$this->info("✅ Wrote translated file: $targetFile");
|
||||
}
|
||||
}
|
||||
|
||||
protected function translateJsonFile($jsonFile, $sourceLang, $targetLang)
|
||||
{
|
||||
$targetFile = resource_path("lang/{$targetLang}.json");
|
||||
// $targetFile = resource_path("lang/{$targetLang}.json");
|
||||
$targetFile = $this->langPath . "/{$targetLang}/{$jsonFile}";
|
||||
$content = json_decode(file_get_contents($jsonFile), true);
|
||||
$translated = [];
|
||||
|
||||
@@ -155,7 +192,7 @@ class TranslateLang extends Command
|
||||
|
||||
protected function translateSpecificFile($filename, $source, $target)
|
||||
{
|
||||
$langPath = resource_path("lang");
|
||||
$langPath = $this->langPath;
|
||||
|
||||
if (str_ends_with($filename, '.json')) {
|
||||
$jsonPath = "{$langPath}/{$filename}";
|
||||
@@ -165,7 +202,8 @@ class TranslateLang extends Command
|
||||
if (file_exists($jsonPath)) {
|
||||
$this->translateJsonFile($jsonPath, $source, $target);
|
||||
} else {
|
||||
$this->error("❌ JSON 文件未找到:$filename");
|
||||
$this->error("❌ JSON 文件未找到:$jsonPath");
|
||||
exit();
|
||||
}
|
||||
} else {
|
||||
$phpPath = "{$langPath}/{$source}/$filename";
|
||||
@@ -175,7 +213,8 @@ class TranslateLang extends Command
|
||||
if (file_exists($phpPath)) {
|
||||
$this->translatePhpFile($phpPath, $source, $target);
|
||||
} else {
|
||||
$this->error("❌ PHP 语言文件未找到:$filename");
|
||||
$this->error("❌ PHP 语言文件未找到:$phpPath");
|
||||
exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -211,4 +250,22 @@ class TranslateLang extends Command
|
||||
return $formatted;
|
||||
}
|
||||
|
||||
private function langToDirName($lang): string
|
||||
{
|
||||
$map = [
|
||||
'zh_CN' => 'chs',
|
||||
'zh_TW' => 'cht',
|
||||
];
|
||||
return $map[$lang] ?? $lang;
|
||||
}
|
||||
|
||||
private function getLangPath($runEnv): string
|
||||
{
|
||||
if (isset(self::$runEnvToLangPathMaps[$runEnv])) {
|
||||
return self::$runEnvToLangPathMaps[$runEnv];
|
||||
} else {
|
||||
throw new \InvalidArgumentException("invalid runEnv: $runEnv, allowed values: " . implode(', ', array_keys(self::$runEnvToLangPathMaps)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -56,13 +56,13 @@ class Locale
|
||||
$log = "Cookie::get(): $lang";
|
||||
}
|
||||
do_log($log);
|
||||
return self::$languageMaps[$lang] ?? null;
|
||||
return self::$languageMaps[$lang] ?? $lang;
|
||||
}
|
||||
|
||||
public static function getDefault()
|
||||
{
|
||||
$defaultLang = get_setting("main.defaultlang");
|
||||
return self::$languageMaps[$defaultLang] ?? null;
|
||||
return self::$languageMaps[$defaultLang] ?? $defaultLang;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -296,7 +296,7 @@ class User extends Authenticatable implements FilamentUser, HasName
|
||||
}
|
||||
if (!$locale) {
|
||||
$lang = $this->language->site_lang_folder;
|
||||
$locale = Locale::$languageMaps[$lang] ?? 'en';
|
||||
$locale = Locale::$languageMaps[$lang] ?? $lang;
|
||||
$log .= ", [NO_DATA_FROM_COOKIE], lang from database: $lang, locale: $locale";
|
||||
}
|
||||
do_log($log);
|
||||
|
||||
Reference in New Issue
Block a user