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

Merge pull request #4766 from whymarrh/null-translations

Rework i18n-helper getMessage function
This commit is contained in:
Whymarrh Whitby 2018-07-12 15:46:09 -02:30 committed by GitHub
commit 47d596c090
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 41 deletions

View File

@ -1,6 +1,6 @@
const { Component } = require('react')
const { connect } = require('react-redux')
const PropTypes = require('prop-types')
const connect = require('../../metamask-connect')
const { Redirect, withRouter } = require('react-router-dom')
const { compose } = require('recompose')
const h = require('react-hyperscript')

View File

@ -8,8 +8,11 @@ const t = require('../i18n-helper').getMessage
class I18nProvider extends Component {
getChildContext () {
const { localeMessages } = this.props
const { current, en } = localeMessages
return {
t: t.bind(null, localeMessages),
t (key, ...args) {
return t(current, key, ...args) || t(en, key, ...args) || `[${key}]`
},
}
}

View File

@ -1,27 +0,0 @@
const connect = require('react-redux').connect
const t = require('../i18n-helper').getMessage
const metamaskConnect = (mapStateToProps, mapDispatchToProps) => {
return connect(
_higherOrderMapStateToProps(mapStateToProps),
mapDispatchToProps
)
}
const _higherOrderMapStateToProps = (mapStateToProps) => {
let _t
let currentLocale
return (state, ownProps = {}) => {
const stateProps = mapStateToProps
? mapStateToProps(state, ownProps)
: ownProps
if (currentLocale !== state.metamask.currentLocale) {
currentLocale = state.metamask.currentLocale
_t = t.bind(null, state.localeMessages)
}
stateProps.t = _t
return stateProps
}
}
module.exports = metamaskConnect

View File

@ -1,20 +1,22 @@
// cross-browser connection to extension i18n API
const log = require('loglevel')
/**
* Returns a localized message for the given key
* @param {object} locale The locale
* @param {string} key The message key
* @param {string[]} substitutions A list of message substitution replacements
* @return {null|string} The localized message
*/
const getMessage = (locale, key, substitutions) => {
// check locale is loaded
if (!locale) {
// throw new Error('Translator - has not loaded a locale yet.')
return ''
return null
}
// check entry is present
const { current, en } = locale
const entry = current[key] || en[key]
if (!entry) {
// throw new Error(`Translator - Unable to find value for "${key}"`)
log.error(`Translator - Unable to find value for "${key}"`)
return `[${key}]`
if (!locale[key]) {
log.error(`Translator - Unable to find value for key "${key}"`)
return null
}
const entry = locale[key]
let phrase = entry.message
// perform substitutions
if (substitutions && substitutions.length) {
@ -29,8 +31,7 @@ const getMessage = (locale, key, substitutions) => {
async function fetchLocale (localeName) {
try {
const response = await fetch(`./_locales/${localeName}/messages.json`)
const locale = await response.json()
return locale
return await response.json()
} catch (error) {
log.error(`failed to fetch ${localeName} locale because of ${error}`)
return {}