1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Include relative time polyfill locale data (#8896)

We were including the polyfill for the `Intl.RelativeTimeFormat` API,
but we weren't including any locale data. This polyfill doesn't work
without the locale data for whichever locale you're formatting.

The data for all locales we support is now included. The locale data
is loaded from disk as-needed (during app startup, and upon each change
in locale).
This commit is contained in:
Mark Stacey 2020-07-02 21:34:48 -03:00 committed by GitHub
parent d3aa9f8620
commit f5d4ab1cc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 3 deletions

View File

@ -3,6 +3,8 @@ const path = require('path')
const watch = require('gulp-watch') const watch = require('gulp-watch')
const glob = require('fast-glob') const glob = require('fast-glob')
const locales = require('../../app/_locales/index.json')
const { createTask, composeSeries } = require('./task') const { createTask, composeSeries } = require('./task')
module.exports = createStaticAssetTasks module.exports = createStaticAssetTasks
@ -45,6 +47,20 @@ const copyTargets = [
}, },
] ]
const languageTags = new Set()
for (const locale of locales) {
const { code } = locale
const tag = code.split('_')[0]
languageTags.add(tag)
}
for (const tag of languageTags) {
copyTargets.push({
src: `./node_modules/@formatjs/intl-relativetimeformat/dist/locale-data/${tag}.json`,
dest: `intl/${tag}/relative-time-format-data.json`,
})
}
const copyTargetsDev = [ const copyTargetsDev = [
...copyTargets, ...copyTargets,
{ {

View File

@ -79,3 +79,21 @@ export async function fetchLocale (localeCode) {
} }
} }
const relativeTimeFormatLocaleData = new Set()
export async function loadRelativeTimeFormatLocaleData (localeCode) {
const languageTag = localeCode.split('_')[0]
if (
Intl.RelativeTimeFormat &&
typeof Intl.RelativeTimeFormat.__addLocaleData === 'function' &&
!relativeTimeFormatLocaleData.has(languageTag)
) {
const localeData = await fetchRelativeTimeFormatData(languageTag)
Intl.RelativeTimeFormat.__addLocaleData(localeData)
}
}
async function fetchRelativeTimeFormatData (languageTag) {
const response = await window.fetch(`./intl/${languageTag}/relative-time-format-data.json`)
return await response.json()
}

View File

@ -4,7 +4,7 @@ import getBuyEthUrl from '../../../app/scripts/lib/buy-eth-url'
import { checksumAddress } from '../helpers/utils/util' import { checksumAddress } from '../helpers/utils/util'
import { calcTokenBalance, estimateGas } from '../pages/send/send.utils' import { calcTokenBalance, estimateGas } from '../pages/send/send.utils'
import ethUtil from 'ethereumjs-util' import ethUtil from 'ethereumjs-util'
import { fetchLocale } from '../helpers/utils/i18n-helper' import { fetchLocale, loadRelativeTimeFormatLocaleData } from '../helpers/utils/i18n-helper'
import { getMethodDataAsync } from '../helpers/utils/transactions.util' import { getMethodDataAsync } from '../helpers/utils/transactions.util'
import { fetchSymbolAndDecimals } from '../helpers/utils/token-util' import { fetchSymbolAndDecimals } from '../helpers/utils/token-util'
import switchDirection from '../helpers/utils/switch-direction' import switchDirection from '../helpers/utils/switch-direction'
@ -2012,8 +2012,9 @@ export function setIpfsGateway (val) {
} }
export function updateCurrentLocale (key) { export function updateCurrentLocale (key) {
return (dispatch) => { return async (dispatch) => {
dispatch(showLoadingIndication()) dispatch(showLoadingIndication())
await loadRelativeTimeFormatLocaleData(key)
return fetchLocale(key) return fetchLocale(key)
.then((localeMessages) => { .then((localeMessages) => {
log.debug(`background.setCurrentLocale`) log.debug(`background.setCurrentLocale`)

View File

@ -10,7 +10,7 @@ import txHelper from './lib/tx-helper'
import { getEnvironmentType } from '../app/scripts/lib/util' import { getEnvironmentType } from '../app/scripts/lib/util'
import { ALERT_TYPES } from '../app/scripts/controllers/alert' import { ALERT_TYPES } from '../app/scripts/controllers/alert'
import { ENVIRONMENT_TYPE_POPUP } from '../app/scripts/lib/enums' import { ENVIRONMENT_TYPE_POPUP } from '../app/scripts/lib/enums'
import { fetchLocale } from './app/helpers/utils/i18n-helper' import { fetchLocale, loadRelativeTimeFormatLocaleData } from './app/helpers/utils/i18n-helper'
import switchDirection from './app/helpers/utils/switch-direction' import switchDirection from './app/helpers/utils/switch-direction'
import { getPermittedAccountsForCurrentTab, getSelectedAddress } from './app/selectors' import { getPermittedAccountsForCurrentTab, getSelectedAddress } from './app/selectors'
import { ALERT_STATE } from './app/ducks/alerts/unconnected-account' import { ALERT_STATE } from './app/ducks/alerts/unconnected-account'
@ -48,6 +48,11 @@ async function startApp (metamaskState, backgroundConnection, opts) {
: {} : {}
const enLocaleMessages = await fetchLocale('en') const enLocaleMessages = await fetchLocale('en')
await loadRelativeTimeFormatLocaleData('en')
if (metamaskState.currentLocale) {
await loadRelativeTimeFormatLocaleData(metamaskState.currentLocale)
}
if (metamaskState.textDirection === 'rtl') { if (metamaskState.textDirection === 'rtl') {
await switchDirection('rtl') await switchDirection('rtl')
} }