Skip to content

Commit

Permalink
✨ Support disable i18n.
Browse files Browse the repository at this point in the history
  • Loading branch information
zuiidea committed Oct 9, 2019
1 parent 9cf9737 commit b2b3fcd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .umirc.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export default {
},
pwa: {
manifestOptions: {
srcPath: 'manifest.json'
srcPath: 'manifest.json',
},
}
},
},
],
],
Expand Down
4 changes: 2 additions & 2 deletions src/layouts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down
52 changes: 35 additions & 17 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
/**
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b2b3fcd

Please sign in to comment.