mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-14 12:30:49 +08:00
add lang: ja
This commit is contained in:
@@ -5,6 +5,7 @@ use App\Http\Middleware\Locale;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use Nexus\Plugin\Hook;
|
||||
use Nexus\Translation\NexusTranslator;
|
||||
|
||||
final class Nexus
|
||||
{
|
||||
@@ -30,6 +31,8 @@ final class Nexus
|
||||
|
||||
private static array $translations = [];
|
||||
|
||||
private static ?NexusTranslator $translator = null;
|
||||
|
||||
const PLATFORM_USER = 'user';
|
||||
const PLATFORM_ADMIN = 'admin';
|
||||
const PLATFORM_TRACKER = 'tracker';
|
||||
@@ -280,29 +283,38 @@ final class Nexus
|
||||
return self::$appendFooters;
|
||||
}
|
||||
|
||||
public static function addTranslationNamespace($path, $namespace)
|
||||
public static function addTranslationNamespace($path, $namespace): void
|
||||
{
|
||||
if (empty($namespace)) {
|
||||
throw new \InvalidArgumentException("namespace can not be empty");
|
||||
}
|
||||
self::$translationNamespaces[$namespace] = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
||||
if (IN_NEXUS) {
|
||||
//只有 Nexus 下需要,Laravel 下是通过 configurePackage 中 hasTranslations() 加载的
|
||||
self::getTranslator()->addNamespace($namespace, $path);
|
||||
}
|
||||
}
|
||||
|
||||
public static function trans($key, $replace = [], $locale = null)
|
||||
{
|
||||
if (!IN_NEXUS) {
|
||||
return trans($key, $replace, $locale ?? get_langfolder_cookie(true));
|
||||
if (is_null($locale)) {
|
||||
$locale = get_langfolder_cookie(true);
|
||||
}
|
||||
if (empty(self::$translations)) {
|
||||
//load from default lang dir
|
||||
$langDir = ROOT_PATH . 'resources/lang/';
|
||||
self::loadTranslations($langDir);
|
||||
//load from namespace
|
||||
foreach (self::$translationNamespaces as $namespace => $path) {
|
||||
self::loadTranslations($path, $namespace);
|
||||
}
|
||||
if (IN_NEXUS) {
|
||||
return self::getTranslator()->trans($key, $replace, $locale);
|
||||
} else {
|
||||
return trans($key, $replace, $locale);
|
||||
}
|
||||
return self::getTranslation($key, $replace, $locale ?? get_langfolder_cookie(true));
|
||||
// if (empty(self::$translations)) {
|
||||
// //load from default lang dir
|
||||
// $langDir = ROOT_PATH . 'resources/lang/';
|
||||
// self::loadTranslations($langDir);
|
||||
// //load from namespace
|
||||
// foreach (self::$translationNamespaces as $namespace => $path) {
|
||||
// self::loadTranslations($path, $namespace);
|
||||
// }
|
||||
// }
|
||||
// return self::getTranslation($key, $replace, $locale ?? get_langfolder_cookie(true));
|
||||
}
|
||||
|
||||
private static function loadTranslations($path, $namespace = null)
|
||||
@@ -364,5 +376,13 @@ final class Nexus
|
||||
return $getKey;
|
||||
}
|
||||
|
||||
private static function getTranslator(): NexusTranslator
|
||||
{
|
||||
if (is_null(self::$translator)) {
|
||||
self::$translator = new NexusTranslator(Locale::getDefault());
|
||||
}
|
||||
return self::$translator;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
49
nexus/Translation/NexusTranslator.php
Normal file
49
nexus/Translation/NexusTranslator.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Nexus\Translation;
|
||||
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use Illuminate\Translation\FileLoader;
|
||||
use Illuminate\Translation\Translator;
|
||||
|
||||
class NexusTranslator
|
||||
{
|
||||
protected Filesystem $filesystem;
|
||||
protected FileLoader $loader;
|
||||
protected Translator $translator;
|
||||
|
||||
public function __construct(string $defaultLocale = 'en', string $fallbackLocale = 'en', string $defaultPath = null)
|
||||
{
|
||||
$this->filesystem = new Filesystem();
|
||||
$this->loader = new FileLoader($this->filesystem, $defaultPath ?? ROOT_PATH . 'resources/lang');
|
||||
|
||||
// Laravel-style fallback
|
||||
$this->translator = new Translator($this->loader, $defaultLocale);
|
||||
$this->translator->setFallback($fallbackLocale);
|
||||
}
|
||||
|
||||
public function setLocale(string $locale): void
|
||||
{
|
||||
$this->translator->setLocale($locale);
|
||||
}
|
||||
|
||||
public function getLocale(): string
|
||||
{
|
||||
return $this->translator->getLocale();
|
||||
}
|
||||
|
||||
public function addNamespace(string $namespace, string $path): void
|
||||
{
|
||||
$this->loader->addNamespace($namespace, $path);
|
||||
}
|
||||
|
||||
public function trans(string $key, array $replace = [], string $locale = null): string
|
||||
{
|
||||
return $this->translator->get($key, $replace, $locale);
|
||||
}
|
||||
|
||||
public function has(string $key, string $locale = null): bool
|
||||
{
|
||||
return $this->translator->has($key, $locale);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user