Localization tool for nodejs apps. Uses google-sheet as a resource storage.
npm install google-sheet-localize-test1Localization tool for nodejs apps. Uses google-sheet as a resource storage.
This is a Node.js module available through the
npm registry. It can be installed using the npm
``sh`
npm install google-sheet-localize-test1 --save
Important: Create your sheet for localization with following structure:
| Section | Field | EN | SE |
| ------------- | ------------- | --------- | --------- |
| XXX | YYY | ZZZ | ZZZ |
| Header | Title | Hello | Hallå |
| Content | Description | World | Värld |
Important: To enable access to your sheet by api, please turn on access by link:
1. Click 'Share' button in the right top side.
2. Then choose 'Anyone with the link' and hit 'Done'.
!google sheet enabling access
Example of google sheet:
You can copy data for testing from example table provided by link.
!google sheet document example
Don't forget change tab name to "tab_name_of_sheet_with_translations" for applying our example to work.
Example of configuring for expressjs(Be sure you have installed nodejs):
1. Create new folder localize-test and init project. Provide info of your project in dialogue.
`sh`
npm init
2. Install "express" web app framework and "express-handlebars" view engine npm modules for our example.
`sh`
npm install express
npm install express-handlebars
And of course:
`sh`
npm install google-sheet-localize-test1 --save
3. Create "app.js" file in root folder of project.
app.js
`js
var express = require('express'),
exphbs = require('express-handlebars'),
path = require('path'),
loc = require('google-sheet-localize-test1');
var app = express();
// loc.synchronize() performs downloading translations from the google sheet
// it must be called before we try to localize something!
loc.synchronize();
app.engine('.html', exphbs({
extname: '.html'
}));
app.set('view engine', '.html');
//Serving static files in Express
//details info: https://expressjs.com/en/starter/static-files.html
app.set('views', path.join(__dirname,'/'));
app.get('/sync', (req, res) => {
loc.synchronize();
res.sendStatus(200);
});
app.get('/:lang?', (req, res) => {
res.render('start_page', (err, html) => {
// loc.localize(...) performs localization of document html content
// and returns it as a promise
loc.localize(html, req.params.lang, 'start_page')
.then(localizedHtml => res.send(localizedHtml));
})
});
app.listen(3000);
`
4. Create "start_page.html" in root folder of project.
start_page.html ((Content | Description))
`html`
((XXX | YYY]))
((Header | Title))
Where:
- XXX: name of section in google sheet.
- YYY: name of field in google sheet.
5. Finally create your "localize-config.json" in root folder of project.
localize-config.json
`json`
{
"pages": [
{
"sheetId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"tabName": "tab_name_of_sheet_with_translations",
"pageName": "start_page"
}
],
"apiKey": "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY",
"apiVersion": "v4",
"defaultLanguage": "EN"
}
6. Read configuring section to get 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
and 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
to spicify in our localize-config.json.
7. Write in terminal(in project folder):
`sh`
node app.js
8. Open in browser http://localhost:3000/se and see our translated page with content from created google sheet, yey!
9. Open in browser http://localhost:3000/ and see our translated page to default "EN"!
10. Open in browser http://localhost:3000/sync to synchronize your changes from Google Sheet any time.
For applying tool to work create json config file, with following content:
localize-config.json
`json`
{
"pages": [
{
"sheetId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"tabName": "tab_name_of_sheet_with_translations",
"pageName": "page_name_in_app"
},
{
"sheetId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"tabName": "another_tab_name_of_sheet_with_translations",
"pageName": "another_page_name_in_app"
}
],
"apiKey": "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY",
"apiVersion": "v4",
"defaultLanguage": "EN"
}
Where:
- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX: id of google sheet.
You can take it from google sheet basic url:
```
https://docs.google.com/spreadsheets/d/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/edit#gid=000000000
- YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY: key for accessing your google api.
You can create it very simple:
1. Log in your google account.
2. Go to https://console.developers.google.com/
3. Create new project or select existing:
Creating project:
a. Highlight 'IAM & admin' and select 'Manage resources' in left menu.
b. Click 'Create project button' fill 'Project name' and hit 'Create'.
!creating test project
4. Enable "Google Sheets Api" by following link and clicking "Enable".
5. Select 'Credentials' in left menu.
Be sure your new project selected:
6. Click 'Create credentials' and select 'API Key'.
- tabName (optional) is tab name from your document.
- apiVersion is google api version set it to "v4".
- defaultLanguage (optional) is language wich must be rendered when requested non-existing or empty language.
- fs-extra: fs-extra contains methods that aren't included in the vanilla Node.js fs package. Such as mkdir -p, cp -r, and rm -rf.
- request: Simplified HTTP request client.
- request-promise: The simplified HTTP request client 'request' with Promise support. Powered by Bluebird.
The MIT license
Copyright © 2018 Vladyslav Behashevskyi (vlad.begashevskiy@gmail.com)