# @mies-co/next-lng

> Use translations within nextjs.

Latest version **0.0.47** (published 2020-11-20) · 0 weekly downloads

## Install

```sh
npm install @mies-co/next-lng
pnpm add @mies-co/next-lng
yarn add @mies-co/next-lng
bun add @mies-co/next-lng
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.47 |
| Published | 2020-11-20 |
| First published | 2020-07-21 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 54.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Sébastien d'Olbecke |
| Maintainers | sebdolbecke |

## Links

- npm: https://www.npmjs.com/package/@mies-co/next-lng
- Repository: https://github.com/mies-co/next-extensions
- Homepage: https://github.com/mies-co/next-extensions#readme
- Issues: https://github.com/mies-co/next-extensions/issues
- npm.io page: https://npm.io/package/@mies-co/next-lng

## Dependencies (5)

- [glob](https://npm.io/package/glob.md) ^7.1.6
- [lodash](https://npm.io/package/lodash.md) ^4.17.19
- [nookies](https://npm.io/package/nookies.md) ^2.3.2
- [deepmerge](https://npm.io/package/deepmerge.md) ^4.2.2
- [@mies-co/next-utils](https://npm.io/package/@mies-co/next-utils.md) ^0.0.9

## Recent versions

- 0.0.47 (latest) — 2020-11-20
- 0.0.46 — 2020-09-24
- 0.0.45 — 2020-09-24
- 0.0.44 — 2020-09-24
- 0.0.43 — 2020-09-24
- 0.0.42 — 2020-09-24
- 0.0.41 — 2020-09-04
- 0.0.40 — 2020-09-04
- 0.0.39 — 2020-09-02
- 0.0.38 — 2020-09-02
- 0.0.37 — 2020-09-02
- 0.0.36 — 2020-09-02
- 0.0.35 — 2020-09-02
- 0.0.34 — 2020-09-02
- 0.0.33 — 2020-09-01
- … 31 more at https://npm.io/package/@mies-co/next-lng/versions

## README

[![Next-Lng](https://user-images.githubusercontent.com/33988299/88075764-88832980-cb79-11ea-865c-86ce7b07c91e.png)](https://github.com/mies-co/next-extensions/tree/master/packages/next-lng)

[![Package Quality](https://npm.packagequality.com/shield/@mies-co%2Fnext-lng.svg)](https://packagequality.com/#?package=@mies-co/next-lng)

Light and easy solution to translate your Next.js apps. 

This is a simpler alternative to [next-i18next](https://github.com/isaachinman/next-i18next).

# Table of contents

- [Example](#Example)
- [Installation](#Installation)
- [Basic translation](#Basic-translation)
- [Scoped translation](#Scoped-translation)
- [Legacy getInitialProps translation](#Legacy-getInitialProps-translation)

## Example

[See an example app here](https://github.com/mies-co/next-extensions/tree/master/examples/next-lng-example)

## Installation

```env
# regular NPM package install 
npm install --save @mies-co/next-lng
 
# install directly from GitHub 
npm install --save github:@mies-co/next-lng
 
# install NPM package via Yarn 
yarn add @mies-co/next-lng
```

**OR USING THE GITHUB REGISTRY**

You may also create a .npmrc file at the root of your Next.js app, with the following content:

```sh
# Specifies the registraty for @mies-co packages
@mies-co:registry=https://npm.pkg.github.com/
```

Then add this in package.json:

```json
{
    "dependencies": {
        "@mies-co/next-lng": "latest"
    }
}
```

## Basic translation

[embedmd]:# (../../examples/next-lng-example/src/pages/[lng]/index.js)
```js
import { withLng, useLng, getServerSidePropsLng, Link } from "@mies-co/next-lng";

const HomePage = props => {
	// useLng can be used anywhere in your app, it's a React context.
	const { lng, setLng, t } = useLng();

	// NB! the ids on dom elements are used only for testing purposes and can be safely deleted
	return (
		<>
			<h1>Basic example</h1>
			<h2 id="x-greet">{t("greet")}</h2>
			<p id="x-whoami">{t("whoami", { firstname: "Bob" })}</p>
			<button onClick={() => setLng("en")}>EN</button>
			<button onClick={() => setLng("fr")}>FR</button>
			<p>Current language is {lng}</p>
			<br />
			<h2>Links:</h2>
			<Link href="/legacy">
				<a>Legacy example</a>
			</Link>
			<br />
			<Link href="/scoped">
				<a>Scoped example</a>
			</Link>
			<br />
			<Link href="/[slug]" as="/with-slug">
				<a>Slug example</a>
			</Link>
		</>
	);
};

export { getServerSidePropsLng as getServerSideProps };
export default withLng(HomePage);
```

- `withLng`: A HOC to wrap your page with.
- `useLng`: A react context exposing `lng`, `setLng` and `t`.
- `getServerSidePropsLng`: An async function fetches your lng API route

## Scoped translation

[embedmd]:# (../../examples/next-lng-example/src/pages/[lng]/scoped.js)
```js
// Example of scoped translation

import { withLng, useLng, getTranslations } from "@mies-co/next-lng";

const Scoped = () => {
	// useLng can be used anywhere in your app, it's a React context.
	const { lng, setLng, t } = useLng();
	// NB! the ids on dom elements are used only for testing purposes and can be safely deleted
	return (
		<>
			<h1>Scoped example</h1>
			<h2 id="x-header-title">{t("header:title")}</h2>
			<p id="x-greet">{t("greet")}</p>
			<p id="x-whoami">{t("whoami", { firstname: "Bob" })}</p>
			<button onClick={() => setLng("en")}>EN</button>
			<button onClick={() => setLng("fr")}>FR</button>
			<p>Current language is {lng}</p>
		</>
	);
};

// Arguments:
// [0] - a string or string[] of globs
// [1] - an object that overrides the default `options` defined in next.config.js
const getServerSideProps = getTranslations(["*/common", "header"], { shallow: false });
export { getServerSideProps };

export default withLng(Scoped);
```

- `withLng`: A HOC to wrap your page with.
- `useLng`: A react context exposing `lng`, `setLng` and `t`.
- `getTranslations`: A function that passes extra arguments to the default getServerSideProps (fetching your API routes)
    - [0] A glob string or an array of glob strings that match the translations files to be fetched
    - [1] The options object overrides the options defined in your `next.config.js` file. It enables to override "per-file".


## Legacy getInitialProps translation

[embedmd]:# (../../examples/next-lng-example/src/pages/[lng]/legacy.js)
```js
// Example of legacy getInitialProps usage

import { withLng, useLng, getTranslations } from "@mies-co/next-lng";

const Legacy = props => {
	// useLng can be used anywhere in your app, it's a React context.
	const { lng, setLng, t } = useLng();
	// NB! the ids on dom elements are used only for testing purposes and can be safely deleted
	return (
		<>
			<h1>Legacy example</h1>
			<h2 id="x-header-title">{t("header:title")}</h2>
			<p id="x-greet">{t("greet")}</p>
			<p id="x-whoami">{t("whoami", { firstname: "Bob" })}</p>
			<button onClick={() => setLng("en")}>EN</button>
			<button onClick={() => setLng("fr")}>FR</button>
			<p>Current language is {lng}</p>
		</>
	);
};

Legacy.getInitialProps = async () => {
	return {
		// Pass a lng object that describes the scope and options to pass
		// This is only if you want to use scoped translations or customize the options. Otherwise don't even return anything.
		lng: {
			scope: ["*/common", "header"],
			// options: { shallow: false }
		}
	};
};

export default withLng(Legacy);
```

---
_Source: https://npm.io/package/@mies-co/next-lng · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
