mirror of
https://github.com/grocy/grocy.git
synced 2025-10-14 09:11:11 +00:00
Added localization support
This commit is contained in:
103
services/LocalizationService.php
Normal file
103
services/LocalizationService.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace Grocy\Services;
|
||||
|
||||
class LocalizationService extends BaseService
|
||||
{
|
||||
const DEFAULT_CULTURE = 'en';
|
||||
|
||||
public function __construct(string $culture)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->Culture = $culture;
|
||||
|
||||
$this->StringsDefaultCulture = $this->LoadLocalizationFile(self::DEFAULT_CULTURE);
|
||||
$this->StringsCurrentCulture = $this->LoadLocalizationFile($culture);
|
||||
$this->StringsMerged = array_merge($this->StringsDefaultCulture, $this->StringsCurrentCulture);
|
||||
}
|
||||
|
||||
protected $Culture;
|
||||
protected $StringsDefaultCulture;
|
||||
protected $StringsCurrentCulture;
|
||||
protected $StringsMerged;
|
||||
|
||||
private function LoadLocalizationFile(string $culture)
|
||||
{
|
||||
$file = __DIR__ . "/../localization/$culture.php";
|
||||
|
||||
if (file_exists($file))
|
||||
{
|
||||
return require $file;
|
||||
}
|
||||
else
|
||||
{
|
||||
return array();
|
||||
}
|
||||
}
|
||||
|
||||
private function LogMissingLocalization(string $culture, string $text)
|
||||
{
|
||||
$file = __DIR__ . "/../data/missing_translations_$culture.json";
|
||||
|
||||
$missingTranslations = array();
|
||||
if (file_exists($file))
|
||||
{
|
||||
$missingTranslations = json_decode(file_get_contents($file), true);
|
||||
}
|
||||
|
||||
if (!array_key_exists($text, $missingTranslations))
|
||||
{
|
||||
$missingTranslations[$text] = '#TranslationMissing#';
|
||||
}
|
||||
|
||||
if (count($missingTranslations) > 0)
|
||||
{
|
||||
file_put_contents($file, json_encode($missingTranslations, JSON_PRETTY_PRINT));
|
||||
}
|
||||
}
|
||||
|
||||
public function Localize(string $text, ...$placeholderValues)
|
||||
{
|
||||
if (MODE === 'dev')
|
||||
{
|
||||
if (!array_key_exists($text, $this->StringsDefaultCulture))
|
||||
{
|
||||
$this->LogMissingLocalization(self::DEFAULT_CULTURE, $text);
|
||||
}
|
||||
|
||||
if (!array_key_exists($text, $this->StringsCurrentCulture))
|
||||
{
|
||||
$this->LogMissingLocalization($this->Culture, $text);
|
||||
}
|
||||
}
|
||||
|
||||
$localizedText = $text;
|
||||
if (array_key_exists($text, $this->StringsMerged))
|
||||
{
|
||||
$localizedText = $this->StringsMerged[$text];
|
||||
}
|
||||
|
||||
for ($i = 0; $i < count($placeholderValues); $i++)
|
||||
{
|
||||
$localizedText = str_replace('#' . ($i + 1), $placeholderValues[$i], $localizedText);
|
||||
}
|
||||
|
||||
return $localizedText;
|
||||
}
|
||||
|
||||
public function GetLocalizations()
|
||||
{
|
||||
return $this->StringsMerged;
|
||||
}
|
||||
|
||||
public function GetDefaultCultureLocalizations()
|
||||
{
|
||||
return $this->StringsDefaultCulture;
|
||||
}
|
||||
|
||||
public function GetCurrentCultureLocalizations()
|
||||
{
|
||||
return $this->StringsCurrentCulture;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user