Skip to content

Commit

Permalink
L10N handler update.
Browse files Browse the repository at this point in the history
Changelog excerpt:
- The arrayFromL10nToArray method, common to CIDRAM's and phpMussel's
  codebases, as well as to the code for the DocGen scripts at both those
  projects' documentation repositories, has been integrated to the L10N
  handler, so that it can be removed from those codebases, and can instead
  be called from the L10N handler, for cleaner/easier maintainability).
  • Loading branch information
Maikuolan committed Oct 12, 2023
1 parent a56aac8 commit c970e2d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ found at:
=== Changes made since last versioned release ===

- [2023.10.12; Maikuolan]: Preferred variants can now be specified to the L10N
handler.
handler. The arrayFromL10nToArray method, common to CIDRAM's and phpMussel's
codebases, as well as to the code for the DocGen scripts at both those
projects' documentation repositories, has been integrated to the L10N
handler, so that it can be removed from those codebases, and can instead be
called from the L10N handler, for cleaner/easier maintainability).

=== Version/Release 1.9.8 ===
PATCH RELEASE.
Expand Down
52 changes: 52 additions & 0 deletions src/L10N.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,58 @@ public function getString($String)
return is_string($Out) ? $Out : '';
}

/**
* Parses an array of L10N data references from L10N data to an array.
*
* @param string|array $References The L10N data references.
* @return array An array of L10N data.
*/
public function arrayFromL10nToArray($References): array
{
if (!is_array($References)) {
$References = [$References];
}
$Out = [];
foreach ($References as $Reference) {
$Try = '';
if (isset($this->Data[$Reference])) {
$Try = $this->Data[$Reference];
} elseif (is_array($this->Fallback)) {
if (isset($this->Fallback[$Reference])) {
$Try = $this->Fallback[$Reference];
}
} elseif ($this->Fallback instanceof \Maikuolan\Common\L10N) {
if (isset($this->Fallback->Data[$Reference])) {
$Try = $this->Fallback->Data[$Reference];
} elseif (is_array($this->Fallback->Fallback) && isset($this->Fallback->Fallback[$Reference])) {
$Try = $this->Fallback->Fallback[$Reference];
}
}
if ($Try === '') {
if (($SPos = strpos($Reference, ' ')) !== '') {
$Try = (($TryFrom = $this->getString(substr($Reference, 0, $SPos))) !== '' && strpos($TryFrom, '%s') !== false) ? sprintf($TryFrom, substr($Reference, $SPos + 1)) : $Reference;
} else {
$Try = $Reference;
}
}
$Reference = (!is_array($Try) || preg_match('~^[a-z]{2}(?:-[A-Z]{2})?$~', key($Try))) ? [$Try] : $Try;
foreach ($Reference as $Key => $Value) {
if (is_array($Value)) {
$Value = $this->PreferredVariant !== '' && isset($Value[$this->PreferredVariant]) ? $Value[$this->PreferredVariant] : array_shift($Value);
if (!is_string($Value)) {
$Value = '';
}
}
if (!is_string($Key)) {
$Out[] = $Value;
continue;
}
$Out[$Key] = $Value;
}
}
return $Out;
}

/**
* For when there aren't multiple forms.
*
Expand Down

0 comments on commit c970e2d

Please sign in to comment.