# translation-adapter-ts

> The translation adapter for i18n_mrg tool support in TypeScript projects.

Latest version **1.1.0** (published 2020-03-04) · ISC license · 0 weekly downloads

## Install

```sh
npm install translation-adapter-ts
pnpm add translation-adapter-ts
yarn add translation-adapter-ts
bun add translation-adapter-ts
```

## 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 | 1.1.0 |
| Published | 2020-03-04 |
| First published | 2019-10-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 35 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | lehadnk |
| Maintainers | lehadnk |

## Links

- npm: https://www.npmjs.com/package/translation-adapter-ts
- npm.io page: https://npm.io/package/translation-adapter-ts

## Dependencies (2)

- [pofile](https://npm.io/package/pofile.md) ^1.1.0
- [typescript](https://npm.io/package/typescript.md) ^3.7.5

## Recent versions

- 1.1.0 (latest) — 2020-03-04
- 1.0.7 — 2020-02-26
- 1.0.6 — 2020-02-13
- 1.0.5 — 2020-02-13
- 1.0.4 — 2020-02-13
- 1.0.3 — 2020-02-12
- 1.0.2 — 2019-10-24
- 1.0.1 — 2019-10-24
- 1.0.0 — 2019-10-24

## README

# How to install
1. Add the translation package to the project:
```bash
npm i translation-adapter-ts
```
2. In your build script, add all the directories containing .po files to PofileTransformer:
```typescript
import {PofileTransformer} from "translation-adapter-ts/src/PofileTransformer";
let transformer = new PofileTransformer();
transformer.transformDirectory('i18n/ru_RU/LC_MESSAGES');
transformer.transformDirectory('i18n/zh_CN/LC_MESSAGES');
```
3. In app initialization script, load the translations, and define the default language (language translation are coming from), and language selected by user:
```typescript
import {Localizator} from "translation-adapter-ts/src/Localizator";
import {LanguageEnum} from "translation-adapter-ts/src/Enums/LanguageEnum";
import * as data from "translation-adapter-ts/test/translationFiles/test.json";
import {readFileSync} from "fs";

Localizator.addTranslation(LanguageEnum.RU, data);
Localizator.addTranslation(LanguageEnum.CN, readFileSync('/test/translationFiles/cn.json'));
Localizator.setDefaultLanguge(LanguageEnum.EN);
Localizator.setLanguage(LanguageEnum.RU);
```

If you use babel, with TS resolveJsonModule compiler directive, you may encounter an issue with data structure being exported as module with no default export. There's a workaround to fix that:
```typescript
import * as data from "translation-adapter-ts/test/translationFiles/test.json";
//@ts-ignore
Localizator.addTranslation(LanguageEnum.RU, data.default);
```

4. Wrap translations into __() function:
```typescript
import {__} from 'translation-adapter-ts/src/Main.ts';

console.log(__("Test string"));
console.log(__("Hello, %s", __("world")));
console.log(__("Hey %hello% %world%!"), {"%world%": __('world'), "hello": __("Hello")}); // In case if placeholder order may be different in different languages
```

# Using with web JS
```js
import Localizator from 'translation-adapter-ts/build/Localizator';

Localizator.addTranslation('kr', {
    "Hello": "여보세요"
});

Localizator.addTranslation('it', {
    "Hello": "Ciao"
});

Localizator.addTranslation('cn', {
    "Hello": "你好"
});

Localizator.setLanguage('cn');

export default function __ (args) {
    return Localizator.localize(args);
}

console.log(__('Hello')); // 你好
```

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