2018-04-16 19:11:32 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Grocy\Services;
|
|
|
|
|
2019-05-01 20:04:06 +02:00
|
|
|
use Gettext\Translations;
|
|
|
|
use Gettext\Translator;
|
|
|
|
|
2018-07-14 22:49:42 +02:00
|
|
|
class LocalizationService
|
2018-04-16 19:11:32 +02:00
|
|
|
{
|
|
|
|
public function __construct(string $culture)
|
|
|
|
{
|
2019-05-01 20:04:06 +02:00
|
|
|
$this->Culture = $culture;
|
|
|
|
$this->LoadLocalizations($culture);
|
2018-04-16 19:11:32 +02:00
|
|
|
}
|
|
|
|
|
2019-05-01 20:04:06 +02:00
|
|
|
protected $PotTranslation;
|
|
|
|
protected $PoTranslation;
|
|
|
|
public $Translator;
|
2018-04-16 19:11:32 +02:00
|
|
|
|
2019-05-01 20:04:06 +02:00
|
|
|
private function LoadLocalizations()
|
2018-04-16 19:11:32 +02:00
|
|
|
{
|
2019-05-01 20:04:06 +02:00
|
|
|
$culture = $this->Culture;
|
|
|
|
|
|
|
|
$this->PotTranslation = Translations::fromPoFile(__DIR__ . '/../localization/chore_types.pot');
|
|
|
|
$this->PotTranslation = $this->PotTranslation->mergeWith(Translations::fromPoFile(__DIR__ . '/../localization/component_translations.pot'));
|
|
|
|
$this->PotTranslation = $this->PotTranslation->mergeWith(Translations::fromPoFile(__DIR__ . '/../localization/demo_data.pot'));
|
|
|
|
$this->PotTranslation = $this->PotTranslation->mergeWith(Translations::fromPoFile(__DIR__ . '/../localization/stock_transaction_types.pot'));
|
|
|
|
$this->PotTranslation = $this->PotTranslation->mergeWith(Translations::fromPoFile(__DIR__ . '/../localization/strings.pot'));
|
|
|
|
$this->PotTranslation = $this->PotTranslation->mergeWith(Translations::fromPoFile(__DIR__ . '/../localization/userfield_types.pot'));
|
|
|
|
|
|
|
|
$this->PoTranslation = Translations::fromPoFile(__DIR__ . "/../localization/$culture/chore_types.po");
|
|
|
|
$this->PoTranslation = $this->PoTranslation->mergeWith(Translations::fromPoFile(__DIR__ . "/../localization/$culture/component_translations.po"));
|
|
|
|
$this->PoTranslation = $this->PoTranslation->mergeWith(Translations::fromPoFile(__DIR__ . "/../localization/$culture/demo_data.po"));
|
|
|
|
$this->PoTranslation = $this->PoTranslation->mergeWith(Translations::fromPoFile(__DIR__ . "/../localization/$culture/stock_transaction_types.po"));
|
|
|
|
$this->PoTranslation = $this->PoTranslation->mergeWith(Translations::fromPoFile(__DIR__ . "/../localization/$culture/strings.po"));
|
|
|
|
$this->PoTranslation = $this->PoTranslation->mergeWith(Translations::fromPoFile(__DIR__ . "/../localization/$culture/userfield_types.po"));
|
|
|
|
|
|
|
|
$this->Translator = new Translator();
|
|
|
|
$this->Translator->loadTranslations($this->PoTranslation);
|
|
|
|
$this->Translator->register();
|
2018-04-16 19:11:32 +02:00
|
|
|
}
|
|
|
|
|
2019-05-01 20:04:06 +02:00
|
|
|
public function GetTranslationsForJavaScriptTranslator()
|
2018-11-24 20:39:17 +01:00
|
|
|
{
|
2019-05-01 20:04:06 +02:00
|
|
|
return $this->PoTranslation->toJsonString();
|
2018-11-24 20:39:17 +01:00
|
|
|
}
|
|
|
|
|
2019-05-01 20:19:18 +02:00
|
|
|
//TODO: Missing translations should be automatically added to the source POT file
|
|
|
|
|
2019-05-01 20:04:06 +02:00
|
|
|
public function __t(string $text, ...$placeholderValues)
|
2018-04-16 19:11:32 +02:00
|
|
|
{
|
2019-05-01 20:04:06 +02:00
|
|
|
return __($text, ...$placeholderValues);
|
2018-04-16 19:11:32 +02:00
|
|
|
}
|
|
|
|
|
2019-05-01 20:04:06 +02:00
|
|
|
public function __n($number, $singularForm, $pluralForm)
|
2018-04-16 19:11:32 +02:00
|
|
|
{
|
2019-05-01 20:04:06 +02:00
|
|
|
return $this->Translator->ngettext($singularForm, $pluralForm, $number);
|
2018-04-16 19:11:32 +02:00
|
|
|
}
|
|
|
|
}
|