google-sheet-i18n
v0.1.0
Published
i18n using google sheets
Downloads
4
Maintainers
Readme
Introduction
google-sheet-i18n
provides an way for translations files to be generated using google sheets.
Setup
Google Sheet Configuration
Organize translations in such a way that each row will be configured like so
The first row will be used as the categories, and the rest will be the i18n keys as follows,
Table
| Category | SubCategory | SubCategory2 | .... | en_CA | en_FR | ... | | :------: | :---------: | :----------: | :--: | :---: | :---: | :-: | | onboarding | landing | intro | ... | Hello! | Bonjour! | ... | | onboarding | landing | exit | ... | Good bye! | Au revior! | ... |
CLI Configuration
Configuration requires a credentials JSON file that can be obtained through Google's developer console with the following instructions ( Some instructions borrowed from node-google-spreadsheet )
- Go to the Google Developers Console
- Select your project or create a new one (and then select it)
- Enable the Drive API for your project
- In the sidebar on the left, expand APIs & auth > APIs
- Search for "drive"
- Click on "Drive API"
- click the blue "Enable API" button
- Create a service account for your project
- In the sidebar on the left, expand APIs & auth > Credentials
- Click blue "Add credentials" button
- Select the "Service account" option
- Select the "JSON" key type option
- Click blue "Create" button
- your JSON key file is generated and downloaded to your machine (it is the only copy!)
- note your service account's email address (also available in the JSON key file)
- Share the doc (or docs) with your service account using the email noted above
Running the Translations
npm install -g google-sheet-i18n
- Create a
i18n.config.js
file in the folder you wish to run the translations on. i18n start
i18n Configuration
var path = require('path')
module.exports = {
categories: ['category', 'subcategory', 'subcategory2'],
credentialsPath: path.join(process.cwd(), 'credentials.json'),
languages: ['en_CA', 'fr_CA', 'es_ES'],
sheetId: 'YOUR_SHEET_ID',
outputs: [
{
name: 'Angular.JS',
outPath: path.join(process.cwd(), './client/i18n'),
mapper: myAngularMapper,
concat: true,
},
{
name: 'PHP',
outPath: path.join(process.cwd(), './api/i18n'),
suffix: '_lang',
preset: 'php',
}
],
}
categories
- (required) Different columns used to create a translation key, ie.onboarding.landing.intro
is created the table above when generating i18n filesdelimiter
- (optional, default - '.') The delimiter used to join different categories to created a translation keycredentialsPath
- (required) Absolute path to your crendentials JSON filelanguages
: (required) Specify the column on the sheets we want to use as the i18n values, Note: this will also be the name of the folders in which the translations will be outputtedsheetId
- (required) The ID of the Google Sheet document that you want to useoutput
- (required) The specified file outputsoutPath
- (required) Absolute path to output your file(s)concat
- (optional) If enabled, each sheet will not have it's own file, but will be concatenated, thus, creating one file per language.preset
- (optional) Predefined templates to format each row in accordance to specification. Current presets consists of:php
andjson
prefix
- (optional) As the name of each worksheet will correspond to the name of the file, ie.data
sheet will output asdata.someExtension
, prefix allows for users to define a prefix, such assomePrefix-data.someExtension
suffix
- (optional) Identical to prefix, except for the fact that it will be for suffixmapper
- (optional) Used to define own row mappings
Creating your own mapper
A mapper consists of 2 different parts,
fileMapper
- a function that takes in an array of rows each consisting of akey
and adata
and returns a string to be written to the a filefileExtension
- the type of extension that will be appended at the end of the file name
Example:
var fileMapper = function fileMapper(rows, language) {
var output = rows.reduce(function (outputSoFar, row, index, array) {
outputSoFar[row.key] = row.data
return outputSoFar
}, {})
return '(function() {angular.module(\'translations\').constant(\'' + language + '\', ' + JSON.stringify(output) + ')\n})();\n'
}
var myAngularMapper = {
fileMapper: fileMapper,
fileExtension: '.js',
}