Skip to content

Commit b92d2f3

Browse files
committed
feat: require node >=14, bump deps, redis is now optional, setup github actions
1 parent df7770f commit b92d2f3

21 files changed

+163
-8262
lines changed

.commitlintrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['@commitlint/config-conventional']
3+
};

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!.*.js

.gitattributes

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* text=auto
1+
* text=auto eol=lf

.github/FUNDING.yml

-4
This file was deleted.

.github/scripts/decrypt_secret.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
3+
# Decrypt the file
4+
mkdir $HOME/secrets
5+
# --batch to prevent interactive command
6+
# --yes to assume "yes" for questions
7+
gpg --quiet --batch --yes --decrypt --passphrase="$LARGE_SECRET_PASSPHRASE" \
8+
--output $HOME/secrets/my_secret.json .github/secrets/my_secret.json.gpg
1.69 KB
Binary file not shown.

.github/workflows/ci.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: CI
2+
on:
3+
- push
4+
- pull_request
5+
jobs:
6+
build:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
os:
11+
- ubuntu-latest
12+
node_version:
13+
- 14
14+
- 16
15+
- 18
16+
name: Node ${{ matrix.node_version }} on ${{ matrix.os }}
17+
steps:
18+
- uses: actions/checkout@v3
19+
- name: Decrypt large secret
20+
run: ./.github/scripts/decrypt_secret.sh
21+
env:
22+
LARGE_SECRET_PASSPHRASE: ${{ secrets.LARGE_SECRET_PASSPHRASE }}
23+
- name: Setup node
24+
uses: actions/setup-node@v3
25+
with:
26+
node-version: ${{ matrix.node_version }}
27+
- name: Install dependencies
28+
run: npm install
29+
- name: Run tests
30+
env:
31+
GOOGLE_APPLICATION_CREDENTIALS: $HOME/secrets/my_secret.json
32+
run: npm run test

.gitignore

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,12 @@
44
node_modules
55
coverage
66
.nyc_output
7-
locales
7+
locales/
88
package-lock.json
9+
yarn.lock
10+
11+
Thumbs.db
12+
tmp/
13+
temp/
14+
*.lcov
15+
.env

.husky/commit-msg

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx --no-install commitlint --edit $1

.husky/pre-commit

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx --no-install lint-staged && npm test

.lintstagedrc.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
"*.md": filenames => filenames.map(filename => `remark ${filename} -qfo`),
3+
'package.json': 'fixpack',
4+
'*.js': 'xo --fix'
5+
};

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.prettierrc.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
singleQuote: true,
3+
bracketSpacing: true,
4+
trailingComma: 'none'
5+
};

.remarkrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
plugins: ['preset-github']
3+
};

.travis.yml

-13
This file was deleted.

.xo-config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
prettier: true,
3+
space: true,
4+
extends: ['xo-lass']
5+
};

README.md

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
# mandarin
22

3-
[![build status](https://img.shields.io/travis/niftylettuce/mandarin.svg)](https://travis-ci.org/niftylettuce/mandarin)
4-
[![code coverage](https://img.shields.io/codecov/c/github/niftylettuce/mandarin.svg)](https://codecov.io/gh/niftylettuce/mandarin)
3+
[![build status](https://github.com/ladjs/mandarin/actions/workflows/ci.yml/badge.svg)](https://github.com/ladjs/mandarin/actions/workflows/ci.yml)
54
[![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
65
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
76
[![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://lass.js.org)
8-
[![license](https://img.shields.io/github/license/niftylettuce/mandarin.svg)](LICENSE)
7+
[![license](https://img.shields.io/github/license/ladjs/mandarin.svg)](LICENSE)
98

109
> Automatic i18n markdown translation and i18n phrase translation using Google Translate
1110
12-
> **v2.0.0**: We have integrated Redis as a requirement for caching Google Translate responses (to save you money)
13-
>
14-
> **v1.0.0**: We now use the official Node.js Google Translate package `@google-cloud/translate`
15-
1611

1712
## Table of Contents
1813

1914
* [Install](#install)
2015
* [Requirements](#requirements)
16+
* [Redis](#redis)
17+
* [Google Application Credentials](#google-application-credentials)
2118
* [Usage](#usage)
2219
* [Contributors](#contributors)
2320
* [License](#license)
@@ -31,16 +28,20 @@
3128
npm install mandarin
3229
```
3330

34-
[yarn][]:
3531

36-
```sh
37-
yarn add mandarin
38-
```
32+
## Requirements
3933

34+
### Redis
4035

41-
## Requirements
36+
You will need to have [Redis][] installed in order for caching to work properly.
4237

43-
You will need to have [Redis][] installed.
38+
If you do not plan to use Redis, then set `redis: false` as an option.
39+
40+
### Google Application Credentials
41+
42+
You will also need Google Application Credentials, and you will need to set them as environment variables (e.g. `GOOGLE_APPLICATION_CREDENTIALS=/home/user/Downloads/service-account-file.json`).
43+
44+
For more information on Google Application credentials, see <https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable>.
4445

4546

4647
## Usage
@@ -162,8 +163,6 @@ GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/[FILE_NAME].json" node app.
162163

163164
[npm]: https://www.npmjs.com/
164165

165-
[yarn]: https://yarnpkg.com/
166-
167166
[i18n]: https://github.com/ladjs/i18n
168167

169168
[redis]: https://redis.io/

client_secrets.json.enc

-2.25 KB
Binary file not shown.

index.js

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const path = require('path');
21
const fs = require('fs');
2+
const path = require('path');
3+
const process = require('process');
34

45
// const formatSpecifiers = require('format-specifiers');
56
const Redis = require('@ladjs/redis');
@@ -57,6 +58,9 @@ class Mandarin {
5758
}),
5859
i18n: false,
5960
//
61+
// NOTE: you can pass `GOOGLE_APPLICATION_CREDENTIALS` as an environment variable
62+
// or you can pass individual environment variables
63+
//
6064
// OPTIONAL:
6165
// see all commented options from this following link:
6266
// https://googleapis.dev/nodejs/translate/5.0.1/v2_index.js.html
@@ -81,12 +85,17 @@ class Mandarin {
8185

8286
if (!this.config.i18n) throw new Error('i18n instance option required');
8387

84-
// setup redis
85-
this.redisClient = new Redis(
86-
this.config.redis,
87-
this.config.logger,
88-
this.config.redisMonitor
89-
);
88+
// initialize redis
89+
this.redisClient =
90+
this.config.redis === false
91+
? false
92+
: _.isPlainObject(this.config.redis)
93+
? new Redis(
94+
this.config.redis,
95+
this.config.logger,
96+
this.config.redisMonitor
97+
)
98+
: this.config.redis;
9099

91100
// setup google translate with api key
92101
this.client = new v2.Translate(this.config.clientConfig);
@@ -214,9 +223,10 @@ class Mandarin {
214223
if (locale === i18n.config.defaultLocale) return file;
215224

216225
const translationsRequired = _.intersection(
217-
_.uniq(
218-
_.concat(_.values(i18n.config.phrases), _.values(defaultLocaleFile))
219-
),
226+
_.uniq([
227+
..._.values(i18n.config.phrases),
228+
..._.values(defaultLocaleFile)
229+
]),
220230
_.values(file)
221231
);
222232

@@ -251,15 +261,16 @@ class Mandarin {
251261

252262
// lookup translation result from cache
253263
const key = `${locale}:${revHash(phrase)}`;
254-
let translation = await this.redisClient.get(key);
264+
let translation;
265+
if (this.redisClient) translation = await this.redisClient.get(key);
255266
debug('translation', translation);
256267

257268
// get the translation results from Google
258269
if (!_.isString(translation)) {
259270
debug('getting translation', key);
260271
[translation] = await this.client.translate(safePhrase, locale);
261272
debug('got translation', translation);
262-
await this.redisClient.set(key, translation);
273+
if (this.redisClient) await this.redisClient.set(key, translation);
263274
}
264275

265276
// replace `|` pipe character because translation will

0 commit comments

Comments
 (0)