Skip to content

Commit 7e2d278

Browse files
authored
feat: implement FormatPHP::formatDisplayName() method (#47)
1 parent 4074912 commit 7e2d278

15 files changed

+3522
-3
lines changed

CHANGELOG.md

+22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## 0.7.0 - 2022-02-11
9+
10+
### Added
11+
12+
- Provide functionality for formatting locale-appropriate display names with `Intl\DisplayNames` and `FormatPHP::formatDisplayName()`.
13+
14+
### Changed
15+
16+
- Nothing.
17+
18+
### Deprecated
19+
20+
- Nothing.
21+
22+
### Removed
23+
24+
- Nothing.
25+
26+
### Fixed
27+
28+
- Nothing.
29+
830
## 0.6.0 - 2022-02-07
931

1032
### Added

README.md

+61-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ echo $formatphp->formatMessage([
8181

8282
### Formatting Numbers and Currency
8383

84-
You may use the methods `formatNumber()` and `formatCurrency()` for format
85-
numbers and currency, according to the locale.
84+
You may use the methods `formatNumber()` and `formatCurrency()` to format
85+
numbers and currency according to the locale.
8686

8787
```php
8888
use FormatPHP\Config;
@@ -366,6 +366,65 @@ Additional options include:
366366
You may use the zone names of the [IANA time zone database](https://www.iana.org/time-zones),
367367
such as "Asia/Shanghai", "Asia/Kolkata", "America/New_York".
368368

369+
### Formatting Display Names of Languages, Regions, Currency, and More
370+
371+
You may use the method `formatDisplayName()` to format the display names of
372+
languages, regions, currency, and more. This returns a locale-appropriate,
373+
translated string for the type requested.
374+
375+
```php
376+
use FormatPHP\Config;
377+
use FormatPHP\FormatPHP;
378+
use FormatPHP\Intl;
379+
380+
$config = new Config(new Intl\Locale('en-US'));
381+
$formatphp = new FormatPHP($config);
382+
383+
echo $formatphp->formatDisplayName('zh-Hans-SG', new Intl\DisplayNamesOptions([
384+
'type' => 'language',
385+
])); // e.g., "Chinese (Simplified, Singapore)"
386+
387+
echo $formatphp->formatDisplayName('Deva', new Intl\DisplayNamesOptions([
388+
'type' => 'script',
389+
])); // e.g., "Devanagari"
390+
391+
echo $formatphp->formatDisplayName('CNY', new Intl\DisplayNamesOptions([
392+
'type' => 'currency',
393+
])); // e.g., "Chinese yuan"
394+
395+
echo $formatphp->formatDisplayName('CNY', new Intl\DisplayNamesOptions([
396+
'type' => 'currency',
397+
'style' => 'narrow',
398+
])); // e.g., "¥"
399+
400+
echo $formatphp->formatDisplayName('UN', new Intl\DisplayNamesOptions([
401+
'type' => 'region',
402+
])); // e.g., "United Nations"
403+
```
404+
405+
#### Using Intl\DisplayNamesOptions with formatDisplayName()
406+
407+
When formatting display names, you must provide a `DisplayNamesOptions` instance
408+
with at least a `type` defined.
409+
410+
* `type`: The type of data for which we wish to format a display name. This
411+
currently supports `currency`, `language`, `region`, and `script`. While
412+
[ECMA-402](https://tc39.es/ecma402/#sec-intl-displaynames-constructor) defines
413+
`calendar` and `dateTimeField` as additional types, these types are not
414+
implemented in Node.js or in any browsers. In fact, if set, the
415+
implementations throw exceptions, so this FormatPHP follows the same
416+
pattern.
417+
* `fallback`: The fallback strategy to use. If we are unable to format a display
418+
name, we will return the same code provided if `fallback` is set to `code.` If
419+
`fallback` is `none`, then we return `null`. The default `fallback` is `code`.
420+
* `style`: The formatting style to use: `long`, `short`, or `narrow`. This
421+
currently only affects the display name when `type` is `currency`, and the
422+
default is `long`.
423+
* `languageDisplay`: This is a suggestion for displaying the language according
424+
to the locale's dialect or standard representation. In JavaScript, this
425+
defaults to `dialect`. For now, PHP supports only the `standard`
426+
representation, so `dialect` has no effect.
427+
369428
### Rich Text Formatting (Use of Tags in Messages)
370429

371430
While the ICU message syntax does not prohibit the use of HTML tags in formatted
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* This file is part of skillshare/formatphp
5+
*
6+
* skillshare/formatphp is open source software: you can distribute
7+
* it and/or modify it under the terms of the MIT License
8+
* (the "License"). You may not use this file except in
9+
* compliance with the License.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14+
* implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*
17+
* @copyright Copyright (c) Skillshare, Inc. <https://www.skillshare.com>
18+
* @license https://opensource.org/licenses/MIT MIT License
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace FormatPHP\Exception;
24+
25+
/**
26+
* Thrown when we are unable to format a display name
27+
*/
28+
class UnableToFormatDisplayNameException extends UnableToFormatStringException
29+
{
30+
}

src/FormatPHP.php

+15
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use Exception as PhpException;
2828
use FormatPHP\Intl\DateTimeFormat;
2929
use FormatPHP\Intl\DateTimeFormatOptions;
30+
use FormatPHP\Intl\DisplayNames;
31+
use FormatPHP\Intl\DisplayNamesOptions;
3032
use FormatPHP\Intl\MessageFormat;
3133
use FormatPHP\Intl\NumberFormat;
3234
use FormatPHP\Intl\NumberFormatOptions;
@@ -156,6 +158,19 @@ public function formatCurrency($value, string $currencyCode, ?NumberFormatOption
156158
return $this->formatNumber($value, $options);
157159
}
158160

161+
/**
162+
* @throws Exception\InvalidArgumentException
163+
* @throws Exception\UnableToFormatDisplayNameException
164+
*
165+
* @inheritdoc
166+
*/
167+
public function formatDisplayName(string $value, ?DisplayNamesOptions $options = null): ?string
168+
{
169+
$formatter = new DisplayNames($this->config->getLocale(), $options);
170+
171+
return $formatter->of($value);
172+
}
173+
159174
protected function getConfig(): ConfigInterface
160175
{
161176
return $this->config;

src/FormatterInterface.php

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
use DateTimeInterface as PhpDateTimeInterface;
2626
use FormatPHP\Intl\DateTimeFormatOptions;
27+
use FormatPHP\Intl\DisplayNamesOptions;
2728
use FormatPHP\Intl\NumberFormatOptions;
2829

2930
/**
@@ -109,4 +110,15 @@ public function formatNumber($number, ?NumberFormatOptions $options = null): str
109110
* @throws Exception\UnableToFormatNumberException
110111
*/
111112
public function formatCurrency($value, string $currencyCode, ?NumberFormatOptions $options = null): string;
113+
114+
/**
115+
* Returns a translated, localized display string for the given code
116+
*
117+
* Additional options may be provided to configure how the display name
118+
* should be formatted.
119+
*
120+
* @throws Exception\InvalidArgumentException
121+
* @throws Exception\UnableToFormatDisplayNameException
122+
*/
123+
public function formatDisplayName(string $value, ?DisplayNamesOptions $options = null): ?string;
112124
}

0 commit comments

Comments
 (0)