Skip to content

Commit

Permalink
Update i18nTextCollector to collect strings also from themes
Browse files Browse the repository at this point in the history
  • Loading branch information
xini committed Apr 12, 2023
1 parent 2c874a1 commit d5fae7b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/i18n/Messages/YamlWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ public function write($messages, $locale, $path)
}

// Create folder for lang files
$langFolder = $path . '/lang';
$langFolder = $path . DIRECTORY_SEPARATOR . 'lang';
if (!file_exists($langFolder ?? '')) {
Filesystem::makeFolder($langFolder);
touch($langFolder . '/_manifest_exclude');
touch($langFolder . DIRECTORY_SEPARATOR . '_manifest_exclude');
}

// De-normalise messages and convert to yml
$content = $this->getYaml($messages, $locale);

// Open the English file and write the Master String Table
$langFile = $langFolder . '/' . $locale . '.yml';
$langFile = $langFolder . DIRECTORY_SEPARATOR . $locale . '.yml';
if ($fh = fopen($langFile ?? '', "w")) {
fwrite($fh, $content ?? '');
fclose($fh);
Expand Down
44 changes: 33 additions & 11 deletions src/i18n/TextCollection/i18nTextCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SilverStripe\Core\Manifest\ClassLoader;
use SilverStripe\Core\Manifest\Module;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Core\Path;
use SilverStripe\Dev\Debug;
use SilverStripe\Control\Director;
use ReflectionClass;
Expand Down Expand Up @@ -174,6 +175,8 @@ public function run($restrictToModules = null, $mergeWithExisting = false)
return;
}

$modules = $this->getModulesAndThemes();

// Write each module language file
foreach ($entitiesByModule as $moduleName => $entities) {
// Skip empty translations
Expand All @@ -183,7 +186,7 @@ public function run($restrictToModules = null, $mergeWithExisting = false)

// Clean sorting prior to writing
ksort($entities);
$module = ModuleLoader::inst()->getManifest()->getModule($moduleName);
$module = $modules[$moduleName];
$this->write($module, $entities);
}
}
Expand All @@ -210,9 +213,9 @@ public function collect($restrictToModules = [], $mergeWithExisting = false)
// Restrict modules we update to just the specified ones (if any passed)
if (!empty($restrictToModules)) {
// Normalise module names
$modules = array_filter(array_map(function ($name) {
$module = ModuleLoader::inst()->getManifest()->getModule($name);
return $module ? $module->getName() : null;
$allModules = $this->getModulesAndThemes();
$modules = array_filter(array_map(function ($name) use ($allModules) {
return array_key_exists($name, $allModules) ? $name : null;
}, $restrictToModules ?? []));
// Remove modules
foreach (array_diff(array_keys($entitiesByModule ?? []), $modules) as $module) {
Expand Down Expand Up @@ -370,9 +373,10 @@ protected function findModuleForClass($class)
protected function mergeWithExisting($entitiesByModule)
{
// For each module do a simple merge of the default yml with these strings
$modules = $this->getModulesAndThemes();
foreach ($entitiesByModule as $module => $messages) {
// Load existing localisations
$masterFile = ModuleLoader::inst()->getManifest()->getModule($module)->getPath() .
$masterFile = $modules[$module]->getPath() .
DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . $this->defaultLocale . '.yml';
$existingMessages = $this->getReader()->read($this->defaultLocale, $masterFile);

Expand All @@ -396,11 +400,11 @@ protected function getEntitiesByModule()
{
// A master string tables array (one mst per module)
$entitiesByModule = [];
$modules = ModuleLoader::inst()->getManifest()->getModules();
foreach ($modules as $module) {
$modules = $this->getModulesAndThemes();
foreach ($modules as $moduleName => $module) {
// we store the master string tables
$processedEntities = $this->processModule($module);
$moduleName = $module->getName();
$moduleName = strpos($moduleName, 'themes/') === 0 ? $moduleName : $module->getName();
if (isset($entitiesByModule[$moduleName])) {
$entitiesByModule[$moduleName] = array_merge_recursive(
$entitiesByModule[$moduleName],
Expand Down Expand Up @@ -444,6 +448,26 @@ protected function getEntitiesByModule()
}
return $entitiesByModule;
}

/**
* Loads all modules and themes installed, including app. Uses the format of
* the @link ModuleLoader manifest for themes as well.
* Themes can be references with "themes/{theme name}".
*/
private function getModulesAndThemes(): array
{
$modules = ModuleLoader::inst()->getManifest()->getModules();
// load themes as modules
$themes = array_diff(scandir(THEMES_PATH), ['..', '.']);
if ($themes) {
foreach ($themes as $theme) {
if (is_dir(Path::join(THEMES_PATH, $theme))) {
$modules['themes/' . $theme] = new Module(Path::join(THEMES_PATH, $theme), BASE_PATH);
}
}
}
return $modules;
}

/**
* Write entities to a module
Expand Down Expand Up @@ -511,7 +535,7 @@ protected function getFileListForModule(Module $module)
$modulePath = $module->getPath();

// Search all .ss files in themes
if (stripos($module->getRelativePath() ?? '', 'themes' . DIRECTORY_SEPARATOR) === 0) {
if (stripos($module->getRelativePath() ?? '', 'themes/') === 0) {
return $this->getFilesRecursive($modulePath, null, 'ss');
}

Expand Down Expand Up @@ -940,8 +964,6 @@ protected function normalizeEntity($fullName, $_namespace = null)
return "{$namespace}.{$entity}";
}



/**
* Helper function that searches for potential files (templates and code) to be parsed
*
Expand Down

0 comments on commit d5fae7b

Please sign in to comment.