google-sheet-localize-test1
v1.0.20
Published
Localization tool for nodejs apps. Uses google-sheet as a resource storage.
Downloads
4
Readme
google-sheet-localize-test1
Localization tool for nodejs apps. Uses google-sheet as a resource storage.
Installation
This is a Node.js module available through the
npm registry. It can be installed using the
npm
npm install google-sheet-localize-test1 --save
Usage
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:
- Click 'Share' button in the right top side.
- Then choose 'Anyone with the link' and hit 'Done'.
Example of google sheet:
You can copy data for testing from example table provided by link.
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):
Create new folder localize-test and init project. Provide info of your project in dialogue.
npm init
Install "express" web app framework and "express-handlebars" view engine npm modules for our example.
npm install express npm install express-handlebars
And of course:
npm install google-sheet-localize-test1 --save
Create "app.js" file in root folder of project.
app.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);
Create "start_page.html" in root folder of project.
start_page.html
<html> <h1>((XXX | YYY]))</h1> <h2>((Header | Title))</h2> <p>((Content | Description))</p> </html>
Where:
- XXX: name of section in google sheet.
- YYY: name of field in google sheet.
Finally create your "localize-config.json" in root folder of project.
localize-config.json
{ "pages": [ { "sheetId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "tabName": "tab_name_of_sheet_with_translations", "pageName": "start_page" } ], "apiKey": "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY", "apiVersion": "v4", "defaultLanguage": "EN" }
Read configuring section to get 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
and 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY' to spicify in our localize-config.json.Write in terminal(in project folder):
node app.js
Open in browser http://localhost:3000/se and see our translated page with content from created google sheet, yey!
Open in browser http://localhost:3000/ and see our translated page to default "EN"!
Open in browser http://localhost:3000/sync to synchronize your changes from Google Sheet any time.
Configuring
For applying tool to work create json config file, with following content:
localize-config.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:
Log in your google account.
Go to https://console.developers.google.com/
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'.
Enable "Google Sheets Api" by following link and clicking "Enable".
Select 'Credentials' in left menu. Be sure your new project selected:
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.
Dependencies
- 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.
License
The MIT license
Copyright © 2018 Vladyslav Behashevskyi ([email protected])