First Commit
This commit is contained in:
Executable
+166
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* SysCMS - Content Management System
|
||||
*
|
||||
* Copyright (c) 2025 Marco Costa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Author: Marco Costa
|
||||
* Email: contato@syspanel.com.br
|
||||
* Project: SysCMS
|
||||
*
|
||||
* This software is licensed, not sold.
|
||||
* Permission is granted to use this software
|
||||
* subject to the terms of the SysCMS Commercial License.
|
||||
*
|
||||
* Redistribution, sublicensing, resale, or
|
||||
* disclosure of this software, in whole or in part,
|
||||
* is strictly prohibited without prior written permission.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
|
||||
*/
|
||||
|
||||
namespace Kernel;
|
||||
|
||||
class Translator
|
||||
{
|
||||
protected static array $translations = [];
|
||||
protected static array $paths = [];
|
||||
protected static array $themePaths = [];
|
||||
protected static string $locale = 'pt_BR';
|
||||
|
||||
/**
|
||||
* Add a namespace for translations
|
||||
*/
|
||||
public static function addNamespace(string $namespace, string $path): void
|
||||
{
|
||||
self::$paths[$namespace] = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a theme path for translation overrides
|
||||
*/
|
||||
public static function addThemePath(string $path): void
|
||||
{
|
||||
self::$themePaths[] = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current locale
|
||||
*/
|
||||
public static function setLocale(string $locale): void
|
||||
{
|
||||
self::$locale = $locale;
|
||||
self::loadTranslations();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current locale
|
||||
*/
|
||||
public static function getLocale(): string
|
||||
{
|
||||
return self::$locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate a key
|
||||
*/
|
||||
public static function translate(string $key, array $params = []): string
|
||||
{
|
||||
if (!isset(self::$translations[$key])) {
|
||||
return $key;
|
||||
}
|
||||
|
||||
$translation = self::$translations[$key];
|
||||
|
||||
// Se não foi passado platformName explicitamente, tenta pegar do PlatformService
|
||||
if (!isset($params['platformName'])) {
|
||||
try {
|
||||
$platformService = new \App\Services\PlatformService();
|
||||
$params['platformName'] = $platformService->getPlatformName();
|
||||
} catch (\Exception $e) {
|
||||
$params['platformName'] = 'SysCMS';
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($params)) {
|
||||
foreach ($params as $param => $value) {
|
||||
if ($value !== null) {
|
||||
$translation = str_replace(":{$param}", (string)$value, $translation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $translation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load translations from files
|
||||
*/
|
||||
protected static function loadTranslations(): void
|
||||
{
|
||||
self::$translations = [];
|
||||
|
||||
// Load standard paths
|
||||
foreach (self::$paths as $namespace => $path) {
|
||||
self::loadFromPath($path);
|
||||
}
|
||||
|
||||
// Load theme overrides (highest priority)
|
||||
foreach (self::$themePaths as $path) {
|
||||
self::loadFromPath($path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load translations from a specific path
|
||||
*/
|
||||
protected static function loadFromPath(string $path): void
|
||||
{
|
||||
$localePath = $path . '/' . self::$locale;
|
||||
|
||||
if (is_dir($localePath)) {
|
||||
$files = glob($localePath . '/*.json');
|
||||
|
||||
foreach ($files as $file) {
|
||||
$filename = basename($file, '.json');
|
||||
$content = file_get_contents($file);
|
||||
if ($content === false) {
|
||||
continue;
|
||||
}
|
||||
$translations = json_decode($content, true);
|
||||
|
||||
if (is_array($translations)) {
|
||||
// Flatten the translations to support dot notation
|
||||
$flatTranslations = self::flatten($translations);
|
||||
foreach ($flatTranslations as $key => $value) {
|
||||
self::$translations[$filename . '.' . $key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten a multi-dimensional associative array with dot notation.
|
||||
*
|
||||
* @param array $array
|
||||
* @param string $prepend
|
||||
* @return array
|
||||
*/
|
||||
protected static function flatten(array $array, string $prepend = ''): array
|
||||
{
|
||||
$results = [];
|
||||
|
||||
foreach ($array as $key => $value) {
|
||||
if (is_array($value) && !empty($value)) {
|
||||
$results = array_merge($results, self::flatten($value, $prepend . $key . '.'));
|
||||
} else {
|
||||
$results[$prepend . $key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user