# eslint-plugin-lang3

> ESLint plugin for i18n.

Latest version **0.0.1** (published 2023-10-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install eslint-plugin-lang3
pnpm add eslint-plugin-lang3
yarn add eslint-plugin-lang3
bun add eslint-plugin-lang3
```

## 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.1 |
| Published | 2023-10-10 |
| First published | 2023-10-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10.0 |
| Dependencies | 3 |
| Unpacked size | 44.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | itlangit |
| Maintainers | liusheng96gentle |
| Keywords | eslint, eslintplugin, eslint-plugin, i18n, i10next, internationalization, localization |

## Links

- npm: https://www.npmjs.com/package/eslint-plugin-lang3
- npm.io page: https://npm.io/package/eslint-plugin-lang3

## Dependencies (3)

- [fs-extra](https://npm.io/package/fs-extra.md) ^11.1.1
- [requireindex](https://npm.io/package/requireindex.md) ~1.1.0
- [@vitalets/google-translate-api](https://npm.io/package/@vitalets/google-translate-api.md) ^9.2.0

## Alternatives

- [eslint-plugin-sonarjs](https://npm.io/package/eslint-plugin-sonarjs.md) — 2.9M weekly downloads
- [eslint-config-expo](https://npm.io/package/eslint-config-expo.md) — 1.5M weekly downloads
- [@matter/protocol](https://npm.io/package/@matter/protocol.md) — 63.5K weekly downloads
- [@eventcatalog/linter](https://npm.io/package/@eventcatalog/linter.md) — 24.8K weekly downloads
- [@pandacss/eslint-plugin](https://npm.io/package/@pandacss/eslint-plugin.md) — 18.7K weekly downloads

## Recent versions

- 0.0.1 (latest) — 2023-10-10

## README

# eslint-plugin-lang2

ESLint plugin for i18n

## Installation

```bash
npm install eslint-plugin-lang2 --save-dev
```

## Usage

Add `lang` to the plugins section of your `.eslintrc` configuration file.

```json
{
  "plugins": ["lang"]
}
```

Then configure the rules you want to use under the rules section.

```json
{
  "rules": {
    "lang/no-literal-string": 2
  }
}
```

or

```json
{
  "extends": ["plugin:lang/recommended"]
}
```

## Rule `no-literal-string`

This rule aims to avoid developers to display literal string to users
in those projects which need to support [multi-language](https://www.lang.com/).

> <span style="color: lightcoral">Note:</span> Disable auto-fix because key in the call `lang.t(key)` ussally was not the same as the literal

### Rule Details

**It will find out all literal strings and validate them.**

Examples of **incorrect** code for this rule:

```js
/*eslint lang/no-literal-string: "error"*/
const a = 'foo';
```

Examples of **correct** code for this rule:

```js
/*eslint lang/no-literal-string: "error"*/
// safe to assign string to const variables whose name are UPPER_CASE
var FOO = 'foo';

// UPPER_CASE properties are valid no matter if they are computed or not
var a = {
  BAR: 'bar',
  [FOO]: 'foo'
};

// also safe to use strings themselves are UPPCASE_CASE
var foo = 'FOO';
```

#### i18n

This rule allows to call lang translate function.

**Correct** code:

```js
/*eslint lang/no-literal-string: "error"*/
var bar = lang.t('bar');
var bar2 = i18n.t('bar');
```

Maybe you use other internationalization libraries
not [lang](https://www.lang.com/). You can use like this:

```js
/*eslint lang/no-literal-string: ["error", { "ignoreCallee": ["yourI18n"] }]*/
const bar = yourI18n('bar');

// or

/*eslint lang/no-literal-string: ["error", { "ignoreCallee": ["yourI18n.method"] }]*/
const bar = yourI18n.method('bar');
```

#### HTML Markup

All literal strings in html template are typically mistakes. For JSX example:

```HTML
<div>foo</div>
```

They should be translated by [lang translation api](https://www.lang.com/):

```HTML
<div>{lang.t('foo')}</div>
```

Same for [Vue template](https://vuejs.org/v2/guide/syntax.html):

```HTML
<!-- incorrect -->
<template>
  foo
</template>

<!-- correct -->
<template>
  {{ lang.t('foo') }}
</template>
```

It would allow most reasonable usages of string that would rarely be shown to user, like following examples.

Click on them to see details.

<details>
<summary>
react-lang
</summary>

This plugin are compatible with [react-lang](https://react.lang.com/)

```tsx
// correct
<Trans>
  <span>bar</span>
</Trans>
```

</details>

<details>
<summary>
Redux/Vuex
</summary>

This rule also works with those state managers like
[Redux](https://redux.js.org/) and [Vuex](https://vuex.vuejs.org/).

**Correct** code:

```js
var bar = store.dispatch('bar');
var bar2 = store.commit('bar');
```

</details>

<details>
<summary>
Typescript
</summary>

This plugin would not complain on those reasonable usages of string.

The following cases are considered as **correct**:

```typescript
var a: Type['member'];
var a: Omit<T, 'key'>;
enum E {
  A = 1
}
var a = E['A'];
var a: { t: 'button' } = { t: 'button' };
var a: 'abc' | 'name' = 'abc';
```

We require type information to work properly, so you need to add some options in your `.eslintrc`:

```js
  "parserOptions": {
    // path of your tsconfig.json
    "project": "./tsconfig.json"
  }
```

See
[here](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin#usage)
for more deteils.

</details>

<details>

<summary>
Import/Export
</summary>

The following cases are **allowed**:

```typescript
import mod from 'm';
import('mod');
require('mod');

export { named } from 'm';
export * from 'm';
```

</details>

<details>
<summary>
String Comparison
</summary>

String comparison is fine.

```typescript
// correct
name === 'Android' || name === 'iOS';
```

</details>

<details>
<summary>
SwithCase
</summary>

Skip switchcase statement:

```typescript
// correct
switch (type) {
  case 'foo':
    break;
  case 'bar':
    break;
}
```

</details>

### Options

### markupOnly

If `markupOnly` option turn on, only JSX text and strings used as JSX attributes will be validated.

JSX text:

```jsx
// incorrect
<div>hello world</div>
<div>{"hello world"}</div>
```

Strings as JSX attribute:

```jsx
// incorrect
<div foo="foo"></div>
<div foo={"foo"}></div>
```

### onlyAttribute

Only check the `JSX` attributes that listed in this option. **This option would turn on `markupOnly`.**

```jsx
// correct
const foo = 'bar';
<div foo="foo"></div>

// incorrect
<div>foo</div>

/*eslint lang/no-literal-string: ["error", {"onlyAttribute": ["foo"]}]*/
// incorrect
<div foo="foo"></div>
```

#### ignore

The `ignore` option specifies exceptions not to check for
literal strings that match one of regexp paterns.

Examples of correct code for the `{ "ignore": ['foo'] }` option:

```js
/*eslint lang/no-literal-string: ["error", {"ignore": ["foo"]}]*/
const a = 'afoo';
```

#### ignoreCallee

THe `ignoreCallee` option speficies exceptions not check for
function calls whose names match one of regexp patterns.

Examples of correct code for the `{ "ignoreCallee": ["foo"] }` option:

```js
/*eslint lang/no-literal-string: ["error", { "ignoreCallee": ["foo"] }]*/
const bar = foo('bar');
```

#### ignoreAttribute

The `ignoreAttribute` option specifies exceptions not to check for JSX attributes that match one of ignored attributes.

Examples of correct code for the `{ "ignoreAttribute": ["foo"] }` option:

```jsx
/*eslint lang/no-literal-string: ["error", { "ignoreAttribute": ["foo"] }]*/
const element = <div foo="bar" />;
```

#### ignoreProperty

The `ignoreProperty` option specifies exceptions not to check for object properties that match one of ignored properties.

Examples of correct code for the `{ "ignoreProperty": ["foo"] }` option:

```jsx
/*eslint lang/no-literal-string: ["error", { "ignoreProperty": ["foo"] }]*/
const a = { foo: 'bar' };
```

#### ignoreComponent

The `ignoreComponent` option specifies exceptions not to check for string literals inside a list of named components. It includes `<Trans>` per default.

Examples of correct code for the `{ "ignoreComponent": ["Icon"] }` option:

```jsx
/*eslint lang/no-literal-string: ["error", { "ignoreComponent": ["Icon"] }]*/
<Icon>arrow<Icon/>
```

#### validateTemplate

Indicate whether to validate [template strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) or not. Default `false`

```js
/*eslint lang/no-literal-string: ["error", { "validateTemplate": true }]*/
// incorrect
var foo = `hello world`;
```

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