Added basic panel translation functions

This commit is contained in:
Pinga 2023-08-10 16:18:01 +03:00
parent 5cc5c72ea4
commit 9608234596
6 changed files with 37 additions and 3 deletions

View file

@ -8,6 +8,8 @@ use Slim\Handlers\Strategies\RequestResponseArgs;
use Slim\Views\Twig; use Slim\Views\Twig;
use Slim\Views\TwigMiddleware; use Slim\Views\TwigMiddleware;
use Twig\TwigFunction; use Twig\TwigFunction;
use Gettext\Loader\PoLoader;
use Gettext\Translations;
if (session_status() == PHP_SESSION_NONE) { if (session_status() == PHP_SESSION_NONE) {
session_start(); session_start();
@ -42,6 +44,22 @@ $routeParser = $app->getRouteCollector()->getRouteParser();
require_once __DIR__ . '/database.php'; require_once __DIR__ . '/database.php';
$desiredLanguage = 'en_US'; // Default language
// Check for a URL parameter
if (isset($_GET['lang'])) {
$desiredLanguage = $_GET['lang'];
}
$languageFile = '../lang/' . $desiredLanguage . '/messages.po';
if (!file_exists($languageFile)) {
$desiredLanguage = 'en_US'; // Fallback
$languageFile = '../lang/en_US/messages.po';
}
$loader = new PoLoader();
$translations = $loader->loadFile($languageFile);
$container->set('router', function () use ($routeParser) { $container->set('router', function () use ($routeParser) {
return $routeParser; return $routeParser;
}); });
@ -62,7 +80,7 @@ $container->set('flash', function() {
return new \Slim\Flash\Messages; return new \Slim\Flash\Messages;
}); });
$container->set('view', function ($container) { $container->set('view', function ($container) use ($translations) {
$view = Twig::create(__DIR__ . '/../resources/views', [ $view = Twig::create(__DIR__ . '/../resources/views', [
'cache' => false, 'cache' => false,
]); ]);
@ -76,6 +94,17 @@ $container->set('view', function ($container) {
} else { } else {
$view->getEnvironment()->addGlobal('screen_mode', 'light'); $view->getEnvironment()->addGlobal('screen_mode', 'light');
} }
$translateFunction = new TwigFunction('__', function ($text) use ($translations) {
// Find the translation
$translation = $translations->find(null, $text);
if ($translation) {
return $translation->getTranslation();
}
// Return the original text if translation not found
return $text;
});
$view->getEnvironment()->addFunction($translateFunction);
//route //route
$route = new TwigFunction('route', function ($name) { $route = new TwigFunction('route', function ($name) {

View file

@ -30,7 +30,8 @@
"imefisto/psr-swoole-native": "^1.1", "imefisto/psr-swoole-native": "^1.1",
"chubbyphp/chubbyphp-static-file": "^1.2", "chubbyphp/chubbyphp-static-file": "^1.2",
"lasserafn/php-initial-avatar-generator": "^4.2", "lasserafn/php-initial-avatar-generator": "^4.2",
"mevdschee/php-crud-api": "^2.14" "mevdschee/php-crud-api": "^2.14",
"gettext/gettext": "^5.7"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View file

@ -0,0 +1,2 @@
msgid "Dashboard"
msgstr "Dashboard"

BIN
cp/lang/uk_UA/messages.mo Normal file

Binary file not shown.

View file

@ -0,0 +1,2 @@
msgid "Dashboard"
msgstr "Панель керування"

View file

@ -14,7 +14,7 @@
Overview Overview
</div> </div>
<h2 class="page-title"> <h2 class="page-title">
Dashboard {{ __('Dashboard') }}
</h2> </h2>
</div> </div>
<!-- Page title actions --> <!-- Page title actions -->