jenkins-bot has submitted this change and it was merged.

Change subject: Override momentjs's digit transform logic with MW's
......................................................................


Override momentjs's digit transform logic with MW's

Override the digit transform logic from momentjs's locale files
with our own logic based on MW's digit transform tables, so
we will definitely use the same transform tables, and so we
can respect $wgTranslateNumerals.

Bug: T123999
Change-Id: Icea642f41156fb637e3e4b86abeab878fd456601
---
M resources/Resources.php
D resources/src/moment-local-dmy.js
A resources/src/moment-locale-overrides.js
3 files changed, 64 insertions(+), 17 deletions(-)

Approvals:
  Jforrester: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/resources/Resources.php b/resources/Resources.php
index cb7adbe..7f48b44 100644
--- a/resources/Resources.php
+++ b/resources/Resources.php
@@ -735,7 +735,6 @@
                'scripts' => [
                        'resources/lib/moment/moment.js',
                        'resources/src/moment-global.js',
-                       'resources/src/moment-local-dmy.js',
                ],
                'languageScripts' => [
                        'af' => 'resources/lib/moment/locale/af.js',
@@ -817,6 +816,13 @@
                        'zh-hans' => 'resources/lib/moment/locale/zh-cn.js',
                        'zh-hant' => 'resources/lib/moment/locale/zh-tw.js',
                ],
+               // HACK: skinScripts come after languageScripts, and we need 
locale overrides to come
+               // after locale definitions
+               'skinScripts' => [
+                       'default' => [
+                               'resources/src/moment-locale-overrides.js',
+                       ],
+               ],
                'targets' => [ 'desktop', 'mobile' ],
        ],
 
diff --git a/resources/src/moment-local-dmy.js 
b/resources/src/moment-local-dmy.js
deleted file mode 100644
index c67b93e..0000000
--- a/resources/src/moment-local-dmy.js
+++ /dev/null
@@ -1,16 +0,0 @@
-// Use DMY date format for Moment.js, in accordance with MediaWiki's date 
formatting routines.
-// This affects English only (and languages without localisations, that fall 
back to English).
-// http://momentjs.com/docs/#/customization/long-date-formats/
-/*global moment */
-moment.locale( 'en', {
-       longDateFormat: {
-               // Unchanged, but have to be repeated here:
-               LT: 'h:mm A',
-               LTS: 'h:mm:ss A',
-               // Customized:
-               L: 'DD/MM/YYYY',
-               LL: 'D MMMM YYYY',
-               LLL: 'D MMMM YYYY LT',
-               LLLL: 'dddd, D MMMM YYYY LT'
-       }
-} );
diff --git a/resources/src/moment-locale-overrides.js 
b/resources/src/moment-locale-overrides.js
new file mode 100644
index 0000000..d49fcdc
--- /dev/null
+++ b/resources/src/moment-locale-overrides.js
@@ -0,0 +1,57 @@
+// Use DMY date format for Moment.js, in accordance with MediaWiki's date 
formatting routines.
+// This affects English only (and languages without localisations, that fall 
back to English).
+// http://momentjs.com/docs/#/customization/long-date-formats/
+/*global moment, mw */
+moment.locale( 'en', {
+       longDateFormat: {
+               // Unchanged, but have to be repeated here:
+               LT: 'h:mm A',
+               LTS: 'h:mm:ss A',
+               // Customized:
+               L: 'DD/MM/YYYY',
+               LL: 'D MMMM YYYY',
+               LLL: 'D MMMM YYYY LT',
+               LLLL: 'dddd, D MMMM YYYY LT'
+       }
+} );
+
+// HACK: Overwrite moment's i18n with MediaWiki's for the current language so 
that
+// wgTranslateNumerals is respected.
+moment.locale( moment.locale(), {
+       preparse: function ( s ) {
+               var i,
+                       table = mw.language.getDigitTransformTable();
+               if ( mw.config.get( 'wgTranslateNumerals' ) ) {
+                       for ( i = 0; i < 10; i++ ) {
+                               if ( table[ i ] !== undefined ) {
+                                       s = s.replace( new RegExp( 
mw.RegExp.escape( table[ i ] ), 'g' ), i );
+                               }
+                       }
+               }
+               // HACK: momentjs replaces commas in some languages, which is 
the only other use of preparse
+               // aside from digit transformation. We can only override 
preparse, not extend it, so we
+               // have to replicate the comma replacement functionality here.
+               if ( [ 'ar', 'ar-sa', 'fa' ].indexOf( mw.config.get( 
'wgUserLanguage' ) ) !== -1 ) {
+                       s = s.replace( /،/g, ',' );
+               }
+               return s;
+       },
+       postformat: function ( s ) {
+               var i,
+                       table = mw.language.getDigitTransformTable();
+               if ( mw.config.get( 'wgTranslateNumerals' ) ) {
+                       for ( i = 0; i < 10; i++ ) {
+                               if ( table[ i ] !== undefined ) {
+                                       s = s.replace( new RegExp( 
mw.RegExp.escape( i ), 'g' ), table[ i ] );
+                               }
+                       }
+               }
+               // HACK: momentjs replaces commas in some languages, which is 
the only other use of postformat
+               // aside from digit transformation. We can only override 
postformat, not extend it, so we
+               // have to replicate the comma replacement functionality here.
+               if ( [ 'ar', 'ar-sa', 'fa' ].indexOf( mw.config.get( 
'wgUserLanguage' ) ) !== -1 ) {
+                       s = s.replace( /,/g, '،' );
+               }
+               return s;
+       }
+} );

-- 
To view, visit https://gerrit.wikimedia.org/r/281198
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Icea642f41156fb637e3e4b86abeab878fd456601
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Catrope <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Edokter <[email protected]>
Gerrit-Reviewer: Jack Phoenix <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to