Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"extends": "./node_modules/gts/",
"overrides": [
{
"files": "*.ts",
"parser": "@typescript-eslint/parser",
"rules": {
"no-unused-vars": "off",
"no-useless-constructor": "off",
Expand All @@ -12,9 +12,10 @@
}
],
"parserOptions": {
"ecmaVersion": 2017,
"ecmaVersion": 2020,
"sourceType": "module"
},

"plugins": [
"@typescript-eslint/eslint-plugin"
]
Expand Down
24 changes: 24 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"extends": "./node_modules/gts/",
"overrides": [
{
"files": ["**/*.ts", "**/*.tsx"],
"rules": {
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unsafe-function-type": "off",
"@typescript-eslint/no-empty-object-type": "off"
}
}
],
"parser": "@babel/eslint-parser",
"parserOptions": {
"requireConfigFile": false,
"babelOptions": {
"babelrc": false,
"configFile": false,
"presets": ["@babel/preset-typescript"]
}
}
}
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* text=auto eol=lf

17 changes: 4 additions & 13 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [10.x, 12.x, 14.x]
node: [20, 22, 24]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand All @@ -27,7 +27,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '12.x'
node-version: '22'
- run: npm install
- run: npm test
coverage:
Expand All @@ -36,26 +36,17 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '14.x'
node-version: '22'
- run: npm install
- run: npm test
- run: npm run coverage
esm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '14.x'
- run: npm install
- run: npm run test:esm
deno:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
node-version: 22
- run: npm install
- run: npm run compile
- uses: denolib/setup-deno@v2
Expand Down
3 changes: 3 additions & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require('gts/.prettierrc.json')
}
54 changes: 28 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# y18n

[![NPM version][npm-image]][npm-url]
[![js-standard-style][standard-image]][standard-url]
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)

The bare-bones internationalization library used by yargs.
Expand All @@ -12,8 +11,9 @@ Inspired by [i18n](https://www.npmjs.com/package/i18n).

_simple string translation:_

```js
const __ = require('y18n')().__;
```mjs
import y18n from 'y18n';
const __ = y18n().__;

console.log(__('my awesome string %s', 'foo'));
```
Expand All @@ -25,7 +25,8 @@ output:
_using tagged template literals_

```js
const __ = require('y18n')().__;
import y18n from 'y18n';
const __ = y18n().__;

const str = 'foo';

Expand All @@ -39,7 +40,8 @@ output:
_pluralization support:_

```js
const __n = require('y18n')().__n;
import y18n from 'y18n';
const __n = y18n().__n;

console.log(__n('one fish %s', '%d fishes %s', 2, 'foo'));
```
Expand All @@ -53,14 +55,14 @@ output:
As of `v5` `y18n` supports [Deno](https://github.com/denoland/deno):

```typescript
import y18n from "https://deno.land/x/y18n/deno.ts";
import y18n from 'https://deno.land/x/y18n/deno.ts';

const __ = y18n({
locale: 'pirate',
directory: './test/locales'
}).__
directory: './test/locales',
}).__;

console.info(__`Hi, ${'Ben'} ${'Coe'}!`)
console.info(__`Hi, ${'Ben'} ${'Coe'}!`);
```

You will need to run with `--allow-read` to load alternative locales.
Expand All @@ -73,41 +75,43 @@ File names correspond to locales, e.g., `en.json`, `pirate.json`.
When strings are observed for the first time they will be
added to the JSON file corresponding to the current locale.

## Methods

### require('y18n')(config)
## Factory Function

Create an instance of y18n with the config provided, options include:
The default export from y18n is a factory function. Use the function to create an instance of y18n. You may optionally supply configure options:

* `directory`: the locale directory, default `./locales`.
* `updateFiles`: should newly observed strings be updated in file, default `true`.
* `locale`: what locale should be used.
* `fallbackToLanguage`: should fallback to a language-only file (e.g. `en.json`)
- `directory`: the locale directory, default `./locales`.
- `updateFiles`: should newly observed strings be updated in file, default `true`.
- `locale`: what locale should be used.
- `fallbackToLanguage`: should fallback to a language-only file (e.g. `en.json`)
be allowed if a file matching the locale does not exist (e.g. `en_US.json`),
default `true`.

### y18n.\_\_(str, arg, arg, arg)
## Methods

A y18n instance has the following methods.

### \_\_(str, arg, arg, arg)

Print a localized string, `%s` will be replaced with `arg`s.

This function can also be used as a tag for a template literal. You can use it
like this: <code>__&#96;hello ${'world'}&#96;</code>. This will be equivalent to
`__('hello %s', 'world')`.
like this: <code>**&#96;hello ${'world'}&#96;</code>. This will be equivalent to
`**('hello %s', 'world')`.

### y18n.\_\_n(singularString, pluralString, count, arg, arg, arg)
### \_\_n(singularString, pluralString, count, arg, arg, arg)

Print a localized string with appropriate pluralization. If `%d` is provided
in the string, the `count` will replace this placeholder.

### y18n.setLocale(str)
### setLocale(str)

Set the current locale being used.

### y18n.getLocale()
### getLocale()

What locale is currently being used?

### y18n.updateLocale(obj)
### updateLocale(obj)

Update the current locale with the key value pairs in `obj`.

Expand All @@ -123,5 +127,3 @@ ISC

[npm-url]: https://npmjs.org/package/y18n
[npm-image]: https://img.shields.io/npm/v/y18n.svg
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
[standard-url]: https://github.com/feross/standard
12 changes: 6 additions & 6 deletions deno.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { y18n as _y18n } from './build/lib/index.js'
import type { Y18NOpts } from './build/lib/index.d.ts'
import shim from './lib/platform-shims/deno.ts'
import {y18n as _y18n} from './build/lib/index.js';
import type {Y18NOpts} from './build/lib/index.d.ts';
import shim from './lib/platform-shims/deno.ts';

const y18n = (opts: Y18NOpts) => {
return _y18n(opts, shim)
}
return _y18n(opts, shim);
};

export default y18n
export default y18n;
14 changes: 8 additions & 6 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import shim from './build/lib/platform-shims/node.js'
import { y18n as _y18n } from './build/lib/index.js'
import shim from './build/lib/platform-shims/node.js';
import {y18n as _y18n} from './build/lib/index.js';

const y18n = (opts) => {
return _y18n(opts, shim)
}
const y18n = opts => {
return _y18n(opts, shim);
};

export default y18n
export default y18n;

export {y18n as 'module.exports'};
8 changes: 0 additions & 8 deletions lib/cjs.ts

This file was deleted.

Loading
Loading