Implement custom translation layer

This commit is contained in:
shalvah
2023-05-27 22:59:39 +02:00
parent f8c413cfd8
commit 392b1094d7
10 changed files with 94 additions and 59 deletions

View File

@@ -11,6 +11,7 @@ use Knuckles\Scribe\Matching\RouteMatcher;
use Knuckles\Scribe\Matching\RouteMatcherInterface;
use Knuckles\Scribe\Tools\BladeMarkdownEngine;
use Knuckles\Scribe\Tools\Utils;
use Knuckles\Scribe\Writing\CustomTranslationsLoader;
class ScribeServiceProvider extends ServiceProvider
{
@@ -24,10 +25,7 @@ class ScribeServiceProvider extends ServiceProvider
$this->registerCommands();
$this->loadJsonTranslationsFrom(__DIR__.'/../lang');
$this->publishes([
__DIR__.'/../lang' => $this->app->langPath('vendor/scribe'),
], 'scribe-translations');
$this->configureTranslations();
// Bind the route matcher implementation
$this->app->bind(RouteMatcherInterface::class, config('scribe.routeMatcher', RouteMatcher::class));
@@ -52,6 +50,22 @@ class ScribeServiceProvider extends ServiceProvider
}
}
protected function configureTranslations(): void
{
$this->publishes([
__DIR__.'/../lang/' => $this->app->langPath(),
], 'scribe-translations');
if ($this->app->runningInConsole()) {
$this->loadTranslationsFrom($this->app->langPath('scribe.php'), 'scribe');
$this->loadTranslationsFrom(realpath(__DIR__.'/../lang'), 'scribe');
$this->app->extend('translation.loader', function ($defaultFileLoader) {
return new CustomTranslationsLoader($defaultFileLoader);
});
}
}
protected function registerViews(): void
{
// Register custom Markdown Blade compiler so we can automatically have MD views converted to HTML

View File

@@ -363,12 +363,8 @@ class Utils
{
$translation = trans($key, $replace);
if ($translation === $key) {
$translation = trans($key, $replace, config('app.fallback_locale'));
}
if ($translation === $key) {
return trans($key, $replace, 'en');
if ($translation === $key || $translation === null) {
$translation = trans($key, $replace, 'en');
}
return $translation;

View File

@@ -0,0 +1,59 @@
<?php
namespace Knuckles\Scribe\Writing;
use Illuminate\Translation\FileLoader;
use Knuckles\Scribe\Tools\Globals;
class CustomTranslationsLoader extends FileLoader
{
protected FileLoader $defaultLoader;
protected mixed $path;
protected ?array $scribeTranslationsCache = null;
protected ?array $userTranslationsCache = null;
public function __construct(FileLoader $loader)
{
$this->defaultLoader = $loader;
$this->files = app('files');
$this->path = app('path.lang');
}
public function load($locale, $group, $namespace = null)
{
// Laravel expects translation strings to be broken up into groups (files):
// `lang/scribe/en/auth.php`, `lang/scribe/en/links.php`
// We want to trick it into accepting a simple `lang/scribe.php`.
if ($namespace == 'scribe') {
if (isset($this->scribeTranslationsCache)) {
$lines = $this->scribeTranslationsCache[$group] ?? [];
} elseif ($this->files->exists($full = "{$this->hints[$namespace]}/scribe.php")) {
$this->scribeTranslationsCache = $this->files->getRequire($full);
$lines = $this->scribeTranslationsCache[$group] ?? [];
} else {
return [];
}
return $this->loadScribeNamespaceOverrides($lines, $locale, $group, $namespace);
}
return $this->defaultLoader->load($locale, $group, $namespace);
}
protected function loadScribeNamespaceOverrides(array $lines, $locale, $group, $namespace)
{
$userTranslationsFile = "{$this->path}/scribe.php";
if ($this->files->exists($userTranslationsFile)) {
if (!isset($this->userTranslationsCache)) {
$this->userTranslationsCache = $this->files->getRequire($userTranslationsFile);
}
$userTranslations = $this->userTranslationsCache[$group] ?? [];
return array_replace_recursive($lines, $userTranslations);
}
return $lines;
}
}