summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/moment-locale-overrides.js
blob: bafb86a227953ed49b8860f9121cd050e223e10f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* global mediaWiki, moment */

( function ( mw ) {
	// HACK: Overwrite moment's i18n with MediaWiki's for the current language so that
	// wgTranslateNumerals is respected.
	moment.updateLocale( 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( 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;
		}
	} );
}( mediaWiki ) );