Skip to content

Commit

Permalink
L10N handler patch.
Browse files Browse the repository at this point in the history
Changelog excerpt:
- Preferred variants can now be specified to the L10N handler.
  • Loading branch information
Maikuolan committed Oct 11, 2023
1 parent 119dc5d commit a56aac8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ found at:

=== Changes made since last versioned release ===

(none)
- [2023.10.12; Maikuolan]: Preferred variants can now be specified to the L10N
handler.

=== Version/Release 1.9.8 ===
PATCH RELEASE.
Expand Down
30 changes: 22 additions & 8 deletions src/L10N.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* L10N handler (last modified: 2023.09.18).
* L10N handler (last modified: 2023.10.12).
*
* This file is a part of the "common classes package", utilised by a number of
* packages and projects, including CIDRAM and phpMussel.
Expand Down Expand Up @@ -37,6 +37,11 @@ class L10N
*/
public $FallbackDirectionality = '';

/**
* @var string Useful in case a string might have variants available.
*/
public $PreferredVariant = '';

/**
* @var string The pluralisation rule to use for integers.
*/
Expand Down Expand Up @@ -131,7 +136,7 @@ public function getPlural($Number, $String)
} else {
return '';
}
if (!is_array($Choices)) {
if (is_string($Choices)) {
return $Choices;
}
if (is_float($Number)) {
Expand All @@ -142,9 +147,14 @@ public function getPlural($Number, $String)
$Choice = 0;
}
if (isset($Choices[$Choice])) {
return $Choices[$Choice];
$Out = $Choices[$Choice];
} else {
$Out = $Number > 1 ? array_pop($Choices) : array_shift($Choices);
}
return $Number > 1 ? array_pop($Choices) : array_shift($Choices);
if (is_array($Out)) {
$Out = ($this->PreferredVariant !== '' && isset($Out[$this->PreferredVariant])) ? $Out[$this->PreferredVariant] : array_shift($Out);
}
return is_string($Out) ? $Out : '';
}

/**
Expand All @@ -156,12 +166,16 @@ public function getPlural($Number, $String)
public function getString($String)
{
if (isset($this->Data[$String])) {
return $this->Data[$String];
$Out = $this->Data[$String];
} elseif ($this->Fallback instanceof \Maikuolan\Common\L10N) {
$Out = $this->Fallback->getString($String);
} else {
$Out = isset($this->Fallback[$String]) ? $this->Fallback[$String] : '';
}
if ($this->Fallback instanceof \Maikuolan\Common\L10N) {
return $this->Fallback->getString($String);
if (is_array($Out)) {
$Out = ($this->PreferredVariant !== '' && isset($Out[$this->PreferredVariant])) ? $Out[$this->PreferredVariant] : array_shift($Out);
}
return isset($this->Fallback[$String]) ? $this->Fallback[$String] : '';
return is_string($Out) ? $Out : '';
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Operation.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Operation handler (last modified: 2023.09.18).
* Operation handler (last modified: 2023.10.12).
*
* This file is a part of the "common classes package", utilised by a number of
* packages and projects, including CIDRAM and phpMussel.
Expand Down Expand Up @@ -228,8 +228,8 @@ public function dataTraverse(&$Data, $Path = [], $AllowNonScalar = false)
return $AllowNonScalar || is_scalar($Data) ? $Data : '';
}
$Segment = str_replace('\.', '.', $Segment);
if (is_array($Data) && isset($Data[$Segment])) {
return $this->dataTraverse($Data[$Segment], $Path, $AllowNonScalar);
if (is_array($Data)) {
return isset($Data[$Segment]) ? $this->dataTraverse($Data[$Segment], $Path, $AllowNonScalar) : '';
}
if (is_object($Data) && property_exists($Data, $Segment)) {
return $this->dataTraverse($Data->$Segment, $Path, $AllowNonScalar);
Expand Down
6 changes: 3 additions & 3 deletions src/YAML.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* YAML handler (last modified: 2023.09.18).
* YAML handler (last modified: 2023.10.12).
*
* This file is a part of the "common classes package", utilised by a number of
* packages and projects, including CIDRAM and phpMussel.
Expand Down Expand Up @@ -466,8 +466,8 @@ public function dataTraverse(&$Data, $Path = [], $AllowNonScalar = false)
return $AllowNonScalar || is_scalar($Data) ? $Data : '';
}
$Segment = str_replace('\.', '.', $Segment);
if (is_array($Data) && isset($Data[$Segment])) {
return $this->dataTraverse($Data[$Segment], $Path, $AllowNonScalar);
if (is_array($Data)) {
return isset($Data[$Segment]) ? $this->dataTraverse($Data[$Segment], $Path, $AllowNonScalar) : '';
}
if (is_object($Data) && property_exists($Data, $Segment)) {
return $this->dataTraverse($Data->$Segment, $Path, $AllowNonScalar);
Expand Down

0 comments on commit a56aac8

Please sign in to comment.