-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 41 additions & 35 deletions
76
packages/koot/i18n/server/generate-html-redirect-metas.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,80 @@ | ||
import { changeLocaleQueryKey } from '../../defaults/defines' | ||
import availableLocaleIds from '../locale-ids' | ||
import isI18nEnabled from '../../i18n/is-enabled' | ||
import { changeLocaleQueryKey } from '../../defaults/defines'; | ||
import availableLocaleIds from '../locale-ids'; | ||
import isI18nEnabled from '../../i18n/is-enabled'; | ||
|
||
/** | ||
* 生成用以声明该页面其他语种 URL 的 meta 标签的 HTML 代码 | ||
* @param {Object} options | ||
* @param {Object} options.ctx | ||
* @param {Object} options | ||
* @param {Object} options.ctx | ||
* @param {Object} options.proxyRequestOrigin Koot 配置: server.proxyRequestOrigin | ||
* @param {String} options.localeId 当前语种 | ||
* @returns {String} HTML 代码 | ||
*/ | ||
const generateHtmlRedirectMetas = ({ ctx, localeId/*, proxyRequestOrigin*/ }) => { | ||
if (!isI18nEnabled()) | ||
return '' | ||
const generateHtmlRedirectMetas = ({ | ||
ctx, | ||
localeId /*, proxyRequestOrigin*/, | ||
}) => { | ||
if (!isI18nEnabled()) return ''; | ||
|
||
// let { href, origin } = ctx | ||
// if (typeof proxyRequestOrigin.protocol === 'string') { | ||
// origin = origin.replace(/^http:\/\//, `${proxyRequestOrigin.protocol}://`) | ||
// href = href.replace(/^http:\/\//, `${proxyRequestOrigin.protocol}://`) | ||
// } | ||
const { | ||
hrefTrue: href, | ||
originTrue: origin | ||
} = ctx | ||
const { hrefTrue: href, originTrue: origin } = ctx; | ||
|
||
const isUseRouter = process.env.KOOT_I18N_URL_USE === 'router' | ||
const isUseRouter = process.env.KOOT_I18N_URL_USE === 'router'; | ||
|
||
let html = availableLocaleIds//getLocaleIds() | ||
.filter(thisLocaleId => thisLocaleId !== localeId) | ||
.map(l => { | ||
|
||
let thisHref = '' | ||
let html = availableLocaleIds //getLocaleIds() | ||
.filter((thisLocaleId) => thisLocaleId !== localeId) | ||
.map((l) => { | ||
let thisHref = ''; | ||
|
||
if (isUseRouter) { | ||
thisHref = origin | ||
+ href | ||
thisHref = | ||
origin + | ||
href | ||
.replace(new RegExp(`^${origin}`), '') | ||
.replace(new RegExp(`^${localeId}`), l) | ||
.replace(new RegExp(`^/${localeId}`), '/' + l) | ||
.replace(new RegExp(`^/${localeId}`), '/' + l); | ||
} else { | ||
thisHref = (() => { | ||
if (ctx.query[changeLocaleQueryKey] === '') { | ||
return href.replace( | ||
new RegExp(`${changeLocaleQueryKey}=`), | ||
`${changeLocaleQueryKey}=${l}` | ||
) | ||
); | ||
} | ||
if (typeof ctx.query[changeLocaleQueryKey] === 'string') | ||
return href.replace( | ||
new RegExp(`${changeLocaleQueryKey}=[a-zA-Z-_]+`), | ||
`${changeLocaleQueryKey}=${l}` | ||
) | ||
return href + (ctx.querystring ? `&` : ( | ||
href.substr(href.length - 1) === '?' | ||
); | ||
return ( | ||
href + | ||
(ctx.querystring | ||
? `&` | ||
: href.substr(href.length - 1) === '?' | ||
? '' | ||
: `?` | ||
)) + `${changeLocaleQueryKey}=${l}` | ||
})() | ||
: `?`) + | ||
`${changeLocaleQueryKey}=${l}` | ||
); | ||
})(); | ||
} | ||
|
||
if (__DEV__) thisHref = thisHref.replace('://localhost', '://127.0.0.1') | ||
return `<link rel="alternate" hreflang="${l}" href="${thisHref}" />` | ||
if (__DEV__) | ||
thisHref = thisHref | ||
.replace('://localhost', '://127.0.0.1') | ||
.replace(/^https:\/\//, 'http://'); | ||
return `<link rel="alternate" hreflang="${l}" href="${thisHref}" />`; | ||
}) | ||
.join('') | ||
.join(''); | ||
|
||
if (isUseRouter) { | ||
html += `<base href="/${localeId}">` | ||
html += `<base href="/${localeId}">`; | ||
} | ||
|
||
return html | ||
} | ||
return html; | ||
}; | ||
|
||
export default generateHtmlRedirectMetas | ||
export default generateHtmlRedirectMetas; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,48 @@ | ||
import availableLocaleIds from '../locale-ids' | ||
import getLangFromCtx from './get-lang-from-ctx' | ||
import setCookie from '../set-cookie' | ||
import isI18nEnabled from '../is-enabled' | ||
import availableLocaleIds from '../locale-ids'; | ||
import getLangFromCtx from './get-lang-from-ctx'; | ||
import setCookie from '../set-cookie'; | ||
import isI18nEnabled from '../is-enabled'; | ||
|
||
/** | ||
* URL 使用 router 方式时,在同构中间件流程的匹配 react 路由之前,检查是否需要跳转 | ||
* 如果需要跳转,此时发送跳转请求 | ||
* @param {Object} ctx | ||
* @param {Object} ctx | ||
* @returns {Boolean} 是否进行跳转 | ||
*/ | ||
const useRouterRedirect = (ctx) => { | ||
if (!isI18nEnabled()) | ||
return false | ||
if (!isI18nEnabled()) return false; | ||
|
||
if (process.env.KOOT_I18N_URL_USE !== 'router') | ||
return false | ||
if (process.env.KOOT_I18N_URL_USE !== 'router') return false; | ||
|
||
let pathname = ctx.path | ||
if (pathname.substr(0, 1) === '/') pathname = pathname.substr(1) | ||
pathname = pathname.split('/') | ||
let pathname = ctx.path; | ||
if (pathname.substr(0, 1) === '/') pathname = pathname.substr(1); | ||
pathname = pathname.split('/'); | ||
|
||
if (!availableLocaleIds.includes(pathname[0])) { | ||
const localeId = getLangFromCtx(ctx) | ||
const localeId = getLangFromCtx(ctx); | ||
|
||
// console.log('lang', lang) | ||
// console.log('pathname', pathname) | ||
|
||
pathname.unshift(localeId) | ||
pathname = '/' + pathname.join('/') | ||
pathname.unshift(localeId); | ||
pathname = '/' + pathname.join('/'); | ||
|
||
// 生成跳转后的地址 | ||
const newpath = ctx.originTrue | ||
+ ctx.hrefTrue | ||
let newpath = | ||
ctx.originTrue + | ||
ctx.hrefTrue | ||
.replace(new RegExp(`^${ctx.originTrue}`), '') | ||
.replace(new RegExp(`^${ctx.path}`), pathname) | ||
.replace(new RegExp(`^${ctx.path}`), pathname); | ||
|
||
if (process.env.WEBPACK_BUILD_ENV === 'dev') | ||
newpath = newpath.replace(/^https:\/\//, 'http://'); | ||
|
||
// console.log('newpath', newpath) | ||
setCookie(localeId, ctx) | ||
return ctx.redirect(newpath) | ||
setCookie(localeId, ctx); | ||
return ctx.redirect(newpath); | ||
} | ||
|
||
return false | ||
} | ||
return false; | ||
}; | ||
|
||
export default useRouterRedirect | ||
export default useRouterRedirect; |