-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First rewritten version that works both in the Browser and in Node.js…
… using Promises. No Google API glue yet
- Loading branch information
1 parent
c5f86f0
commit 8274416
Showing
23 changed files
with
871 additions
and
6,728 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"presets": ["es2015", "es2016", "es2017"], | ||
"plugins": ["transform-runtime"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2016 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Translate | ||
|
||
Translate text to different languages on node.js and the browser. Flexible module and powerful back-end using Google Translate: | ||
|
||
```js | ||
translate('Hello world', { to: 'es' }).then(text => { | ||
console.log(text); // Hola mundo | ||
}); | ||
``` | ||
|
||
Example with an async workflow: | ||
|
||
```js | ||
const whatever = async () => { | ||
const text = await translate('Hello world', { to: 'es' }); | ||
console.log(text); // Hola mundo | ||
}; | ||
``` | ||
|
||
|
||
|
||
## Getting started | ||
|
||
This package can be used either in Node.js and on the browser. For the browser we are using `fetch`, so you might want to [polyfill it](https://polyfill.io/v2/docs/) depending on [the browsers you support](https://caniuse.com/#feat=fetch). | ||
|
||
To use it in `node.js` first install it: | ||
|
||
```bash | ||
npm install translate | ||
``` | ||
|
||
Then import it to use it: | ||
|
||
```js | ||
const translate = require('translate'); | ||
``` | ||
|
||
To use it in the browser you download the main `translate.min.js` file and include it: | ||
|
||
```html | ||
<script src="translate.min.js"></script> | ||
``` | ||
|
||
Or use the awesome [unpkg](https://unpkg.com/) **CDN**: | ||
|
||
```html | ||
<script src="https://unpkg.com/translate@1"></script> | ||
``` | ||
|
||
After including it with either of those methods, the usage is the same for both of them afterwards. | ||
|
||
|
||
|
||
## Parameters | ||
|
||
The first parameter is the **string** that you want to translate. Right now only a single string of text is accepted. | ||
|
||
The second parameter is the options. It accepts either a `String` of the language to translate **to** or a simple `Object`. | ||
|
||
The full options are: | ||
|
||
- **`to`**: the string of the language to translate to. It can be in any of the two ISO 639 (1 or 2) or the full name in English like `Spanish`. Defaults to **en**. | ||
- **`from`**: the string of the language to translate to. It can be in any of the two ISO 639 (1 or 2) or the full name in English like `Spanish`. Also defaults to **en**. | ||
- More options to come like API key, translation files, cache, etc. | ||
|
||
Examples: | ||
|
||
```js | ||
// Translate from English (default) to Spanish (specified) | ||
const foo = await translate('Hello world', 'es'); | ||
|
||
// Same as this: | ||
const bar = await translate('Hello world', { to: 'es' }); | ||
``` | ||
|
||
> On both `to` and `from` defaulting to `en`: while I _am_ Spanish and was quite tempted to set this as one of those, English is the main language of the Internet and the main secondary language for those who have a different native language. This is why most of the translations will happen either to or from English. | ||
You can also change the default `from` language for all of your codebase, like if your main language is different from English: | ||
|
||
```js | ||
translate.from = 'es'; | ||
``` | ||
|
||
|
||
|
||
## Promises | ||
|
||
Working with Promises and specially with [async/await](https://ponyfoo.com/articles/understanding-javascript-async-await) is really great since it reduces [Callback Hell](http://callbackhell.com/) quite a bit. | ||
|
||
To see it in action, first you'll need an `async` function. Then put your `await` calls inside: | ||
|
||
```js | ||
// Browser; jQuery for demonstration purposes | ||
$('#translate').submit(async e => { | ||
e.preventDefault(); | ||
const text = $('.text').text(); | ||
const spanish = await translate(text, { to: 'es' }); | ||
alert(spanish); | ||
}); | ||
|
||
// Node.js; serverjs.io style middleware for demonstration purposes | ||
const route = async ctx => { | ||
const spanish = await translate(ctx.body, { to: 'es' }); | ||
return send(spanish); | ||
}; | ||
``` | ||
|
||
|
||
|
||
## Authors | ||
|
||
Current package and development: [Francisco Presencia](https://francisco.io/) | ||
|
||
Original package and idea: Andrew Lunny (alunny), Marak Squires, Google |
Oops, something went wrong.