Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CoCo_Code_Op2
next.js
提交
67b67b28
N
next.js
项目概览
CoCo_Code_Op2
/
next.js
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
next.js
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
67b67b28
编写于
9月 16, 2020
作者:
L
Long Ho
提交者:
GitHub
9月 16, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(examples/with-react-intl): add locale negotation to client side (#16806)
fix #16752 cc @thuringia
上级
4ba768e2
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
91 addition
and
24 deletion
+91
-24
examples/with-react-intl/lang/en-GB.json
examples/with-react-intl/lang/en-GB.json
+7
-0
examples/with-react-intl/lang/zh-Hans-CN.json
examples/with-react-intl/lang/zh-Hans-CN.json
+7
-0
examples/with-react-intl/lang/zh-Hant-HK.json
examples/with-react-intl/lang/zh-Hant-HK.json
+7
-0
examples/with-react-intl/package.json
examples/with-react-intl/package.json
+3
-1
examples/with-react-intl/pages/_app.tsx
examples/with-react-intl/pages/_app.tsx
+51
-15
examples/with-react-intl/pages/_document.tsx
examples/with-react-intl/pages/_document.tsx
+16
-8
未找到文件。
examples/with-react-intl/lang/en-GB.json
0 → 100644
浏览文件 @
67b67b28
{
"AvQcw8"
:
"React Intl Next.js Example, in en-GB"
,
"N015Sp"
:
"Hello, World, from the UK!"
,
"ejEGdx"
:
"Home (British version)"
,
"fnfXnF"
:
"An example app integrating React Intl with Next.js"
,
"g5pX+a"
:
"About (British version)"
}
examples/with-react-intl/lang/zh-Hans-CN.json
0 → 100644
浏览文件 @
67b67b28
{
"AvQcw8"
:
" React Intl Next.js示例"
,
"N015Sp"
:
"你好,世界!"
,
"ejEGdx"
:
"首页"
,
"fnfXnF"
:
"一个将React Intl与Next.js集成的示例应用程序"
,
"g5pX+a"
:
"关于"
}
examples/with-react-intl/lang/zh-Hant-HK.json
0 → 100644
浏览文件 @
67b67b28
{
"AvQcw8"
:
"React Intl Next.js示例"
,
"N015Sp"
:
"你好,世界!"
,
"ejEGdx"
:
"首頁"
,
"fnfXnF"
:
"一個將React Intl與Next.js集成的示例應用程序"
,
"g5pX+a"
:
"關於"
}
examples/with-react-intl/package.json
浏览文件 @
67b67b28
...
...
@@ -3,10 +3,12 @@
"version"
:
"1.0.0"
,
"scripts"
:
{
"dev"
:
"cross-env NODE_ICU_DATA=node_modules/full-icu ts-node --project tsconfig.server.json server.ts"
,
"dev-no-custom-server"
:
"next dev"
,
"build"
:
"npm run extract:i18n && npm run compile:i18n && next build && tsc -p tsconfig.server.json"
,
"extract:i18n"
:
"formatjs extract '{pages,components}/*.{js,ts,tsx}' --format simple --id-interpolation-pattern '[sha512:contenthash:base64:6]' --out-file lang/en.json"
,
"compile:i18n"
:
"formatjs compile-folder --ast --format simple lang/ compiled-lang/"
,
"start"
:
"cross-env NODE_ENV=production NODE_ICU_DATA=node_modules/full-icu node dist/server"
"start"
:
"cross-env NODE_ENV=production NODE_ICU_DATA=node_modules/full-icu node dist/server"
,
"start-no-custom-server"
:
"next start"
},
"dependencies"
:
{
"@formatjs/cli"
:
"^2.7.3"
,
...
...
examples/with-react-intl/pages/_app.tsx
浏览文件 @
67b67b28
...
...
@@ -5,37 +5,73 @@ import App from 'next/app';
function
MyApp
({
Component
,
pageProps
,
locale
,
messages
})
{
return
(
<
IntlProvider
locale
=
{
locale
}
messages
=
{
messages
}
>
<
IntlProvider
locale
=
{
locale
}
defaultLocale
=
"en"
messages
=
{
messages
}
>
<
Component
{
...
pageProps
}
/>
</
IntlProvider
>
);
}
// We need to load and expose the translations on the request for the user's
// locale. These will only be used in production, in dev the `defaultMessage` in
// each message description in the source code will be used.
const
getMessages
=
(
locale
:
string
=
'
en
'
)
=>
{
switch
(
locale
)
{
default
:
return
import
(
'
../compiled-lang/en.json
'
);
case
'
fr
'
:
return
import
(
'
../compiled-lang/fr.json
'
);
/**
* Get the messages and also do locale negotiation. A multi-lingual user
* can specify locale prefs like ['ja', 'en-GB', 'en'] which is interpreted as
* Japanese, then British English, then English
* @param locales list of requested locales
* @returns {[string, Promise]} A tuple containing the negotiated locale
* and the promise of fetching the translated messages
*/
function
getMessages
(
locales
:
string
|
string
[]
=
[
'
en
'
])
{
if
(
!
Array
.
isArray
(
locales
))
{
locales
=
[
locales
];
}
};
let
langBundle
;
let
locale
;
for
(
let
i
=
0
;
i
<
locales
.
length
&&
!
locale
;
i
++
)
{
locale
=
locales
[
i
];
switch
(
locale
)
{
case
'
fr
'
:
langBundle
=
import
(
'
../compiled-lang/fr.json
'
);
break
;
case
'
en-GB
'
:
langBundle
=
import
(
'
../compiled-lang/en-GB.json
'
);
break
;
case
'
zh-Hans-CN
'
:
langBundle
=
import
(
'
../compiled-lang/zh-Hans-CN.json
'
);
break
;
case
'
zh-Hant-HK
'
:
langBundle
=
import
(
'
../compiled-lang/zh-Hant-HK.json
'
);
break
;
default
:
break
;
// Add more languages
}
}
if
(
!
langBundle
)
{
return
[
'
en
'
,
import
(
'
../compiled-lang/en.json
'
)];
}
return
[
locale
,
langBundle
];
}
const
getInitialProps
:
typeof
App
.
getInitialProps
=
async
appContext
=>
{
const
{
ctx
:
{
req
},
}
=
appContext
;
const
locale
=
(
req
as
any
)?.
locale
||
(
window
as
any
).
LOCALE
||
'
en
'
;
const
requestedLocales
:
string
|
string
[]
=
(
req
as
any
)?.
locale
||
(
typeof
navigator
!==
'
undefined
'
&&
navigator
.
languages
)
||
// IE11
(
typeof
navigator
!==
'
undefined
'
&&
(
navigator
as
any
).
userLanguage
)
||
(
typeof
window
!==
'
undefined
'
&&
(
window
as
any
).
LOCALE
)
||
'
en
'
;
const
[
supportedLocale
,
messagePromise
]
=
getMessages
(
requestedLocales
);
const
[
appProps
,
messages
]
=
await
Promise
.
all
([
polyfill
(
l
ocale
),
getMessages
(
locale
)
,
polyfill
(
supportedL
ocale
),
messagePromise
,
App
.
getInitialProps
(
appContext
),
]);
return
{...(
appProps
as
any
),
locale
,
messages
};
return
{...(
appProps
as
any
),
locale
:
supportedLocale
,
messages
};
};
MyApp
.
getInitialProps
=
getInitialProps
;
...
...
examples/with-react-intl/pages/_document.tsx
浏览文件 @
67b67b28
...
...
@@ -16,25 +16,33 @@ class MyDocument extends Document<Props> {
static
async
getInitialProps
(
ctx
:
DocumentContext
)
{
const
{
req
}
=
ctx
;
const
initialProps
=
await
Document
.
getInitialProps
(
ctx
);
const
locale
=
(
req
as
any
).
locale
;
return
{
...
initialProps
,
locale
:
(
req
as
any
).
locale
||
'
en
'
,
lang
:
((
req
as
any
).
locale
||
'
en
'
).
split
(
'
-
'
)[
0
]
,
locale
,
lang
:
locale
?
locale
.
split
(
'
-
'
)[
0
]
:
undefined
,
nonce
:
(
req
as
any
).
nonce
,
};
}
render
()
{
let
scriptEl
;
if
(
this
.
props
.
locale
)
{
scriptEl
=
(
<
script
nonce
=
{
this
.
props
.
nonce
}
dangerouslySetInnerHTML
=
{
{
__html
:
`window.LOCALE="
${
this
.
props
.
locale
}
"`
,
}
}
></
script
>
);
}
return
(
<
Html
lang
=
{
this
.
props
.
lang
}
>
<
Head
/>
<
body
>
<
script
nonce
=
{
this
.
props
.
nonce
}
dangerouslySetInnerHTML
=
{
{
__html
:
`window.LOCALE="
${
this
.
props
.
locale
}
"`
,
}
}
></
script
>
{
scriptEl
}
<
Main
/>
<
NextScript
/>
</
body
>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录