Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include relative time polyfill locale data #8896

Merged
merged 1 commit into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions development/build/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const path = require('path')
const watch = require('gulp-watch')
const glob = require('fast-glob')

const locales = require('../../app/_locales/index.json')

const { createTask, composeSeries } = require('./task')

module.exports = createStaticAssetTasks
Expand Down Expand Up @@ -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 = [
...copyTargets,
{
Expand Down
18 changes: 18 additions & 0 deletions ui/app/helpers/utils/i18n-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
5 changes: 3 additions & 2 deletions ui/app/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import getBuyEthUrl from '../../../app/scripts/lib/buy-eth-url'
import { checksumAddress } from '../helpers/utils/util'
import { calcTokenBalance, estimateGas } from '../pages/send/send.utils'
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 { fetchSymbolAndDecimals } from '../helpers/utils/token-util'
import switchDirection from '../helpers/utils/switch-direction'
Expand Down Expand Up @@ -2012,8 +2012,9 @@ export function setIpfsGateway (val) {
}

export function updateCurrentLocale (key) {
return (dispatch) => {
return async (dispatch) => {
dispatch(showLoadingIndication())
await loadRelativeTimeFormatLocaleData(key)
return fetchLocale(key)
.then((localeMessages) => {
log.debug(`background.setCurrentLocale`)
Expand Down
7 changes: 6 additions & 1 deletion ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import txHelper from './lib/tx-helper'
import { getEnvironmentType } from '../app/scripts/lib/util'
import { ALERT_TYPES } from '../app/scripts/controllers/alert'
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 { getPermittedAccountsForCurrentTab, getSelectedAddress } from './app/selectors'
import { ALERT_STATE } from './app/ducks/alerts/unconnected-account'
Expand Down Expand Up @@ -48,6 +48,11 @@ async function startApp (metamaskState, backgroundConnection, opts) {
: {}
const enLocaleMessages = await fetchLocale('en')

await loadRelativeTimeFormatLocaleData('en')
if (metamaskState.currentLocale) {
await loadRelativeTimeFormatLocaleData(metamaskState.currentLocale)
}

if (metamaskState.textDirection === 'rtl') {
await switchDirection('rtl')
}
Expand Down