|
|
import debugFactory from 'debug'; |
|
|
import i18n from 'i18n-calypso'; |
|
|
import moment, { LongDateFormatKey } from 'moment'; |
|
|
const debug = debugFactory( 'apps:odyssey' ); |
|
|
|
|
|
const DEFAULT_LANGUAGE = 'en'; |
|
|
const DEFAULT_MOMENT_LOCALE = 'en'; |
|
|
|
|
|
const SIMPLE_SITE_DEFAULT_LOCALE = 'en'; |
|
|
const ALWAYS_LOAD_WITH_LOCALE = [ 'pt', 'zh' ]; |
|
|
|
|
|
|
|
|
|
|
|
export const phpToMomentMapping = { |
|
|
d: 'DD', |
|
|
D: 'ddd', |
|
|
j: 'D', |
|
|
l: 'dddd', |
|
|
N: 'E', |
|
|
|
|
|
|
|
|
w: 'd', |
|
|
|
|
|
|
|
|
W: 'W', |
|
|
F: 'MMMM', |
|
|
m: 'MM', |
|
|
M: 'MMM', |
|
|
n: 'M', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
o: 'Y', |
|
|
Y: 'YYYY', |
|
|
y: 'YY', |
|
|
a: 'a', |
|
|
A: 'A', |
|
|
|
|
|
|
|
|
g: 'h', |
|
|
G: 'H', |
|
|
h: 'hh', |
|
|
H: 'HH', |
|
|
i: 'mm', |
|
|
s: 'ss', |
|
|
u: 'SSSSSS', |
|
|
v: 'SSS', |
|
|
e: 'z', |
|
|
|
|
|
|
|
|
O: 'ZZ', |
|
|
P: 'Z', |
|
|
|
|
|
T: 'z', |
|
|
|
|
|
|
|
|
c: 'YYYY-MM-DDTHH:mm-ssZ', |
|
|
r: 'ddd, DD MMM YYYY HH:mm:ss ZZ', |
|
|
U: 'X', |
|
|
}; |
|
|
|
|
|
const getLanguageCodeFromLocale = ( localeSlug: string ) => { |
|
|
debug( localeSlug ); |
|
|
if ( localeSlug.indexOf( '-' ) > -1 ) { |
|
|
return localeSlug.split( '-' )[ 0 ]; |
|
|
} |
|
|
return localeSlug; |
|
|
}; |
|
|
|
|
|
const convertPhpToMomentFormat = ( phpFormat: string ): string => { |
|
|
const phpToMomentMap = phpToMomentMapping as { |
|
|
[ key: string ]: string; |
|
|
}; |
|
|
|
|
|
let momentFormat = ''; |
|
|
|
|
|
for ( let i = 0; i < phpFormat.length; i++ ) { |
|
|
const char = phpFormat.charAt( i ); |
|
|
if ( phpToMomentMap[ char ] ) { |
|
|
momentFormat += phpToMomentMap[ char ]; |
|
|
} else { |
|
|
momentFormat += char; |
|
|
} |
|
|
} |
|
|
|
|
|
return momentFormat; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const fixLongDateFormatForEn = ( localeSlug: string ) => { |
|
|
if ( localeSlug === SIMPLE_SITE_DEFAULT_LOCALE ) { |
|
|
const keysReplacedByWordPressCore: LongDateFormatKey[] = [ |
|
|
'LTS', |
|
|
'LT', |
|
|
'L', |
|
|
'LL', |
|
|
'LLL', |
|
|
'LLLL', |
|
|
]; |
|
|
const currentLocaleData = moment.localeData(); |
|
|
const momentLongDateFormat: Partial< { [ key in LongDateFormatKey ]: string } > = {}; |
|
|
keysReplacedByWordPressCore.forEach( ( key ) => { |
|
|
if ( currentLocaleData.longDateFormat( key ) ) { |
|
|
momentLongDateFormat[ key ] = convertPhpToMomentFormat( |
|
|
currentLocaleData.longDateFormat( key ) |
|
|
); |
|
|
} |
|
|
} ); |
|
|
moment.updateLocale( localeSlug, { |
|
|
longDateFormat: momentLongDateFormat as { [ key in LongDateFormatKey ]: string }, |
|
|
} ); |
|
|
} |
|
|
}; |
|
|
|
|
|
const loadMomentLocale = async ( localeSlug: string, languageCode: string, isSimple: boolean ) => { |
|
|
return import( `moment/locale/${ localeSlug }` ) |
|
|
.catch( ( error: Error ) => { |
|
|
debug( |
|
|
`Encountered an error loading moment locale file for ${ localeSlug }. Falling back to language datetime format.`, |
|
|
error |
|
|
); |
|
|
|
|
|
if ( localeSlug !== languageCode ) { |
|
|
localeSlug = languageCode; |
|
|
return import( `moment/locale/${ localeSlug }` ); |
|
|
} |
|
|
|
|
|
return Promise.reject( error ); |
|
|
} ) |
|
|
.catch( ( error: Error ) => { |
|
|
debug( |
|
|
`Encountered an error loading moment locale file for ${ localeSlug }. Falling back to US datetime format.`, |
|
|
error |
|
|
); |
|
|
|
|
|
|
|
|
isSimple && fixLongDateFormatForEn( localeSlug ); |
|
|
localeSlug = DEFAULT_MOMENT_LOCALE; |
|
|
} ) |
|
|
.then( () => moment.locale( localeSlug ) ); |
|
|
}; |
|
|
|
|
|
const loadLanguageFile = ( languageFileName: string ) => { |
|
|
const url = `https://widgets.wp.com/odyssey-stats/v1/languages/${ languageFileName }-v1.1.json`; |
|
|
|
|
|
return globalThis.fetch( url ).then( ( response ) => { |
|
|
if ( response.ok ) { |
|
|
return response.json().then( ( body ) => { |
|
|
if ( body ) { |
|
|
i18n.setLocale( body ); |
|
|
} |
|
|
} ); |
|
|
} |
|
|
return Promise.reject( response ); |
|
|
} ); |
|
|
}; |
|
|
|
|
|
export default ( localeSlug: string, isSimple = false ) => { |
|
|
const languageCode = getLanguageCodeFromLocale( localeSlug ); |
|
|
|
|
|
|
|
|
if ( languageCode !== DEFAULT_LANGUAGE ) { |
|
|
const languageFileName = ALWAYS_LOAD_WITH_LOCALE.includes( languageCode ) |
|
|
? localeSlug |
|
|
: languageCode; |
|
|
|
|
|
loadLanguageFile( languageFileName ) |
|
|
.then( () => debug( `Loaded locale files for ${ languageCode } successfully.` ) ) |
|
|
.catch( ( error ) => |
|
|
debug( |
|
|
`Encountered an error loading locale file for ${ languageCode }. Falling back to English.`, |
|
|
error |
|
|
) |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return loadMomentLocale( localeSlug, languageCode, isSimple ); |
|
|
}; |
|
|
|