# pick-by-lang

> > Helper for transforming objects with i18n content

Latest version **1.0.0** (published 2019-04-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install pick-by-lang
pnpm add pick-by-lang
yarn add pick-by-lang
bun add pick-by-lang
```

## 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.0.0 |
| Published | 2019-04-03 |
| First published | 2019-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 7.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Igor Kamyshev |
| Maintainers | igor.kamyshev |

## Links

- npm: https://www.npmjs.com/package/pick-by-lang
- Repository: https://github.com/igorkamyshev/pick-by-lang
- Homepage: https://github.com/igorkamyshev/pick-by-lang#readme
- Issues: https://github.com/igorkamyshev/pick-by-lang/issues
- npm.io page: https://npm.io/package/pick-by-lang

## Dependencies (2)

- [lodash.mapvalues](https://npm.io/package/lodash.mapvalues.md) ^4.6.0
- [lodash.isplainobject](https://npm.io/package/lodash.isplainobject.md) ^4.0.6

## Recent versions

- 1.0.0 (latest) — 2019-04-03

## README

# pick-by-lang
 
> Helper for transforming objects with i18n content
 
## Installation
 
```sh
yarn add pick-by-lang
```
 
Or if you prefer `npm`:
 
```sh
npm i pick-by-lang
```
 
## Usage
 
### TL;DR
 
```js
import { pickByLang } from 'pick-by-lang'
 
const pickRu = pickByLang('ru')
const pickEn = pickByLang('en')
 
const content = {
  title: {
    en: 'Shop',
    ru: 'Магазин',
  }
})
 
console.log(pickRu(content)) // { title: 'Магазин' }
console.log(pickEn(content)) // { title: 'Shop' }
```
 
### Docs
 
`pickByLang` accepts two parameters — `lang` and `content` for translating. If content is not supplied, it returns function with one argument.
 
Example:
```js
const content = {
  title: {
    en: 'Shop',
    ru: 'Магазин',
  }
})
 
// Simple usage
console.log(pickByLang('ru', content)) // { title: 'Магазин' }
 
// Curried usage
const pickRu = pickByLang('ru')
console.log(pickRu(content)) // { title: 'Магазин' }
```
 
Lib can handle objects constructed by following rules:
 
1. Any field can contain language variants. If field does not contain language variants, it will be left untouched.
 
Example:
```js
const content = {
  title: {
    en: 'Shop',
    ru: 'Магазин',
  },
  site: 'https://google.com'
}
 
console.log(pickByLang('en', content)) // { title: 'Shop', site: 'https://google.com' }
```
 
2. You can create fields for specific languages and hide it for other.
 
Example:
```js
const content = {
  name: {
    en: 'Igor',
    ru: 'Игорь',
  },
  slides: [
    {
      name: 'Hop-hey, only for ru',
      onlyFor: ['ru'],
    },
    {
      name: 'Text for any language',
    },
  ],
}
 
console.log(pickByLang('en', content)) // { name: 'Igor', slides: [{ name: 'Text for any language' }] }
console.log(pickByLang('ru', content)) // { name: 'Igor', slides: [{ name: 'Hop-hey, only for ru' }, { name: ''Text for any language' }] }
```
 
That's all. Enjoy!

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