Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz committed Jul 17, 2022
0 parents commit ba99c6b
Show file tree
Hide file tree
Showing 36 changed files with 3,722 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
22 changes: 22 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react/recommended",
"plugin:react-hooks/recommended",
"plugin:prettier/recommended"
],
"rules": {
"@typescript-eslint/no-var-requires": "off"
},
"env": {
"browser": true,
"es2021": true
}
}
39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: CI
on:
push:
branches:
- main
pull_request:
jobs:
lint:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: pnpm/[email protected]
with:
version: 7
- name: Use Node.js 16
uses: actions/setup-node@v2
with:
node-version: 16
cache: 'pnpm'
- name: Install dependencies
run: pnpm install
- name: Run ESLint
run: pnpm lint
typecheck:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: pnpm/[email protected]
with:
version: 7
- name: Use Node.js 16
uses: actions/setup-node@v2
with:
node-version: 16
cache: 'pnpm'
- name: Install dependencies
run: pnpm install
- name: Run TSC
run: pnpm typecheck
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules/
.next/
out/
dist/

.DS_Store
*.log*
.eslintcache
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

pnpm lint-staged
10 changes: 10 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": true,
"arrowParens": "avoid"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Tom Lienard

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
140 changes: 140 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<p align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="./assets/logo-white.png">
<source media="(prefers-color-scheme: light)" srcset="./assets/logo-black.png" />
<img alt="" height="80px" src="./assets/logo-white.png">
</picture>
<br />
Type-safe internationalization (i18n) for Next.js
</p>

---

- [Features](#features)
- [Usage](#usage)
- [Examples](#examples)
- [Change current language](#change-current-language)
- [Use JSON files instead of TS for locales](#use-json-files-instead-of-ts-for-locales)
- [License](#license)

## Features

- **100% Type-safe**: Locales in TS or JSON, type-safe `t()`, type-safe params
- **Small**: 1.2 KB gzipped (1.7 KB uncompressed), no dependencies
- **Simple**: No webpack configuration, no CLI, just pure TypeScript
- **SSR**: Load only the required locale, SSRed

## Usage

```bash
pnpm install next-international
```

1. Make sure that you've followed [Next.js Internationalized Routing](https://nextjs.org/docs/advanced-features/i18n-routing), and that `strict` is set to `true` in your `tsconfig.json`

2. Create `locales/index.ts` with your locales:

```ts
import { createI18n } from 'next-international'
import type Locale from './en'

export const {
useI18n,
I18nProvider,
getLocaleStaticProps,
} = createI18n<typeof Locale>({
en: () => import('./en'),
fr: () => import('./fr'),
});
```

Each locale file should export a default object (don't forget `as const`):

```ts
// locales/en.ts
export default {
'hello': 'Hello',
'welcome': 'Hello {name}!',
} as const
```

3. Add `getLocaleStaticProps` to your pages if you want SSR, or wrap your existing `getStaticProps`:

```ts
// pages/index.tsx
export const getStaticProps = getLocaleStaticProps()

// or with an existing `getStaticProps` function:
export const getStaticProps = getLocaleStaticProps((ctx) => {
// your existing code
return {
...
}
})
```

4. Use `useI18n`:

```tsx
import { useI18n } from '../locales'

function App() {
const t = useI18n()
return (
<div>
<p>{t('hello')}</p>
<p>{t('welcome', { name: 'John' })}</p>
</div>
)
}
```

## Examples

### Change current language

Export `useChangeLocale` from `createI18n`:
```tsx
export const {
useChangeLocale,
...
} = createI18n({
...
});
```

Then use this as a hook:
```tsx
import { useChangeLocale } from '../locales'

function App() {
const changeLocale = useChangeLocale()

return (
<button onClick={() => changeLocale('en')}>English</button>
<button onClick={() => changeLocale('fr')}>French</button>
)
}
```

### Use JSON files instead of TS for locales

Currently, this breaks the parameters type-safety, so we recommend using the TS syntax. See this issue: https://github.com/microsoft/TypeScript/issues/32063.

```ts
import { createI18n } from 'next-international'
import type Locale from './en.json'

export const {
useI18n,
I18nProvider,
getLocaleStaticProps,
} = createI18n<typeof Locale>({
en: () => import('./en.json'),
fr: () => import('./fr.json'),
});
```

## License

[MIT](./LICENSE)
Binary file added assets/logo-black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo-white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/next/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
7 changes: 7 additions & 0 deletions examples/next/locales/en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
console.log('Loaded EN');

export default {
hello: 'Hello',
welcome: 'Hello {name}!',
'about.you': 'Hello {name}! You have {age} yo',
} as const;
9 changes: 9 additions & 0 deletions examples/next/locales/fr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineLocale } from '.';

console.log('Loaded FR');

export default defineLocale({
hello: 'Bonjour',
welcome: 'Bonjour {name}!',
'about.you': 'Bonjour {name}! Vous avez {age} ans',
});
9 changes: 9 additions & 0 deletions examples/next/locales/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createI18n } from 'next-international';
import type Locale from './en';

export const { useI18n, I18nProvider, useChangeLocale, defineLocale, getLocaleStaticProps } = createI18n<typeof Locale>(
{
en: () => import('./en'),
fr: () => import('./fr'),
},
);
5 changes: 5 additions & 0 deletions examples/next/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
13 changes: 13 additions & 0 deletions examples/next/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// const withTM = require('next-transpile-modules')(['next-international']);

/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
i18n: {
locales: ['en', 'fr'],
defaultLocale: 'en',
},
};

module.exports = nextConfig;
25 changes: 25 additions & 0 deletions examples/next/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "next-i18n",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"next": "12.2.2",
"next-international": "workspace:*",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"@types/node": "^18.0.4",
"@types/react": "^18.0.15",
"eslint": "8.19.0",
"eslint-config-next": "12.2.2",
"next-transpile-modules": "^9.0.0",
"typescript": "^4.7.4"
}
}
12 changes: 12 additions & 0 deletions examples/next/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AppProps } from 'next/app';
import { I18nProvider } from '../locales';

const App = ({ Component, pageProps }: AppProps) => {
return (
<I18nProvider locale={pageProps.locale}>
<Component {...pageProps} />
</I18nProvider>
);
};

export default App;
36 changes: 36 additions & 0 deletions examples/next/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { GetStaticProps } from 'next';
import { getLocaleStaticProps, useChangeLocale, useI18n } from '../locales';

const Home = () => {
const t = useI18n();
const changeLocale = useChangeLocale();

return (
<div>
<p>Hello: {t('hello')}</p>
<p>
Hello:{' '}
{t('welcome', {
name: 'John',
})}
</p>
<p>
Hello:{' '}
{t('about.you', {
age: '23',
name: 'Doe',
})}
</p>
<button type="button" onClick={() => changeLocale('en')}>
EN
</button>
<button type="button" onClick={() => changeLocale('fr')}>
FR
</button>
</div>
);
};

export const getStaticProps: GetStaticProps = getLocaleStaticProps();

export default Home;
Binary file added examples/next/public/favicon.ico
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/next/public/vercel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ba99c6b

Please sign in to comment.