npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

google-sheet-i18n

v0.1.0

Published

i18n using google sheets

Downloads

4

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 )

  1. Go to the Google Developers Console
  2. Select your project or create a new one (and then select it)
  3. 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
  1. 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)
  1. Share the doc (or docs) with your service account using the email noted above

Running the Translations

  1. npm install -g google-sheet-i18n
  2. Create a i18n.config.js file in the folder you wish to run the translations on.
  3. 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 files

  • delimiter - (optional, default - '.') The delimiter used to join different categories to created a translation key

  • credentialsPath - (required) Absolute path to your crendentials JSON file

  • languages: (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 outputted

  • sheetId - (required) The ID of the Google Sheet document that you want to use

  • output - (required) The specified file outputs

    • outPath - (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 and json

    • prefix - (optional) As the name of each worksheet will correspond to the name of the file, ie. data sheet will output as data.someExtension, prefix allows for users to define a prefix, such as somePrefix-data.someExtension

    • suffix - (optional) Identical to prefix, except for the fact that it will be for suffix

    • mapper - (optional) Used to define own row mappings

Creating your own mapper

A mapper consists of 2 different parts,

  1. fileMapper - a function that takes in an array of rows each consisting of a key and a data and returns a string to be written to the a file

  2. fileExtension - 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',
}