Skip to content

Novis\Intl\Locale::resolve

Shows which locale's data is used for a locale tag.

Locale::resolve takes a list of locale tags and a Locale::Service, and returns one tag for each, in the same order. The result is the locale whose rules the Novis\Intl classes use for that service. When there is no data for a region, the language's data is used. With Locale::Service::Dates, "de-AT" gives "de".

The result is "und" when only the general rules are used. That happens for a tag with no data at all, such as "zz". It also happens when a language has no special rules for that service. German sorts with the general rules, so "de" with Locale::Service::Collation gives "und".

Locale::Service has one case for each class: Collation, Numbers, Plurals, Dates, RelativeTime, Lists and Segmentation.

Locale::resolve throws a LogicError when a tag is not valid.

Good to know: the other Novis\Intl methods do not throw for a valid tag with no data. They use the general rules. Call Locale::resolve when you need to know which data was used.

Parameters

  • $tagsarray<string>
  • $serviceService

Return value

Returns array<string>.

Examples

Which data a tag uses

<?nvs
// Shows which locale's date rules are used for three tags.
use Novis\Intl\Locale;
array<string> $tags = ['de-AT', 'fr-CA', 'zz'];
// "de-AT" and "fr-CA" use their language's data. "zz" has no data, so the result is "und".
// This prints de | fr | und.
echo Core\Str::join(Locale::resolve($tags, Locale::Service::Dates), ' | '), "\n";
Output
de | fr | und

Each service has its own data

<?nvs
// Shows that one tag can use different data for different services.
use Novis\Intl\Locale;
// German has its own date rules. This prints de.
echo Locale::resolve(['de-AT'], Locale::Service::Dates)[0], "\n";
// German sorts text with the general rules. This prints und.
echo Locale::resolve(['de-AT'], Locale::Service::Collation)[0], "\n";
Output
de
und

Warn about missing data

<?nvs
// Shows how to find the locales in a settings list that have no date rules of their own.
use Novis\Intl\Locale;
array<string> $configured = ['en-GB', 'de-CH', 'zz', 'ja'];
array<string> $used = Locale::resolve($configured, Locale::Service::Dates);
// The result has one tag for each configured tag, in the same order. This prints zz.
for (int $i = 0; $i < Core\Arr::count($configured); $i++) {
if ($used[$i] == 'und') {
echo $configured[$i], "\n";
}
}
Output
zz