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

babelsheet-stream

v0.0.8

Published

Rx.js observable that parses google spreadsheets that have Babelsheet compliant structure

Downloads

3

Readme

babelsheet-stream

Rx.js observable that parses google spreadsheets that have Babelsheet-compliant structure

Purpose

There is cool Babelsheet project which enables straightforward way for syncing the translation files in your project by fetching them from specifically structured Google Spreadsheet file. The problem with Babelsheet however is that although it provides a nice features, it might be the case that you have a specific structure of translations that it does not handle. Possible approaches to such problem would be:

  1. Fork Babelsheet and create PR with missing feature implementation
  2. Use Babelsheet and format produced data to fit your format by writing another tool script
  3. Write custom script to parse spreadsheet

This project makes it easier to go with the 3rd way by using Rx.js abstractions.

Example: flat JSONs per language

Suppose you want to create flat JSON files per language:

import { fromBabelsheet, writeJSONFile } from 'babelsheet-stream';
import { mergeMap, groupBy } from 'rxjs/operators'

// Import your credentials.json file that you can generate in Google API console 
// panel when creating Service Account.
import credentials from './.credentials.json';

fromBabelsheet({
  spreadsheetId: "__YOUR_SPREADSHEET_ID__",
  credentials,
}).pipe(
  groupBy(
    ({ language }) => language,
    ({ path, value }) => ({ path: path.join('.'), value })
  ),
  mergeMap(languageEntries$ => languageEntries$.pipe(
    writeJSONFile(`${__dirname}/i18n/flat/${languageEntries$.key}.json`)
  )),
).subscribe(
  ({ filePath, entryCount }) => {
    console.log(`Wrote file: "${filePath}" with ${entryCount} entries`);
  }
);

The above script will group the language translations by language and write JSON files per each of them. As you can see beside reading and parsing the spreadsheets, babelsheet-stream also provides some helper for writing the JSON files.

Example: nested JSON structure

Suppose you want to group translations per language, but you also want to split the translations by the root section of the translation key path. For instance if you have translation keys like this:

  • common.edit
  • login.form.email
  • login.error.wrongCredentials

you would like it to end up in files like:

  • i18n/en/common.json
  • i18n/pl/common.json
  • i18n/en/login.json
  • i18n/pl/login.json

Then, the following code is an exemplary implementation of tool that can do this:

import { fromBabelsheet, writeJSONFile } from 'babelsheet-stream';
import { groupBy, mergeMap } from 'rxjs/operators'

// Import your credentials.json file that you can generate in Google API console 
// panel when creating Service Account.
import credentials from './.credentials.json';

fromBabelsheet({
  spreadsheetId: "__YOUR_SPREADSHEET_ID__",
  credentials,
}).pipe(
  groupBy(
    ({ language, path }) => `${language}/${path[0]}`,
    ({ path, value }) => ({ path: path.slice(1), value })
  ),
  mergeMap(languageEntries$ => languageEntries$.pipe(
    writeJSONFile(`${__dirname}/i18n/nested/${languageEntries$.key}.json`)
  )),
).subscribe(
  ({ filePath, entryCount }) => {
    console.log(`Wrote file: "${filePath}" with ${entryCount} entries`);
  }
);

Example: CSV

import { fromBabelsheet, writeCSVFile } from 'babelsheet-stream';
import { mergeMap, groupBy } from 'rxjs/operators'

// Import your credentials.json file that you can generate in Google API console 
// panel when creating Service Account.
import credentials from './.credentials.json';

fromBabelsheet({
  spreadsheetId: "10LiCKh8KRmFUQUHqMgcx70THb1xprYp5HdyQR_6zcBY",
  credentials,
}).pipe(
  groupBy(
    ({ language }) => language,
    ({ path, value }) => ({ translationKey: path.join('.'), value })
  ),
  mergeMap(languageEntries$ => languageEntries$.pipe(
    writeCSVFile({
      filePath: `${__dirname}/i18n/csv/${languageEntries$.key}.csv`,
      columnsOrder: ["translationKey", "value"],
    })
  )),
).subscribe(
  ({ filePath, entryCount }) => {
    console.log(`Wrote file: "${filePath}" with ${entryCount} entries`);
  }
);