WordPress.org

Make WordPress Core

Opened 7 years ago

Last modified 3 years ago

#22225 reviewing enhancement

WordPress does not localize ordinal suffixes in dates

Reported by: mihaimihai Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.4.2
Component: I18N Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

In wp-includes/functions.php function date_i18n(), elements like month, month abbreviation, weekday etc.. are localized but not the ordinal suffixes (e.g. st, nd, rt, th)

effect: when using the ordinal suffixes for dates

e.g.

<?php the_time('F jS, Y'); ?>


on translated/localized page page, the month name is translated (F) but the ordinal suffix no.

Not browser/OS/environment dependent.

Attachments (3)

22225.patch (5.2 KB) - added by realloc 4 years ago.
Patch and Unit tests
22225.2.patch (5.2 KB) - added by realloc 4 years ago.
ordinal suffix for days
22225.3.patch (6.2 KB) - added by realloc 4 years ago.
Patch and Unit tests with tests for filter

Download all attachments as: .zip

Change History (18)

#2 @jrf
7 years ago

+1

#3 @chriscct7
4 years ago

  • Keywords needs-patch added

@realloc
4 years ago

Patch and Unit tests

#4 @realloc
4 years ago

  • Keywords needs-patch removed

I had to extend some functionality in WP_locale too. There were no unit tests for this class so I tested all of the things needed together.

#5 @johnbillion
4 years ago

  • Keywords has-patch has-unit-tests needs-testing added

#6 follow-up: @gibrown
4 years ago

@realloc nice approach of using wp-locale, but I think there is some confusion about 'S'. It is the day of the month rather than the month number. So for instance, the output of

<?php
date_i18n( 'S', 1424001600 ) //Feb 15th, 2015

Should not be expecting 'nd' in the unit test, it should be 'th'.

I think it needs a function that is hardcoded into wp-locale to handle each locale. Maybe something based off of this wikipedia article? For some languages we should entirely remove the 'S'. I ended up here due to Russian and Hebrew localization of 'F jS' where I think I understand the best solution to be removing the 'S' entirely. cc @kovshenin

Also looks like in PHP 5.3.0+ there is support for formatting an ordinal. So maybe should use that with a reasonable fall back for old versions of PHP?

#7 in reply to: ↑ 6 @realloc
4 years ago

Replying to gibrown:

@realloc nice approach of using wp-locale, but I think there is some confusion about 'S'. It is the day of the month rather than the month number. So for instance, the output of

Thanks a lot! After reading http://php.net/manual/en/function.date.php again I have to admit that your are completely right. 'S' is indeed not meant for months but for days.

Also looks like in PHP 5.3.0+ there is support for formatting an ordinal. So maybe should use that with a reasonable fall back for old versions of PHP?

It seems that the class NumberFormatter needs PHP 5 >= 5.3.0 and PECL intl >= 1.0.0 ... I'm sure that discussions about these things will be very interesting in the near future.

Anyway, I worked on a new solution today. ;) I think that WordPress should at least provide the functionality for the English ordinal numbers but should also come with a filter so that a developer could extend it easily. In German the rules are very different for example: they append the suffix 'te' to numbers from 0 to 19 and 'ste' from 20 and above.

@realloc
4 years ago

ordinal suffix for days

#8 follow-up: @gibrown
4 years ago

Feels like it would be good to have test cases in different languages for this to prove out that it works across multiple languages. Looking at the code it is not obvious to me that it would work for how you describe German for instance.

@realloc
4 years ago

Patch and Unit tests with tests for filter

#9 in reply to: ↑ 8 @realloc
4 years ago

Replying to gibrown:

Feels like it would be good to have test cases in different languages for this to prove out that it works across multiple languages.

I just added 2 filter examples to illustrate the idea. Any multilingual plugin could so implement the necessary functionality.

#10 @ocean90
3 years ago

  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

This ticket was mentioned in Slack in #core-i18n by ocean90. View the logs.


3 years ago

#12 @ocean90
3 years ago

  • Milestone changed from Awaiting Review to 4.6
  • Owner changed from SergeyBiryukov to ocean90

#13 @ocean90
3 years ago

  • Keywords dev-feedback needs-testing removed
  • Milestone changed from 4.6 to Future Release
  • Owner ocean90 deleted

I don't like the filter approach because it doesn't provide out of the box support for any other locale. As you've already noticed Russian doesn't have a specific identifier for ordinals and an empty translations is not supported.

I'd like to see an approach which follows the Plural-Forms entry in a .po file. It's "Plural-Forms: nplurals=2; plural=n != 1;\n" for German or "Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n" for Russian.
You'll see similar rules in the Unicode CLDR database: http://www.unicode.org/cldr/charts/29/supplemental/language_plural_rules.html.

An example for English could be "Ordinal-Forms: nplurals=4; plural=(n % 10 = 1 && n % 100 != 11) ? 'st' : (n % 10 = 3 && n % 100 != 13) ? 'nd' : (n % 10 = 3 && n % 100 != 13) ? 'rd' : 'th';".

That's something which we can be stored into the translation file and parsed by a WP_Locale::get_ordinal_suffix() method.

This ticket was mentioned in Slack in #core by johnbillion. View the logs.


3 years ago

#15 @tobifjellner
3 years ago

What if we simply drop back the whole solution to something that already works for everyone?, by simply using gettext an already normal translations tools?.
We could add 31 strings to translate, with proper context to make sure that they're understood and correctly translated.

_x( 'st', 'Ordinal suffix for day 1 in month, "-" for empty', 'my-text-domain'
_x( 'nd', 'Ordinal suffix for day 2 in month, "-" for empty', 'my-text-domain'
_x( 'rd', 'Ordinal suffix for day 3 in month, "-" for empty', 'my-text-domain'
_x( 'th', 'Ordinal suffix for day 4 in month, "-" for empty', 'my-text-domain'
_x( 'th', 'Ordinal suffix for day 5 in month, "-" for empty', 'my-text-domain'
_x( 'st', 'Ordinal suffix for day 31 in month, "-" for empty', 'my-text-domain'

...
And then wrap it up in a simple function?

Last edited 3 years ago by tobifjellner (previous) (diff)
Note: See TracTickets for help on using tickets.