add translations
This commit is contained in:
parent
3a82230e9c
commit
7449cd6e59
3 changed files with 127 additions and 0 deletions
77
app/Providers/TranslationServiceProvider.php
Normal file
77
app/Providers/TranslationServiceProvider.php
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class TranslationServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap the application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
foreach ($this->collectLocalesStrings() as $locale) {
|
||||
// * Suported locales
|
||||
Cache::rememberForever(sprintf('translations.%s', $locale), function () use ($locale) {
|
||||
$translations = [
|
||||
'php' => $this->phpTranslations($locale),
|
||||
'json' => $this->jsonTranslations($locale),
|
||||
];
|
||||
return $translations;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gather provided langs checking from files
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
private function collectLocalesStrings(): Collection
|
||||
{
|
||||
return collect(File::allFiles(resource_path('lang/')))->flatMap(function ($file) {
|
||||
if ($file->getRelativePath()) {
|
||||
return [$file->getRelativePath() => ''];
|
||||
}
|
||||
})->keys();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gather all php translations
|
||||
*
|
||||
* @param string $locale
|
||||
* @return array
|
||||
*/
|
||||
private function phpTranslations(string $locale): array
|
||||
{
|
||||
$path = resource_path("lang/$locale");
|
||||
|
||||
return collect(File::allFiles($path))->flatMap(function ($file) use ($locale) {
|
||||
$translation = $file->getBasename('.php');
|
||||
return [$translation => trans($translation, [], $locale)];
|
||||
})->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gather json translations
|
||||
*
|
||||
* @param string $locale
|
||||
* @return array
|
||||
*/
|
||||
private function jsonTranslations(string $locale): array
|
||||
{
|
||||
$path = resource_path("lang/$locale.json");
|
||||
|
||||
if (is_string($path) && is_readable($path)) {
|
||||
return json_decode(file_get_contents($path), true) ?? [];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
@ -175,6 +175,7 @@ return [
|
|||
App\Providers\AuthServiceProvider::class,
|
||||
App\Providers\EventServiceProvider::class,
|
||||
App\Providers\RouteServiceProvider::class,
|
||||
App\Providers\TranslationServiceProvider::class,
|
||||
App\Providers\ViewServiceProvider::class,
|
||||
],
|
||||
|
||||
|
|
|
|||
49
resources/ts/modules/trans.ts
Normal file
49
resources/ts/modules/trans.ts
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
export default {
|
||||
data() {
|
||||
return {
|
||||
locale: window.__SYSTEM._locale,
|
||||
};
|
||||
},
|
||||
methods: {
|
||||
/**
|
||||
* Translate the given key.
|
||||
*/
|
||||
__(key: string, replace: Record<PropertyKey, string> = {}): string {
|
||||
let translation: string,
|
||||
translationNotFound = true;
|
||||
|
||||
try {
|
||||
translation = key
|
||||
.split(".")
|
||||
.reduce(
|
||||
(t, i) => t[i as any] ?? null,
|
||||
window.__SYSTEM._translations.php
|
||||
) as string;
|
||||
if (translation) {
|
||||
translationNotFound = false;
|
||||
}
|
||||
} catch (e) {
|
||||
translation = key;
|
||||
}
|
||||
|
||||
if (translationNotFound) {
|
||||
translation = (window.__SYSTEM._translations.json[key as any] ??
|
||||
key) as string;
|
||||
}
|
||||
const vars = translation.match(/:[0-9A-Za-z_]+/g);
|
||||
if (vars) {
|
||||
for (const match of vars) {
|
||||
try {
|
||||
translation = translation.replace(
|
||||
new RegExp(match, "g"),
|
||||
replace[match.substring(1)]
|
||||
);
|
||||
} catch (e) {
|
||||
console.error("String replace failed ", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return translation;
|
||||
},
|
||||
},
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue