Skip to content

Commit

Permalink
Add support for cross-domain aliases.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Dec 15, 2023
1 parent 7622e42 commit 6d9cb34
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions module/VuFind/src/VuFind/I18n/Translator/Loader/ExtendedIni.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ class ExtendedIni implements FileLoaderInterface
*/
protected $loadedAliasFiles = [];

/**
* Loaded TextDomains used for resolving aliases.
*
* @var array
*/
protected $aliasDomains = [];

/**
* Constructor
*
Expand Down Expand Up @@ -151,12 +158,18 @@ public function load($locale, $filename)
// Reset the loaded files list:
$this->resetLoadedFiles();

// Identify the current TextDomain name:
$currentDomain = empty($filename) ? 'default' : $filename;

// Load base data:
$data = $this->loadLanguageLocale($locale, $filename, $this->useAliases);

// Set up a reference to the current domain for use in alias processing:
$this->aliasDomains[$currentDomain] = $data;

// Apply aliases:
if ($this->useAliases) {
$this->applyAliases($data);
$this->applyAliases($data, $locale, $currentDomain);
}

// Load fallback data, if any:
Expand Down Expand Up @@ -236,13 +249,26 @@ protected function loadLanguageLocale($locale, $domain, $processAliases = false)
*
* @return void
*/
protected function applyAliases(TextDomain $data): void
protected function applyAliases(TextDomain $data, string $currentLocale, string $currentDomain): void
{
foreach ($this->aliases as $alias => $target) {
// If the current alias target does not include a TextDomain part, assume it refers
// to the current active TextDomain:
if (count($target) < 2) {
array_unshift($target, $currentDomain);
}
[$domain, $key] = $target;
// If the alias references another TextDomain, we need to load that now; note that we
// do not process aliases again at this step, so aliases to aliases will not work.
if (!isset($this->aliasDomains[$domain])) {
$this->aliasDomains[$domain] = $this->loadLanguageLocale($currentLocale, $domain);
}
// Do not overwrite existing values with alias, and do not create aliases
// when target values are missing.
if ($data->offsetExists($target) && !$data->offsetExists($alias)) {
$data->offsetSet($alias, $data->offsetGet($target));
if ($this->aliasDomains[$domain]->offsetExists($key)
&& !$data->offsetExists($alias)
) {
$data->offsetSet($alias, $this->aliasDomains[$domain]->offsetGet($key));
}
}
}
Expand Down Expand Up @@ -277,6 +303,18 @@ public function enableAliases(): void
$this->useAliases = true;
}

/**
* Expand an alias string into an array (either [textdomain, key] or just [key]).
*
* @param string $alias String to parse
*
* @return string[]
*/
protected function normalizeAlias(string $alias): array
{
return explode('::', $alias);
}

/**
* Load an alias configuration (if not already loaded) and mark it loaded.
*
Expand All @@ -289,7 +327,10 @@ protected function markAndLoadAliases(string $filename): void
if (!in_array($filename, $this->loadedAliasFiles)) {
$this->loadedAliasFiles[] = $filename;
if (file_exists($filename)) {
$this->aliases = array_merge($this->aliases, parse_ini_file($filename));
// Parse and normalize the alias configuration:
$newAliases = array_map([$this, 'normalizeAlias'], parse_ini_file($filename));
// Merge with pre-existing aliases:
$this->aliases = array_merge($this->aliases, $newAliases);
}
}
}
Expand Down

0 comments on commit 6d9cb34

Please sign in to comment.