更新 'installer/Migration/V5_4_0/Migration.php'

This commit is contained in:
zhongjin 2023-05-06 09:58:46 +08:00
parent 1913497473
commit 025dcd59d4

View File

@ -25,6 +25,132 @@ use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use OrangeHRM\Installer\Util\V1\AbstractMigration;
class TranslationHelper
{
protected ?LangStringHelper $langStringHelper = null;
private Connection $connection;
/**
* @param Connection $connection
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
/**
* @return Connection
*/
protected function getConnection(): Connection
{
return $this->connection;
}
/**
* @param string $language
* @return void
*/
public function addTranslations(string $language): void
{
$filepath = __DIR__ . '/translation/' . $language . '.yaml';
$yml = Yaml::parseFile($filepath);
$translations = array_shift($yml);
foreach ($translations as $translation) {
$sourceObj = new TransUnit($translation['target'], $translation['unitId']);
$this->saveTranslationRecord($translation['group'], $sourceObj, $language);
}
}
/**
* @param string $groupName
* @param TransUnit $source
* @param string $language
* @return void
*/
private function saveTranslationRecord(string $groupName, TransUnit $source, string $language): void
{
$groupId = $this->getLangStringHelper()->getGroupId($groupName);
$langStringId = $this->getLangStringHelper()->getLangStringIdByUnitIdAndGroup($source->getUnitId(), $groupId);
if ($langStringId == null) {
throw new Exception(
'Cannot add a translation to a non existent lang string: ' . $source->getUnitId()
);
}
$langId = $this->getLanguageId($language);
$existTranslation = $this->getTranslationRecord($langStringId, $langId);
if ($existTranslation != null) {
// TODO hanldle customized translations
} else {
$insetQuery = $this->createQueryBuilder();
$insetQuery->insert('ohrm_i18n_translate')
->values(
[
'lang_string_id' => ':langStringId',
'language_id' => ':langId',
'value' => ':target',
]
)
->setParameter('langStringId', $langStringId)
->setParameter('langId', $langId)
->setParameter('target', $source->getTarget())
->executeQuery();
}
}
/**
* @return LangStringHelper
*/
private function getLangStringHelper(): LangStringHelper
{
if (is_null($this->langStringHelper)) {
$this->langStringHelper = new LangStringHelper($this->getConnection());
}
return $this->langStringHelper;
}
/**
* @param string $langCode
* @return int
* @throws Exception
*/
private function getLanguageId(string $langCode): int
{
$searchQuery = $this->createQueryBuilder();
$searchQuery->select('language.id')
->from('ohrm_i18n_language', 'language')
->where('language.code = :langCode')
->setParameter('langCode', $langCode);
return $searchQuery->executeQuery()->fetchOne();
}
/**
* @return QueryBuilder
*/
protected function createQueryBuilder(): QueryBuilder
{
return $this->getConnection()->createQueryBuilder();
}
/**
* @param array $langStringId
* @param int $langId
* @return string
* @throws Exception
*/
private function getTranslationRecord(int $langStringId, int $langId): string
{
$searchQuery = $this->createQueryBuilder();
$searchQuery->select('translate.id')
->from('ohrm_i18n_translate', 'translate')
->where('translate.language_id = :langCode')
->andWhere('translate.lang_string_id = :langStringId')
->setParameter('langCode', $langId)
->setParameter('langStringId', $langStringId);
return $searchQuery->executeQuery()->fetchOne();
}
}
class Migration extends AbstractMigration
{
protected ?LangStringHelper $langStringHelper = null;