react-intl 使用
The first part of this series was an introduction of the PHP Intl extension and of how to localize your application’s messages. In this part, we’re going to learn about localizing numbers, dates, calendars, and similar complex data. Let’s get started!
本系列的第一部分介绍了PHP Intl扩展以及如何本地化应用程序的消息。 在这一部分中,我们将学习本地化数字,日期,日历和类似的复杂数据。 让我们开始吧!
This may sound odd, but one of my main concerns when formatting numbers is working with decimal points, as they differ from place to place. Check Wikipedia for more details about different decimal mark variations.
这听起来可能很奇怪,但是在格式化数字时,我主要关心的问题之一是小数点,因为它们之间的位置不同。 有关不同的十进制标记变体的更多详细信息,请查阅Wikipedia 。
Style1,234,567.891234567.891234567,891,234,567·891.234.567,891˙234˙567,8912,34,567.891’234’567.891’234’567,891.234.567’89123,4567.89 样式 1,234,567.89 1234567.89 1234567,89 1,234,567·89 1.234.567,89 1˙234˙567,89 12,34,567.89 1'234'567.89 1'234'567,89 1.234.567'89 123,4567.89The PHP Intl extension has a NumberFormatter which deals with number localization:
PHP Intl扩展具有一个NumberFormatter ,用于处理数字本地化:
$numberFormatter = new NumberFormatter( 'de_DE', NumberFormatter::DECIMAL ); var_dump( $numberFormatter->format(123456789) ); $numberFormatter = new NumberFormatter( 'en_US', NumberFormatter::DECIMAL ); var_dump( $numberFormatter->format(123456789) ); $numberFormatter = new NumberFormatter( 'ar', NumberFormatter::DECIMAL ); var_dump( $numberFormatter->format(123456789) ); $numberFormatter = new NumberFormatter( 'bn', NumberFormatter::DECIMAL ); var_dump( $numberFormatter->format(123456789) ); string(11) "123.456.789" string(11) "123,456,789" string(22) "١٢٣٬٤٥٦٬٧٨٩" string(30) "১২,৩৪,৫৬,৭৮৯"The first parameter is the locale code, and the second is the formatting style. In this case, we’re formatting decimals.
第一个参数是语言环境代码,第二个参数是格式样式。 在这种情况下,我们要格式化小数。
Formatting styles describe how our numbers should be formatted: decimal, currency, duration, etc. Check the list of available formatting styles in the documentation. Let’s try some examples for different styles:
格式样式描述了应如何格式化我们的数字:十进制,货币,持续时间等。请查看文档中可用的格式样式列表。 让我们尝试一些不同风格的示例:
$numberFormatter = new NumberFormatter( 'en_US', NumberFormatter::DECIMAL ); $numberFormatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 2); var_dump( $numberFormatter->format(1234.56789) ); $numberFormatter = new NumberFormatter( 'en_US', NumberFormatter::DECIMAL ); $numberFormatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 2); var_dump( $numberFormatter->format(1234) ); string(8) "1,234.57" string(8) "1,234.00"When specifying the fractional part attribute, the value is rounded up using the up mode. We can change that by specifying the rounding style.
指定小数部分属性时,使用向上模式将值四舍五入。 我们可以通过指定舍入样式来更改它。
$numberFormatter = new NumberFormatter( 'en_US', NumberFormatter::DECIMAL ); $numberFormatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 2); $numberFormatter->setAttribute(NumberFormatter::ROUNDING_MODE, NumberFormatter::ROUND_CEILING); var_dump($numberFormatter->format(1234.5678) ); $numberFormatter = new NumberFormatter( 'en_US', NumberFormatter::DECIMAL ); $numberFormatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 2); $numberFormatter->setAttribute(NumberFormatter::ROUNDING_MODE, NumberFormatter::ROUND_DOWN); var_dump($numberFormatter->format(1234.5678) ); string(8) "1,234.57" string(8) "1,234.56"The spellout and duration options from the first part can also be used here with the following options:
第一部分的spellout和duration选项也可以在此处与以下选项一起使用:
$numberFormatter = new NumberFormatter( 'en_US', NumberFormatter::DURATION ); var_dump( $numberFormatter->format(123) ); $numberFormatter = new NumberFormatter( 'en_US', NumberFormatter::SPELLOUT ); var_dump( $numberFormatter->format(123) ); string(4) "2:03" string(24) "one hundred twenty-three"We can also parse strings to get a formatted value from them:
我们还可以解析字符串以从中获取格式化的值:
$numberFormatter = new NumberFormatter( 'en_US', NumberFormatter::DURATION ); var_dump( $numberFormatter->parse("4:03") ); $numberFormatter = new NumberFormatter( 'en_US', NumberFormatter::SPELLOUT ); var_dump( $numberFormatter->parse("one hundred") ); float(243) float(100)Although we can totally make our application locale agnostic, we should switch between locales to test the various changes.
尽管我们完全可以使应用程序的语言环境与众不同,但是我们应该在语言环境之间切换以测试各种更改。
Formatting numbers as currencies is not very different, we only change the formatting type and add the currency code.
将数字设置为货币格式并没有太大区别,我们只更改格式类型并添加货币代码。
$numberFormatter = new NumberFormatter( 'en_US', NumberFormatter::CURRENCY ); var_dump( $numberFormatter->formatCurrency(1234.56789, "USD" ) ); string(9) "$1,234.57"We can avoid typing the currency code for every locale by calling the getSymbol method on the NumberFormatter instance.
通过在NumberFormatter实例上调用getSymbol方法,可以避免在每个语言环境中键入货币代码。
$numberFormatter = new NumberFormatter( 'en_US', NumberFormatter::CURRENCY ); var_dump( $numberFormatter->formatCurrency(1234.56789, $numberFormatter->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL)) ); $numberFormatter = new NumberFormatter( 'fr_FR', NumberFormatter::CURRENCY ); var_dump( $numberFormatter->formatCurrency(1234.56789, $numberFormatter->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL)) ); string(9) "$1,234.57" string(14) "1 234,57 €"We can use the attributes we mentioned earlier ($numberFormatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 2);) with currencies, too.
我们也可以将我们前面提到的属性( $numberFormatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 2); )与货币一起使用。
Before using PHP Intl calendars, we need to brush up on timezones and try a quick example:
在使用PHP Intl日历之前,我们需要了解时区并尝试一个简单的示例:
A time zone is a region that observes a uniform standard time for legal, commercial, and social purposes. Time zones tend to follow the boundaries of countries and their subdivisions because it is convenient for areas in close commercial or other communication to keep the same time.
时区是为了法律,商业和社会目的而遵守统一标准时间的区域。 时区倾向于遵循国家及其细分的边界,因为在进行紧密商业或其他交流的区域保持同一时间很方便。
— Wikipedia
—维基百科
The IntlTimeZone class is responsible for creating and managing timezones. This is no different from timezones in the DateTimeZone class:
IntlTimeZone类负责创建和管理时区。 这与DateTimeZone类中的时区没有什么不同:
$timezone = IntlTimeZone::createDefault(); var_dump($timezone, $timezone->getDisplayName()); $timezone = IntlTimeZone::countEquivalentIDs("GMT"); var_dump($timezone); $timezone = IntlTimeZone::createTimeZone("GMT"); var_dump($timezone); object(IntlTimeZone)#1 (4) { ["valid"]=> bool(true) ["id"]=> string(3) "UTC" ["rawOffset"]=> int(0) ["currentOffset"]=> int(0) } string(3) "GMT" int(10) object(IntlTimeZone)#1 (4) { ["valid"]=> bool(true) ["id"]=> string(3) "GMT" ["rawOffset"]=> int(0) ["currentOffset"]=> int(0) }The PHP Intl extension has a nice, expressive API for doing calendar operations:
PHP Intl扩展具有执行日历操作的出色表达API:
$calendar = IntlCalendar::createInstance(); var_dump($calendar->getTimeZone()->getId()); string(3) "UTC"The createInstance method accepts a timezone (from the previous section), and a locale code. We can also create a calendar instance from a DateTime instance.
createInstance方法接受时区(来自上一节)和语言环境代码。 我们还可以从DateTime实例创建日历实例。
$calendar = IntlCalendar::fromDateTime( (new DateTime) );We can do all sorts of comparisons between calendar dates.
我们可以在日历日期之间进行各种比较。
$calendar1 = IntlCalendar::fromDateTime( DateTime::createFromFormat('j-M-Y', '11-Apr-2016') ); $calendar2 = IntlCalendar::createInstance(); $durationFormatter = new NumberFormatter( 'en_US', NumberFormatter::DURATION ); $diff = $calendar1->fieldDifference($calendar2->getTime(), IntlCalendar::FIELD_MILLISECOND); var_dump( $calendar1->equals($calendar2), $diff, $durationFormatter->format( $diff ) ); bool(true) int(595) string(4) "9:55"Important note: Our calendar1 variable is advanced by the diff value, so the calendars are equal after the fieldDifference method call. Keep this mutability in mind when using these classes!
重要说明 :我们的calendar1变量使用diff值进行超前,因此在fieldDifference方法调用之后,日历是相等的。 使用这些类时,请记住这种可变性!
The IntlCalendar::FIELD_MILLISECOND constant defines the type of comparison, year, month, week, etc. Check the documentation for the full list.
IntlCalendar::FIELD_MILLISECOND常量定义比较的类型,年,月,周等。请查看文档以获取完整列表。
If you’ve used the briannesbitt/carbon package before, you may have liked the expressive syntax for navigating dates. We can do the same using the IntlCalendar class.
如果您以前使用过briannesbitt/carbon软件包,则可能喜欢使用用于导航日期的表达语法。 我们可以使用IntlCalendar类执行相同的IntlCalendar 。
$calendar1 = IntlCalendar::createInstance(); var_dump(IntlDateFormatter::formatObject($calendar1)); $calendar1->add(IntlCalendar::FIELD_MONTH, 1); var_dump(IntlDateFormatter::formatObject($calendar1)); $calendar1->add(IntlCalendar::FIELD_DAY_OF_WEEK, 1); var_dump(IntlDateFormatter::formatObject($calendar1)); $calendar1->add(IntlCalendar::FIELD_WEEK_OF_YEAR, 1); var_dump(IntlDateFormatter::formatObject($calendar1)); string(25) "Apr 12, 2016, 12:06:22 AM" string(25) "May 12, 2016, 12:06:22 AM" string(25) "May 15, 2016, 12:06:22 AM" string(25) "May 22, 2016, 12:06:22 AM"Check the documentation for more details and examples.
查看文档以获取更多详细信息和示例。
In this two part series, we discovered the PHP Intl extension and the ICU library. The extension still has some other parts like Collators, Spoofchecker, UConverter, etc. We’ll focus on those in subsequent posts, but in the meanwhile, if you have any questions or comments, please leave them below!
在这个由两部分组成的系列文章中,我们发现了PHP Intl扩展和ICU库。 该扩展程序还有其他一些部分,例如Collators , Spoofchecker , UConverter等。我们将在后续文章中重点介绍这些内容,但是与此同时,如果您有任何问题或意见,请将其留在下面!
翻译自: https://www.sitepoint.com/localizing-dates-currency-and-numbers-with-php-intl/
react-intl 使用
相关资源:riw:控制台应用程序和库,可帮助您结合react-intl本地化React应用程序-源码