diff --git a/.umirc.js b/.umirc.js index 05a0fab..999104d 100644 --- a/.umirc.js +++ b/.umirc.js @@ -60,9 +60,9 @@ export default { }, pwa: { manifestOptions: { - srcPath: 'manifest.json' + srcPath: 'manifest.json', }, - } + }, }, ], ], diff --git a/src/layouts/index.js b/src/layouts/index.js index 55ea725..8c40b0e 100644 --- a/src/layouts/index.js +++ b/src/layouts/index.js @@ -26,7 +26,7 @@ class Layout extends Component { componentDidMount() { const language = langFromPath(this.props.location.pathname) this.language = language - this.loadCatalog(language) + language && this.loadCatalog(language) } shouldComponentUpdate(nextProps, nextState) { @@ -35,7 +35,7 @@ class Layout extends Component { const { catalogs } = nextState if (preLanguage !== language && !catalogs[language]) { - this.loadCatalog(language) + language && this.loadCatalog(language) this.language = language return false } diff --git a/src/utils/index.js b/src/utils/index.js index 3bac7db..015f00c 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -10,8 +10,10 @@ export config from './config' export request from './request' export { Color } from './theme' -export const { defaultLanguage } = i18n -export const languages = i18n.languages.map(item => item.key) +// export const { defaultLanguage } = i18n +// export const languages = i18n.languages.map(item => item.key) +export const languages = i18n ? i18n.languages.map(item => item.key) : [] +export const defaultLanguage = i18n ? i18n.defaultLanguage : '' /** * Query objects that specify keys and values in an array where all values are objects. @@ -61,23 +63,32 @@ export function arrayToTree( return result } -export const langFromPath = curry( - /** - * Query language from pathname. - * @param {array} languages Specify which languages are currently available. - * @param {string} defaultLanguage Specify the default language. - * @param {string} pathname Pathname to be queried. - * @return {string} Return the queryed language. - */ - (languages, defaultLanguage, pathname) => { - for (const item of languages) { - if (pathname.startsWith(`/${item}/`)) { - return item - } +// export const langFromPath = curry( +// /** +// * Query language from pathname. +// * @param {array} languages Specify which languages are currently available. +// * @param {string} defaultLanguage Specify the default language. +// * @param {string} pathname Pathname to be queried. +// * @return {string} Return the queryed language. +// */ +// (languages, defaultLanguage, pathname) => { +// for (const item of languages) { +// if (pathname.startsWith(`/${item}/`)) { +// return item +// } +// } +// return defaultLanguage +// } +// )(languages)(defaultLanguage) + +export const langFromPath = pathname => { + for (const item of languages) { + if (pathname.startsWith(`/${item}/`)) { + return item } - return defaultLanguage } -)(languages)(defaultLanguage) + return defaultLanguage +} export const deLangPrefix = curry( /** @@ -106,11 +117,18 @@ export const deLangPrefix = curry( * @return {string} Return the pathname after adding the language prefix. */ export function addLangPrefix(pathname) { + if (!i18n) { + return pathname + } + const prefix = langFromPath(window.location.pathname) return `/${prefix}${deLangPrefix(pathname)}` } const routerAddLangPrefix = params => { + if (!i18n) { + return params + } if (isString(params)) { params = addLangPrefix(params) } else {