Comparing version 0.0.0 to 1.0.0
{ | ||
"name": "auto-i18n", | ||
"version": "0.0.0", | ||
"main": "index.js", | ||
"version": "1.0.0", | ||
"main": "dist/index.js", | ||
"files": [ | ||
"dist" | ||
], | ||
"types": "dist/index.d.ts", | ||
"repository": "git@github.com:AnandChowdhary/auto-i18n", | ||
"author": "Anand Chowdhary <anandchowdhary@gmail.com>", | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "tsc", | ||
"build-test": "yarn build && yarn test-without-coverage", | ||
"start": "concurrently 'yarn prettier-watch' 'yarn watch'", | ||
"watch": "onchange '*.{ts,json}' -- yarn build-test", | ||
"prettier-watch": "onchange '**/*.{ts,json}' -- prettier --write {{changed}}", | ||
"test": "jest --coverage --coverageReporters=text-lcov | coveralls", | ||
"test-without-reporting": "jest --coverage", | ||
"test-without-coverage": "jest" | ||
}, | ||
"devDependencies": { | ||
"@types/dot-object": "^1.7.0", | ||
"@types/dotenv": "^6.1.0", | ||
"@types/fs-extra": "^5.0.5", | ||
"@types/jest": "^24.0.11", | ||
"@types/node": "^11.11.3", | ||
"concurrently": "^4.1.0", | ||
"coveralls": "^3.0.3", | ||
"dotenv": "^7.0.0", | ||
"jest": "^24.5.0", | ||
@@ -18,3 +37,25 @@ "onchange": "^5.2.0", | ||
"typescript": "^3.3.3333" | ||
}, | ||
"jest": { | ||
"roots": [ | ||
"<rootDir>" | ||
], | ||
"transform": { | ||
"^.+\\.tsx?$": "ts-jest" | ||
}, | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"jsx", | ||
"json", | ||
"node" | ||
] | ||
}, | ||
"dependencies": { | ||
"@google-cloud/translate": "^2.1.4", | ||
"dot-object": "^1.7.1", | ||
"fraud": "^5.0.0", | ||
"fs-extra": "^7.0.1" | ||
} | ||
} |
178
README.md
@@ -1,1 +0,177 @@ | ||
# 🌐 auto-i18n | ||
# 🌐 Auto I18N | ||
[![Travis CI](https://img.shields.io/travis/AnandChowdhary/auto-i18n.svg)](https://travis-ci.org/AnandChowdhary/auto-i18n) | ||
[![Coverage Status](https://coveralls.io/repos/github/AnandChowdhary/auto-i18n/badge.svg?branch=master)](https://coveralls.io/github/AnandChowdhary/auto-i18n?branch=master) | ||
[![GitHub](https://img.shields.io/github/license/anandchowdhary/auto-i18n.svg)](https://github.com/AnandChowdhary/auto-i18n/blob/master/LICENSE) | ||
![Vulnerabilities](https://img.shields.io/snyk/vulnerabilities/github/AnandChowdhary/auto-i18n.svg) | ||
![NPM type definitions](https://img.shields.io/npm/types/auto-i18n.svg) | ||
[![NPM](https://img.shields.io/npm/v/auto-i18n.svg)](https://www.npmjs.com/package/auto-i18n) | ||
[![NPM](https://nodei.co/npm/auto-i18n.png)](https://www.npmjs.com/package/auto-i18n) | ||
Auto I18N is a package which automagically translate your JSON files, objects, or strings using Google Translate. It's automating your internationalization. | ||
## ⭐ Usage | ||
Add the dependency from NPM: | ||
```bash | ||
npm install auto-i18n | ||
``` | ||
Import the modules you require from the package: | ||
```js | ||
import * from "auto-i18n"; | ||
``` | ||
And then configure your Project ID and API key as environment variables (see [Configuration](#configuration)). | ||
### Translating words | ||
To translate a single phrase: | ||
```js | ||
import { translate } from "auto-i18n"; | ||
const hello = await translate("Hello!", "fr"); // Bonjour! | ||
``` | ||
For every function, can also use promises: | ||
```js | ||
translate("Hello!", "nl") | ||
.then(translation => { | ||
console.log(translation); // Hallo! | ||
}) | ||
.catch(error => { | ||
console.log("Got an error", error); | ||
}); | ||
``` | ||
### Translating objects | ||
To translate an entire object: | ||
```js | ||
import { translateObject } from "auto-i18n"; | ||
const translateMe = { | ||
hello: "Hello", | ||
world: ["world", "earth"] | ||
}; | ||
const translated = await translateObject(translateMe, "es"); | ||
console.log(translated); | ||
/* { hello: "Hola", | ||
world: ["mundo", "tierra"] } */ | ||
``` | ||
### Translating a single-translation file | ||
A single translation file is a JSON file which contains keys for language codes and terms under each key. You can write one for English like this, for example: | ||
File `en.json`: | ||
```json | ||
{ | ||
"en": { | ||
"greeting": "Hello!", | ||
"question": "How are you?" | ||
} | ||
} | ||
``` | ||
To translate it, use the `translateFileSingle` function: | ||
```js | ||
import { translateFileSingle } from "auto-i18n"; | ||
import path from "path"; // Node.js path helper | ||
const filePath = path.join(__dirname, "en.json"); | ||
const translated = await translateFileSingle(filePath, ["fr", "nl", "es"]); | ||
console.log(translated); | ||
``` | ||
This is what `translated` looks like: | ||
```json | ||
{ | ||
"en": { | ||
"greeting": "Hello!", | ||
"question": "How are you?" | ||
}, | ||
"fr": { | ||
"greeting": "Bonjour!", | ||
"question": "Comment vas-tu?" | ||
}, | ||
"nl": { | ||
"greeting": "Hallo!", | ||
"question": "Hoe gaat het met je?" | ||
}, | ||
"es": { | ||
"greeting": "Hola!", | ||
"question": "¿Cómo estás?" | ||
} | ||
} | ||
``` | ||
You can also write the file directly by supplying the third parameter as `true`: | ||
```js | ||
await translateFileSingle(filePath, ["fr", "nl", "es"], true); | ||
``` | ||
### Translation a regular JSON file | ||
If you have a JSON file which is not a single-translation-file, you can also translate it: | ||
```js | ||
await translateFile( | ||
"en.json", /* File path */ | ||
"nl", /* Language code (single) */ | ||
false /* Overwrite the file with translation? */ | ||
); | ||
``` | ||
### Generated translated files | ||
If you have a JSON file (e.g., `en.json`), you can also generate corresponding language files: | ||
```js | ||
await generate( | ||
"en.json", /* File path */ | ||
["nl", "fr", "es"], /* Languages */ | ||
); | ||
``` | ||
## 💡 Notes | ||
### Caching | ||
Auto I18N uses local caching powered by [Fraud](https://github.com/AnandChowdhary/fraud) to decrease API usage and therefore billing. A caching directory, `.cache/auto-i18n` is used, which should be added to your `.gitignore`. You can overwrite this directory as a parameter in each function. | ||
### Configuration | ||
Auto I18N uses the Google Cloud Translation API, for which an API key and Project ID are required. These should be available as environment variables in your `.env` file. For example: | ||
```dotenv | ||
PROJECT_ID = "google-cloud-project-id" | ||
API_KEY = "google-cloud-api-key" | ||
``` | ||
The library will automatically read them from the `.env` file, as long as it's in your project root directory. | ||
## 🛠️ Development | ||
Install dependencies: | ||
```bash | ||
yarn | ||
``` | ||
Compile Typescript to ES6 before publishing to NPM: | ||
```bash | ||
yarn build | ||
``` | ||
## 📝 License | ||
MIT |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9
217
1
178
16346
4
14
4
1
+ Addeddot-object@^1.7.1
+ Addedfraud@^5.0.0
+ Addedfs-extra@^7.0.1
+ Added@google-cloud/common@0.31.1(transitive)
+ Added@google-cloud/projectify@0.3.3(transitive)
+ Added@google-cloud/promisify@0.4.0(transitive)
+ Added@google-cloud/translate@2.1.4(transitive)
+ Added@types/caseless@0.12.5(transitive)
+ Added@types/duplexify@3.6.4(transitive)
+ Added@types/node@22.13.1(transitive)
+ Added@types/request@2.48.12(transitive)
+ Added@types/tough-cookie@4.0.5(transitive)
+ Addedabort-controller@3.0.0(transitive)
+ Addedagent-base@4.3.0(transitive)
+ Addedarrify@1.0.1(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbignumber.js@9.1.2(transitive)
+ Addedbluebird@3.7.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbuffer-equal-constant-time@1.0.1(transitive)
+ Addedcall-bind-apply-helpers@1.0.1(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addedclone@2.1.2(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcommander@2.20.3(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddebug@3.2.74.4.0(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addeddot-object@1.9.0(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedduplexify@3.7.1(transitive)
+ Addedecdsa-sig-formatter@1.0.11(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedent@2.2.2(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.1.1(transitive)
+ Addedes6-promise@4.2.8(transitive)
+ Addedes6-promisify@5.0.0(transitive)
+ Addedevent-target-shim@5.0.1(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedfast-text-encoding@1.0.6(transitive)
+ Addedform-data@2.5.2(transitive)
+ Addedfraud@5.2.1(transitive)
+ Addedfs-extra@7.0.1(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedgaxios@1.8.4(transitive)
+ Addedgcp-metadata@1.0.0(transitive)
+ Addedget-intrinsic@1.2.7(transitive)
+ Addedget-proto@1.0.1(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedgoogle-auth-library@3.1.2(transitive)
+ Addedgoogle-p12-pem@1.0.5(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedgtoken@2.3.3(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhtml-tags@1.2.0(transitive)
+ Addedhttps-proxy-agent@2.2.4(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis@3.3.0(transitive)
+ Addedis-html@1.1.0(transitive)
+ Addedis-regex@1.2.1(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedjson-bigint@0.3.1(transitive)
+ Addedjsonfile@4.0.0(transitive)
+ Addedjwa@1.4.1(transitive)
+ Addedjws@3.2.2(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedlru-cache@5.1.1(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedmime@2.6.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedms@2.1.3(transitive)
+ Addednode-cache@4.2.1(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addednode-forge@0.10.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpify@4.0.1(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedpromise-serial@0.1.5(transitive)
+ Addedpunycode@1.4.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedretry-request@4.2.2(transitive)
+ Addedsafe-buffer@5.1.25.2.1(transitive)
+ Addedsafe-regex-test@1.1.0(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedstream-shift@1.0.3(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedteeny-request@3.11.3(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addedundici-types@6.20.0(transitive)
+ Addeduniversalify@0.1.2(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedyallist@3.1.1(transitive)