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

clak

v1.0.2

Published

parses csv values to return internationalized messages

Downloads

218

Readme

A "nutshell" example, using clak in 4 steps,

// 1. memoize the csv file,
// 2. analyze and persist the precedence, order and position of each locale in at csv file,
// 3. define lazy-lookup functions with default values for each key,
// 4. resolve an i18n value, calling the lazy-lookup function with a locale

const c = clak(csv) // memoize
const langs = c(['en-US','ja-JP']) // analyze and persist column position
const access_denied = c('access_denied', 'no access') // lazy-lookup for key
clak(access_denied, langs, ['ja-JP']) // 'あなたが入れない駄目です'

A longer example with details for each step,

// Supports both two and three letter language id ex, 'en' or 'eng'
const csv = `
"id","key","en-US","ja-JP"
1,"forbidden","you are forbidden","あなたが駄目です"
2,"upstream_error","upstream error","それが駄目です"
3,"access_denied","access denied","あなたが入れない駄目です"
`.slice(1, -1)

// memoize csv to a function returning lang store and tuples
const c = clak(csv, 'en-US') // optional second param, default lang

// lang store defines lang priority; number is lang col position
// ex, [['en-US','ja-JP'], {'en-US': 2, 'ja-JP': 3}]
const langs = c(['en-US','ja-JP'])

// row tuple, if default key has no value from csv, uses scripted default
// ex, ['access_denied','access denied','あなたが入れない駄目です']
const access_denied = c('access_denied', 'no access')

// from each tuple to return the final language-specific value needed.
clak(access_denied, langs, ['ja-JP']) // 'あなたが入れない駄目です'
clak(access_denied, langs, ['en-US']) // 'access denied'
clak(access_denied, langs, ['es-ES']) // 'access denied'

// Where lists of languages are used, a priority order is given so if
// a value for the language at the front of the list is not found,
// a value for the next language in the list will be used and so on.
//
// Using one-item lists and no fallback is fine. Clak will fallback to
// the found or scripted value of the default language, usually "en-US"

clak optionally prints missing row details.

c('forbidden', 'no access') // [!!!] clak: missing row: forbidden
c.warn_disable() // disable warning messages
c('forbidden', 'no access')

clak only returns language strings, so minimal examples of related useful things are demonstrated below..

const tpl = 'Missing fields: {fields}'
const obj = {
  // node and browser native international list-formatting
  fields: new Intl.ListFormat('en', {
    style: 'short',
    type: 'disjunction'
  }).format(['username', 'password'])
}
const msg = Object.keys(obj)
  .reduce((prev, key) => prev.replace(`{${k}}`, obj[k]), tpl)
// 'Missing fields: username and password'
// https://www.w3.org/International/questions/qa-accept-lang-locales
//
// an accept-langauge header might look like this and could be parsed many ways,
//  'en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8'
const acceptLangStr = ctx.get('accept-language')
// https://www.npmjs.com/package/accept-language-parser
const parsed = acceptLanguageParser.parse(acceptLangStr)
const parsedISOSpec = parsed.find(p = p.code && p.region)

const lang = parsedISOSpec &&
  [parsedISOSpec.code, parsedISOSpec.region].join('-')
// 'en-US'

To anyone who may possibly use this package, feel free to open issues and support requests.