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

@capnajax/language

v1.0.0

Published

Easy i18n for ES6 ExpressJS applications

Downloads

2

Readme

language

Easy i18n for ES6 applications

Install

npm install --save @capnajax/language

Language File

The default location for the language file is in ${CWD}. If you're running your project with npm, (e.g. npm start), CWD will be the location of your project's package.json file.

File structure

The language file is a YAML file with a simple structure:

  • All arrays are lists of language translations. Each item in the list has a name property, and a property for each language provided. There is a special all property that's the default of a language-specific translation is not provided. Names should be valid variable names.
  • All objects are simply a specification of the language text object hierarchy.
global:
  topic1:
  - name: an_apple
    en-us: An apple
    fr: Une pomme
    zh-cn: 一个苹果
    zh-tw: 一個蘋果

There is a translation called an-apple in global.topic1 list that has translations for American English, French, and both traditional and simplified Chinese.

Usage

import getLanguageText from '@capnajax/language'

// optional -- defaults to ${CWD}/language.yaml
getLanguageText.setLanguageFilename('/path/to.your/langauge.yaml');
// optional but important -- prevent the language cache from cacheing the
// translations for more than 50 different accept-language headers.
getLanguageText.setMaxLanguageCacheSize(50);
// optional but important -- when a cache is purgeed, purge it to this number
// of languages. More space between min and max means less-frequent purging.
getLanguageText.setMinLanguageCacheSize(40);

async function someFunc() {

  // takes one parameter -- an `accept-language` header value
  let language_text = await getLanguageText('zh_tw, en_us;q=0.9, en_uk;q=0.8');
  // returns {global:{topic1:{an_apple:"一個蘋果"}}}. It has the entire language
  // file.

  console.log(language_text.global.topic1.an_apple);
  // prints "一個蘋果"

  // If using lodash (recommended)
  console.log(_.get(language_text, 'global.topic1.an_apple'));
  // prints "一個蘋果"
}

Change History

1.0.0 (Current release)

  • Periodically purge the calculated language objects cache to prevent it from getting too big over time.

0.9.0 (Previous Release)

First release

Wish List, not scheduled

  • Audit capability, provide a script to test the integrity and completeness of a language file
  • Separate and merge, provide a script to take the language file and separate it into multiple files, one for each language, and another to merge them back together.
  • Identify language headers that would produce the exact same language file so more languages can be cached with the same amount of memory and the cache would need less frequent purging