Skip to content

Commit 13f99b9

Browse files
authored
Add i18n to CRA starter template (#73)
* Add i18n to CRA starter template * Comment out deliver until blueprint is ready * Add versions to packages * Template also requires @emotion/styled
1 parent 7ab197b commit 13f99b9

File tree

8 files changed

+77
-38
lines changed

8 files changed

+77
-38
lines changed

packages/react-scripts/scripts/utils/frontierInit.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function installFrontierDependencies(appPath, appName, answers, ownPath) {
4343
const usePolymer = additionalFeatures.includes('polymer');
4444
const useHF = true;
4545

46-
configureEF(appPath, ownPath);
46+
configureEF(appPath, ownPath, appName);
4747
configureHF(appPath, ownPath);
4848
// we always call this handle function. If usePolymer is false, it will remove the comments that we manually placed in the index file
4949
handlePolymerCodeAndComments(appPath, usePolymer, useHF);
@@ -58,13 +58,17 @@ function installFrontierDependencies(appPath, appName, answers, ownPath) {
5858
'fs-webdev/exo',
5959
'http-proxy-middleware@0.19.1',
6060
'@emotion/core@10.0.9',
61+
'@emotion/styled@10.0.9',
62+
'i18next@15.0.7',
63+
'react-i18next@10.5.1',
6164
]
6265
);
6366
devDepsToInstall.push(
6467
...[
6568
'@fs/eslint-config-frontier-react',
6669
'@fs/testing-library',
6770
'eslint@5.12.0',
71+
'i18next-scanner@2.10.0',
6872
'react-styleguidist@9.0.4',
6973
'webpack@4.28.3',
7074
'jest-dom@3.1.3',
@@ -74,6 +78,7 @@ function installFrontierDependencies(appPath, appName, answers, ownPath) {
7478
alterPackageJsonFile(appPath, appPackage => {
7579
const packageJson = { ...appPackage };
7680
const additionalScripts = {
81+
'locales:sync': `i18next-scanner --output src/locales 'src/**/*.js'`,
7782
styleguide: 'styleguidist server --open',
7883
'styleguide:build': 'styleguidist build',
7984
lint: 'eslint src/',
@@ -90,6 +95,8 @@ function installFrontierDependencies(appPath, appName, answers, ownPath) {
9095
installModulesSync(depsToInstall);
9196
installModulesSync(devDepsToInstall, true);
9297

98+
syncLocales()
99+
93100
replaceStringInFile(appPath, './README.md', /\{GITHUB_ORG\}\/\{GITHUB_REPO\}/g, `fs-webdev/${appName}`)
94101
}
95102

@@ -124,14 +131,15 @@ function replaceStringInFile(appPath, fileToInjectIntoPath, stringToReplace, str
124131
fs.writeFileSync(indexPath, indexCode);
125132
}
126133

127-
function configureEF(appPath, ownPath) {
134+
function configureEF(appPath, ownPath, appName) {
128135
// TODO - modify package.json to make sure name is correct for blueprint
129136
// TODO - use blueprint.yml as a template
130137

131138
const templatePath = path.join(ownPath, 'template-ef');
132139
fs.copySync(templatePath, appPath, { overwrite: true });
133140

134141
depsToInstall.push(...['express@4.16.4']);
142+
replaceStringInFile(appPath, './blueprint.yml', /\{\{APP_NAME\}\}/g, appName)
135143
}
136144

137145
function configureHF(appPath, ownPath) {
@@ -168,6 +176,10 @@ function createLocalEnvFile() {
168176
osUtils.runExternalCommandSync('npx', ['@fs/fr-cli', 'env', 'local']);
169177
}
170178

179+
function syncLocales() {
180+
osUtils.runExternalCommandSync('npm', ['run', 'locales:sync']);
181+
}
182+
171183
function sortScripts(scripts) {
172184
const sortedScripts = {};
173185
Object.keys(scripts)

packages/react-scripts/template-ef/blueprint.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ deploy:
4949
bindings:
5050
<<: *BINDING_DEFAULTS
5151

52-
deliver:
53-
deploy_order:
54-
- name: integration
55-
type: coupled
56-
systems: int
52+
#deliver:
53+
# deploy_order:
54+
# - name: integration
55+
# type: coupled
56+
# systems: int
5757

58-
- name: beta
59-
type: coupled
60-
systems: beta
58+
# - name: beta
59+
# type: coupled
60+
# systems: beta
6161

62-
- name: production
63-
type: coupled
64-
systems: prod
62+
# - name: production
63+
# type: coupled
64+
# systems: prod
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
options: {
3+
debug: true,
4+
sort: true,
5+
defaultLng: 'en',
6+
func: {
7+
list: ['i18next.t', 'i18n.t', 't', '$t'],
8+
},
9+
lngs: ['en'],
10+
resource: {
11+
loadPath: 'src/locales/{{lng}}/{{ns}}.json',
12+
savePath: '{{lng}}/{{ns}}.json',
13+
},
14+
keySeparator: false, // keeps dotted key paths flat instead of nested
15+
},
16+
};

packages/react-scripts/template/src/components/App.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import React from 'react'
22
import { Router, Link, NotFound, RequiresAuth } from '@fs/router'
3+
import { Trans } from 'react-i18next'
34
import Home from './home/Home'
45
import UserInfo from './user/UserInfo'
56

67
function App() {
78
return (
89
<>
910
<nav>
10-
<Link to="/">Home</Link>
11-
<Link to="user">User Info</Link>
11+
<Link to="/"><Trans i18nkey="nav.home">Home</Trans></Link>
12+
<Link to="user"><Trans i18nkey="nav.userInfo">User Info</Trans></Link>
1213
</nav>
1314

1415
<Router>
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
import { css } from '@emotion/core'
3+
import { Trans } from 'react-i18next'
34

45
import Logo from './Logo'
56
import HomeHeader from './HomeHeader'
@@ -9,24 +10,24 @@ const Home = () => (
910
css={css`
1011
text-align: center;
1112
`}
12-
>
13-
<HomeHeader>
14-
<Logo />
15-
<p>
16-
Edit <code>src/components/App.js</code> and save to reload.
17-
</p>
18-
<a
19-
css={css`
13+
>
14+
<HomeHeader>
15+
<Logo />
16+
<p>
17+
<Trans i18nkey="update.instructions">Edit <code>src/components/App.js</code> and save to reload.</Trans>
18+
</p>
19+
<a
20+
css={css`
2021
color: #61dafb;
2122
`}
22-
href="https://www.familysearch.org/frontier/docs/#/"
23-
target="_blank"
24-
rel="noopener noreferrer"
25-
>
26-
Learn Frontier
27-
</a>
28-
</HomeHeader>
29-
</div>
30-
)
23+
href="https://www.familysearch.org/frontier/docs/#/"
24+
target="_blank"
25+
rel="noopener noreferrer"
26+
>
27+
<Trans i18nkey="learn.frontier">Learn Frontier</Trans>
28+
</a>
29+
</HomeHeader>
30+
</div>
31+
)
3132

3233
export default Home

packages/react-scripts/template/src/components/home/Logo.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
import { css } from '@emotion/core'
3+
import { i18n } from '@fs/locale'
34
import logo from './Logo.svg'
45

56
const styles = css`
@@ -16,6 +17,6 @@ const styles = css`
1617
}
1718
`
1819

19-
const Logo = () => <img src={logo} css={styles} alt="logo" />
20+
const Logo = () => <img src={logo} css={styles} alt={i18n.t('logo', 'Spinning Frontier Logo')} />
2021

2122
export default Logo
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import React from 'react'
22
import { useUser } from '@fs/user'
3+
import { useTranslation } from 'react-i18next'
34

45
export default function UserInfo() {
56
const user = useUser()
6-
return <div>Hello, {user.displayName}!</div>
7+
const { t } = useTranslation()
8+
return <div>{t('user.greeting', 'Hello, {{user.displayName}}!')}</div>
79
}

packages/react-scripts/template/src/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ import React, { Suspense } from 'react'
22
import ReactDOM from 'react-dom'
33
import { UserProvider } from '@fs/user'
44
import RootErrorBoundary from '@fs/error-boundary'
5+
import { I18nProvider, addTranslations } from '@fs/locale'
56
import App from './components/App'
67
import * as serviceWorker from './serviceWorker'
8+
import translations from './locales'
9+
10+
addTranslations(translations)
711

812
const FrontierRoot = () => (
913
<RootErrorBoundary>
10-
<Suspense>
11-
<UserProvider>
12-
<App />
13-
</UserProvider>
14+
<Suspense fallback="Loading ...">
15+
<I18nProvider>
16+
<UserProvider>
17+
<App />
18+
</UserProvider>
19+
</I18nProvider>
1420
</Suspense>
1521
</RootErrorBoundary>
1622
)

0 commit comments

Comments
 (0)