Skip to content

Commit

Permalink
Add typescript examples (#1578)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrodurek authored Nov 6, 2022
1 parent b93be8e commit 4874059
Show file tree
Hide file tree
Showing 60 changed files with 204 additions and 23,013 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*

.eslintcache
package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ This project was bootstrapped with [Create React App](https://github.com/faceboo

In the project directory, you can run:

### `yarn start`
### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.\
You will also see any lint errors in the console.

### `yarn test`
### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `yarn build`
### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.
Expand All @@ -29,7 +29,7 @@ Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `yarn eject`
### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
{
"name": "react-typescript4.1",
"version": "0.1.0",
"name": "simple-multi-namespaces",
"version": "1.0.0",
"private": true,
"devDependencies": {
"@types/jest": "26.0.15",
"@types/node": "12.0.0",
"@types/react": "17.0.1",
"@types/react-dom": "16.9.8",
"typescript": "4.1.2"
},
"dependencies": {
"i18next": "20.0.0",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-i18next": "11.8.x",
"react-scripts": "4.0.1",
"web-vitals": "0.2.4"
"i18next": "^22.0.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-i18next": "^12.0.0"
},
"devDependencies": {
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.8",
"react-scripts": "5.0.1",
"typescript": "^4.8.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
"react-app"
]
},
"browserslist": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { resources, defaultNS } from '../i18n/config';

declare module 'i18next' {
interface CustomTypeOptions {
defaultNS: typeof defaultNS;
resources: typeof resources['en'];
}
}
16 changes: 16 additions & 0 deletions example/react-typescript/simple-multi-namespaces/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import './i18n/config';
import { useTranslation } from 'react-i18next';

function App() {
const {t} = useTranslation();

return (
<div className="App">
<p>{t('title')}</p>
<p>{t('description.part1')}</p>
<p>{t('description.part2')}</p>
</div>
);
}

export default App;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useTranslation } from "react-i18next";

function Comp1() {
const {t} = useTranslation();

return (
<div className="App">
<p>{t('title')}</p>
<p>{t('description.part1')}</p>
<p>{t('description.part2')}</p>
</div>
);
}

export default Comp1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useTranslation } from "react-i18next";

function Comp2() {
const {t} = useTranslation('ns2');

return (
<div className="App">
<p>{t('description.part1')}</p>
<p>{t('description.part2')}</p>
</div>
);
}

export default Comp2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useTranslation } from "react-i18next";

function Comp2() {
const {t} = useTranslation(['ns1', 'ns2']);

return (
<div className="App">
<p>{t('ns1:description.part1')}</p>
<p>{t('ns2:description.part2')}</p>
</div>
);
}

export default Comp2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import i18next from 'i18next';
import { initReactI18next } from 'react-i18next';
import ns1 from './en/ns1.json';
import ns2 from './en/ns2.json';

export const defaultNS = 'ns1';

export const resources = {
en: {
ns1,
ns2,
}
};

i18next.use(initReactI18next).init({
lng: 'en', // if you're using a language detector, do not define the lng option
debug: true,
resources,
defaultNS,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"title": "Welcome to react using react-i18next fully type-safe",
"description": {
"part1": "This is a simple example.",
"part2": "😉"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"description": {
"part1": "In order to infer the appropriate type for t function, you should use type augmentation to override the Resources type.",
"part2": "Check out the @types/react-i18next to see an example."
"part2": "Check out the @types/i18next to see an example."
}
}
13 changes: 13 additions & 0 deletions example/react-typescript/simple-multi-namespaces/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';

const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"jsx": "react-jsx"
},
"include": [
"src",
"@types"
"src"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*

.eslintcache
package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ This project was bootstrapped with [Create React App](https://github.com/faceboo

In the project directory, you can run:

### `yarn start`
### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.\
You will also see any lint errors in the console.

### `yarn test`
### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `yarn build`
### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.
Expand All @@ -29,7 +29,7 @@ Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `yarn eject`
### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
{
"name": "react-typescript4.1",
"version": "0.1.0",
"name": "simple",
"version": "1.0.0",
"private": true,
"devDependencies": {
"@types/jest": "26.0.15",
"@types/node": "12.0.0",
"@types/react": "17.0.1",
"@types/react-dom": "16.9.8",
"typescript": "4.1.2"
},
"dependencies": {
"i18next": "20.0.0",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-i18next": "11.8.x",
"react-scripts": "4.0.1",
"web-vitals": "0.2.4"
"i18next": "^22.0.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-i18next": "^12.0.0"
},
"devDependencies": {
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.8",
"react-scripts": "5.0.1",
"typescript": "^4.8.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
"react-app"
]
},
"browserslist": {
Expand Down
7 changes: 7 additions & 0 deletions example/react-typescript/simple/src/@types/i18next.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { resources } from '../i18n/config';

declare module 'i18next' {
interface CustomTypeOptions {
resources: typeof resources['en'];
}
}
16 changes: 16 additions & 0 deletions example/react-typescript/simple/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import './i18n/config';
import { useTranslation } from 'react-i18next';

function App() {
const {t} = useTranslation();

return (
<div className="App">
<p>{t('title')}</p>
<p>{t('description.part1')}</p>
<p>{t('description.part2')}</p>
</div>
);
}

export default App;
15 changes: 15 additions & 0 deletions example/react-typescript/simple/src/i18n/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import i18next from 'i18next';
import { initReactI18next } from 'react-i18next';
import translation from './en/translation.json';

export const resources = {
en: {
translation,
}
};

i18next.use(initReactI18next).init({
lng: 'en', // if you're using a language detector, do not define the lng option
debug: true,
resources,
});
7 changes: 7 additions & 0 deletions example/react-typescript/simple/src/i18n/en/translation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"title": "Welcome to react using react-i18next fully type-safe",
"description": {
"part1": "This is a simple example.",
"part2": "😉"
}
}
13 changes: 13 additions & 0 deletions example/react-typescript/simple/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';

const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"jsx": "react-jsx"
},
"include": [
"src",
"@types"
"src"
]
}
1 change: 0 additions & 1 deletion example/react-typescript4.1/namespaces/.env

This file was deleted.

This file was deleted.

Loading

0 comments on commit 4874059

Please sign in to comment.