# @leonardosarmentocastro/i18n

> support content translation using .yml

Latest version **2.0.0** (published 2023-12-07) · ISC license · 0 weekly downloads

## Install

```sh
npm install @leonardosarmentocastro/i18n
pnpm add @leonardosarmentocastro/i18n
yarn add @leonardosarmentocastro/i18n
bun add @leonardosarmentocastro/i18n
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2023-12-07 |
| First published | 2021-05-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 5.1 KB |
| Known vulnerabilities | 0 (+6 in 2 direct dependencies) |
| Install scripts | no |
| Author | leonardosarmentocastro |
| Maintainers | leonardosarmentocastro |
| Keywords | i18n, internationalization, translate, yml |

## Links

- npm: https://www.npmjs.com/package/@leonardosarmentocastro/i18n
- npm.io page: https://npm.io/package/@leonardosarmentocastro/i18n

## Dependencies (4)

- [locale](https://npm.io/package/locale.md) ^0.1.0
- [js-yaml](https://npm.io/package/js-yaml.md) ^4.1.0
- [mongoose](https://npm.io/package/mongoose.md) 6.5.0
- [messageformat](https://npm.io/package/messageformat.md) ^2.3.0

## Alternatives

- [monaco-yaml](https://npm.io/package/monaco-yaml.md) — 420.1K weekly downloads
- [@crewx/workflow](https://npm.io/package/@crewx/workflow.md) — 3.1K weekly downloads
- [yaml-cat](https://npm.io/package/yaml-cat.md) — 38 weekly downloads
- [nunjucks-in-yaml](https://npm.io/package/nunjucks-in-yaml.md) — 9 weekly downloads
- [shopify-symlinks](https://npm.io/package/shopify-symlinks.md) — 3 weekly downloads

## Recent versions

- 2.0.0 (latest) — 2023-12-07
- 1.3.1 — 2022-08-30
- 1.0.1 — 2022-08-07
- 1.0.0 — 2022-08-04
- 0.0.1 — 2021-05-04

## README

# i18n

### Summary

- store under `./translations` folder, `.yml` files that corresponds to an available language (e.g. `.translations/en-us.yml`)
- translation files are interpreted by [messageformat engine](https://github.com/messageformat/messageformat) engine
- each translation file contains a set of lines as `key:value`, e.g:
  ```yml
  SHARED_ERROR_EMAIL_INVALID: The email "{email}" is invalid.
  USERS_ERROR_USER_NOT_FOUND: User "{userId}" was not found.
  ```
- the default language is set by `process.env.LANGUAGE` and is available in all requests by `req.locale` prop


### Usage

  ```yml
  # .translations/en-us.yml
  TRANSLATED_HELLO: Hello "{name}". Welcome!
  ```

  ```yml
  # .translations/pt-br.yml
  TRANSLATED_HELLO: Olá "{name}". Bem vindo!
  ```

  ```js
  // index.js
  process.env.LOCALE = 'en-us.yml';
  const server = require('@leonardosarmentocastro/server');
  const i18n = require('@leonardosarmentocastro/i18n');

  (async () => {
    await server.start(8080, {
      connect: (app) => {
        i18n.connect(app);

        app.get('/translated', (req, res) => {
          const key = 'TRANSLATED_HELLO';
          const args = { name: 'Leonardo' };
          const translation = i18n.translate.get(key, req.locale, args);

          return res.status(200).send(translation);
        });
      },
    });
  })();
  ```

  ```sh
  curl -X GET http://localhost:8080/translated -H 'accept-language: en-us'
  # Hello "Leonardo". Welcome!

  curl -X GET http://localhost:8080/translated -H 'accept-language: pt-br'
  # Olá "Leonardo". Bem vindo!
  ```

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