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

@idapgroup/spreadsheet-localization-parser

v1.1.1

Published

Parse google spreadsheet to json i18n localization files.

Downloads

217

Readme

Google spreadsheet localization parser NPM Module

Parse google spreadsheet to json i18n localization files.

For auth and load sheets use google-spreadsheet package

Installation

With npm:

npm install @idapgroup/spreadsheet-localization-parser

or with yarn:s

yarn add @idapgroup/spreadsheet-localization-parser

Example

IMPORTANT NOTE - If you need to call await in a script at the root level, you must instead wrap it in an async function like so:

(async function() {
  await someAsyncFunction();
}());

Basic usage

import {GoogleSpreadsheet} from "google-spreadsheet";
import { JWT } from 'google-auth-library';

import {
  loadSheets,
  normalizeDynamicValue,
  parseSpreadsheets,
  writeTranslations
} from "@idapgroup/spreadsheet-localization-parser";

import creds from './config/myapp-1dd646d7c2af.json'; // the file saved above

const SCOPES = [
  'https://www.googleapis.com/auth/spreadsheets',
  'https://www.googleapis.com/auth/drive.file',
];

const jwt = new JWT({
  email: creds.client_email,
  key: creds.private_key,
  scopes: SCOPES,
});
const doc = new GoogleSpreadsheet('<YOUR-DOC-ID>', jwt);

/**
  * load all sheets or specific
  * @param {GoogleSpreadsheet} doc - initialized GoogleSpreadsheet instance 
  * @param {LoadSheetOptions} options - optional options for filter sheets by title, id or index
*/
const sheets = await loadSheets(doc)

// parser options
const parseOptions: ParseOptions = {
  keyColumnName: 'key', // key column name for parse(column name must be equal to this key)
  languages: ['en', 'de'], // languages for parse(column name must be equal to language)
  normalizeValue: normalizeDynamicValue, // optional callback for normalize dynamic value by you i18n lib rules
  keepEmpty: false, // optional keep empty values
}
/**
 * parse sheets
 * @param {GoogleSpreadsheetWorksheet[]} sheets - array of GoogleSpreadsheetWorksheet instances
 * @param {ParseOptions} options - parser options 
*/
const translations = await parseSpreadsheets(sheets, parseOptions);

/**
 * write translations to files
 * @param {string} path - path to directory for write files
 * @param {ParsedTranslations} translations - translations object
*/
writeTranslations(path, translations)

Legacy auth

For ^3.0.0 and below versions of google-spreadsheet;

import {GoogleSpreadsheet} from "google-spreadsheet";
import {loadSheets, parseSpreadsheets, writeTranslations, normalizeDynamicValue}  from 'spreadsheet-localization-parser'

import {
  loadSheets,
  normalizeDynamicValue,
  parseSpreadsheets,
  writeTranslations
} from "@idapgroup/spreadsheet-localization-parser";
/**
 * Initialize the sheet - doc ID is the long id in the sheets URL
 */
const doc = new GoogleSpreadsheet('<the sheet ID from the url>');

/**
 * Initialize Auth
 * @see  https://theoephraim.github.io/node-google-spreadsheet/#/getting-started/authentication
 */
await doc.useServiceAccountAuth({
  // env var values are copied from service account credentials generated by google
  // see "Authentication" section in docs for more info
  client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
  private_key: process.env.GOOGLE_PRIVATE_KEY, // **IMPORTANT NOTE** - replace escaped \n symbols to new line replace(/\\n/g, '\n')
});

More info about GoogleSpreadsheet and GoogleSpreadsheetWorksheet:

Normalize dynamic value

Transform string value with regexp pattern to dynamic value You can write your own normalize function for your i18n lib

import {normalizeDynamicValue} from "@idapgroup/spreadsheet-localization-parser";

normalizeDynamicValue('First %d days free') // output - First {{value}} days free

// use for parser with options
const options = {
  prefix: '{{',
  suffix: '}}',
  dynamicValueName: 'value',
}
parseSpreadsheets(sheets, {languages, keyColumnName, normalizeValue: (value) =>  normalizeDynamicValue(value, options)})