diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20.php b/src/CoreBundle/Migrations/Schema/V200/Version20.php index 8d717f550fd..dfb7a7b85c3 100644 --- a/src/CoreBundle/Migrations/Schema/V200/Version20.php +++ b/src/CoreBundle/Migrations/Schema/V200/Version20.php @@ -7,6 +7,7 @@ namespace Chamilo\CoreBundle\Migrations\Schema\V200; use Chamilo\CoreBundle\DataFixtures\LanguageFixtures; +use Chamilo\CoreBundle\Entity\Language; use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Schema\Schema; @@ -162,7 +163,7 @@ public function up(Schema $schema): void // Update language to ISO. $this->addSql('UPDATE language SET isocode = "en" WHERE isocode IS NULL'); - $this->addSql('ALTER TABLE language CHANGE isocode isocode VARCHAR(10) NOT NULL'); + $this->addSql('ALTER TABLE language CHANGE isocode isocode VARCHAR('.Language::ISO_MAX_LENGTH.') NOT NULL'); $this->addSql('UPDATE language SET english_name = "english" WHERE english_name IS NULL'); $this->addSql('ALTER TABLE language CHANGE english_name english_name VARCHAR(255) NOT NULL'); @@ -182,7 +183,7 @@ public function up(Schema $schema): void $englishName = $item['english_name']; if (isset($languages[$englishName])) { $newIso = $languages[$englishName]; - $this->addSql("UPDATE language SET isocode = '$newIso' WHERE id = $id"); + $this->addSql("UPDATE language SET isocode = '$newIso', parent_id = NULL WHERE id = $id"); } } diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20240122221400.php b/src/CoreBundle/Migrations/Schema/V200/Version20240122221400.php index 3b20061e3ab..77b8990a190 100644 --- a/src/CoreBundle/Migrations/Schema/V200/Version20240122221400.php +++ b/src/CoreBundle/Migrations/Schema/V200/Version20240122221400.php @@ -51,8 +51,8 @@ private function updateAndGenerateSubLanguage(array $sublanguage): string $parentIsoCode = $this->connection->executeQuery($parentIsoQuery, [$parentId])->fetchOne(); // Get the prefix of the parent language's isocode - $firstIso = substr($parentIsoCode, 0, 2); - $newIsoCode = $this->generateSublanguageCode($firstIso, $sublanguage['english_name']); + $firstIso = explode('_', $parentIsoCode)[0]; + $newIsoCode = $firstIso.'_'.$sublanguage['id']; // Update the isocode in the language table $updateLanguageQuery = 'UPDATE language SET isocode = ? WHERE id = ?'; @@ -171,27 +171,6 @@ private function executeVueTranslationsUpdate(): void error_log($content); } - private function generateSublanguageCode(string $parentCode, string $variant, int $maxLength = 10): string - { - $parentCode = strtolower(trim($parentCode)); - $variant = strtolower(trim($variant)); - - // Generate a variant code by truncating the variant name - $variantCode = substr($variant, 0, $maxLength - \strlen($parentCode) - 1); - - // Build the complete code - return substr($parentCode.'_'.$variantCode, 0, $maxLength); - } - - private function deleteImportFolder(): void - { - $kernel = $this->container->get('kernel'); - $rootPath = $kernel->getProjectDir(); - $importPath = $rootPath.'/var/translations/import/'; - - $this->recursiveRemoveDirectory($importPath); - } - private function recursiveRemoveDirectory($directory): void { foreach (glob("{$directory}/*") as $file) { diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20250106152600.php b/src/CoreBundle/Migrations/Schema/V200/Version20250106152600.php new file mode 100644 index 00000000000..dec64b6c362 --- /dev/null +++ b/src/CoreBundle/Migrations/Schema/V200/Version20250106152600.php @@ -0,0 +1,34 @@ +addSql( + 'UPDATE language SET parent_id = NULL WHERE english_name IN ("'.implode('", "', $languageNameList).'")' + ); + } +} diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20250106152601.php b/src/CoreBundle/Migrations/Schema/V200/Version20250106152601.php new file mode 100644 index 00000000000..9acc3c216da --- /dev/null +++ b/src/CoreBundle/Migrations/Schema/V200/Version20250106152601.php @@ -0,0 +1,78 @@ +container->get('kernel'); + $baseTranslationPath = $kernel->getProjectDir().'/var/translations/messages.'; + + $fs = new Filesystem(); + + $subLanguages = $this->connection + ->executeQuery("SELECT id, isocode, parent_id FROM language WHERE parent_id IS NOT NULL") + ->fetchAllAssociative() + ; + + /** @var array $subLanguage */ + foreach ($subLanguages as $subLanguage) { + + $parentIsoCode = $this->connection + ->executeQuery('SELECT isocode FROM language WHERE id = ?', [$subLanguage['parent_id']]) + ->fetchOne() + ; + + $newIsoCode = sprintf( + '%s_%d', + explode('_', $parentIsoCode)[0], + $subLanguage['id'] + ); + + $params = [ + 'new_iso' => $newIsoCode, + 'old_iso' => $subLanguage['isocode'], + ]; + + if ($params['new_iso'] === $params['old_iso']) { + continue; + } + + $this->addSql( + 'UPDATE language SET isocode = :new_iso WHERE id = :id', + [ + 'new_iso' => $newIsoCode, + 'id' => $subLanguage['id'], + ] + ); + + $this->addSql('UPDATE user SET locale = :new_iso WHERE locale = :old_iso', $params); + $this->addSql('UPDATE course SET course_language = :new_iso WHERE course_language = :old_iso', $params); + $this->addSql("UPDATE settings SET selected_value = :new_iso WHERE variable = 'platform_language' AND selected_value = :old_iso", $params); + + $oldPoFile = $baseTranslationPath.$params['old_iso'].'.po'; + $newPoFile = $baseTranslationPath.$params['new_iso'].'.po'; + + if ($fs->exists($oldPoFile)) { + $fs->rename($oldPoFile, $newPoFile); + } + } + } +} diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20250106152602.php b/src/CoreBundle/Migrations/Schema/V200/Version20250106152602.php new file mode 100644 index 00000000000..5a8f2a7ada0 --- /dev/null +++ b/src/CoreBundle/Migrations/Schema/V200/Version20250106152602.php @@ -0,0 +1,27 @@ +addSql('ALTER TABLE language CHANGE isocode isocode VARCHAR('.Language::ISO_MAX_LENGTH.') NOT NULL'); + } +}