Closed peter279k closed 5 years ago
I need to finish writing documentation for the class, to better explain how it's meant to be used and how it's meant to work. I think, some of these things will make more sense when there's accompanying documentation to explain it all properly.
$Data
and $Fallback
should always be arrays, containing all the possible L10N data that the calling app/script might need, but there should never be multiple sets of pluralisation rules used by any particular, singular given L10N array (because the way that pluralisation works in any particular given language should always, whenever possible, be served in a consistent way). For determining which rules to use, the class expects that the L10N arrays ($Data
and $Fallback
) should contain at least two elements, IntegerRule
and FractionRule
. These two elements should indicate which pluralisation rules the instance should use when serving L10N data, for integers and fractions respectively.
$Choices
is sourced from $Data
(when the desired L10N string exists in $Data
), or from $Fallback
(when the desired L10N string doesn't exist in $Data
, but exists in $Fallback
). Providing the ability to source from a fallback means that a calling app/script can safely support language translations that haven't been fully completed yet (i.e., incomplete translations), and can then use some other fully completed language translation as a fallback for that, so that (when the calling app/script tries to call some string which doesn't exist yet in the incomplete translation) the end-user will still see something - albeit in a different language - instead of just seeing an error message, or an empty string (which would be even less meaningful than something written in the wrong language).
There's always a strong likelihood, that two completely different languages, will have two completely different sets of rules regarding how pluralisation works, which is why both the primary L10N array ($Data
) and the fallback L10N array ($Fallback
) must be able to define their own preferred pluralisation rules for themselves respectively.
Each possible (i.e., each supported) pluralisation rule has its own dedicated method, each which returns an integer, representing the key for the correct corresponding element to choose from within the array. Because the rule value should be the same as the name of the method needed to process that rule, the rule value should always be a string (so, I don't think there'll be any problems for the second method_exists
parameter).
As a simple example:
<?php
$ExampleL10NData = [
'IntegerRule' => 'int2Type4', // Integer rule to use for English.
'FractionRule' => 'int1', // Fraction rule to use for English.
'apples' => [
'There is %s apple on the tree.',
'There are %s apples on the tree.'
]
];
$L10N = new \Maikuolan\Common\L10N($ExampleL10NData); // Skipping the fallback in this case, because it's just an example.
foreach ([0, 1, 2, 3, 4, 5] as $Number) {
echo sprintf($L10N->getPlural($Number, 'apples'), $Number) . PHP_EOL;
}
Produces:
There are 0 apples on the tree.
There is 1 apple on the tree.
There are 2 apples on the tree.
There are 3 apples on the tree.
There are 4 apples on the tree.
There are 5 apples on the tree.
Hi @Maikuolan, I realize this from sample code.
There's not the problem and I think I misunderstand the usage.
This issue will be closed because of explanation :).
When I initialize the
L10N
test on PR #3, I found the following issue:Consider the
L10N::getPlural
method and we can find the$Choices
variable in this method.This
$Choices
variable seems that it's from the$Data
orFallback
variables and they can accept the array with multiple rules.This will be the problem when assigning the array to
$Data
and$Fallback
variables.The second parameter in
method_exists
method only accepts thestring
type, notarray
type.@Maikuolan, you should consider this and let the
getPlural
method can find the current rule in$Choices
array and the constructor can also accept the$Data
andFallback
with multiple rules array.