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

nce-i18n

v0.0.1

Published

A i18n extension for the nce cms

Downloads

7

Readme

#i18n for nce

Description

Internationalization (not only) for the nce framework

How to install

Install with npm: npm install --save nce-i18n

Integrate in NCE:

var NCE = require("nce");
var nce = new NCE(/*{
  logger:{},
  fallbackLanguage: "en"
}*/);
var i18n = require("nce-i18n");
var ext = dummy(nce);
ext.install();
ext.activate();

Or use nce-extension-manager...

Install standalone

var i18n = require("nce-i18n");

var dict = i18n.Dictionary.create("name", {de:{hello:"Hallo", vsprintf: "Hallo %s"},en:{hello:"Hello", vsprintf: "Hello %s"}});

var translator = new i18n.Translator({targetLanguage:"en"});
translator(dict, "hello");

// Concat with other functions:
translator(dict, "hello", console.log); // -> undefined but get a console.log of Hello
translator(dict, "hello", console.log, "this is the second parameter for console.log"); // -> undefined but get a console.log of "Hello" and "this is the second parameter for console.log"
translator(dict, "vsprintf", require("sprintf").vsprintf, "World"); // -> Hello World

How to use

Basic funcitons for dictionary

createDictionary(name, dict, defaultLanguage)

Create a new dictionary.

removeDictionary(name)

Remove a dictionary by its name.

getDictionary

Get a dictionary by its name.

Basic funcitons for translator

__(dict, query, fn, args)

  • First argument must be the dictionary or a dictionary name.
  • The query should be a string with dot notation to query, or you are able to use different arguements as string
  • The function (fn) is optional but must be a function!
  • The arguments are optional and get passed to the function (fn). The result of the translation is the first argument!
Example

We have a dictionary called example with the following structure:

var ex = i18n.Dictionary.create("example", {
  en:{
    nested:{
      path: {
        for: {
          example: {
            string: "Result"
          }
        }
      },
      "path.for": {
        example: {
          string: "Another"
        }
      }
    }
  });

We are able to request the following ways:

translator.__("example", "nested.path.for.example.string"); // -> Result
translator.__(ex, "nested.path.for.example.string"); // -> Result
translator.__("example", "nested", "path", "for", "example", "string"); // -> Result
translator.__("example", "nested", "path.for", "example", "string"); // -> Another
translator.__("example", "nested", "path.for", "example.string"); // -> Another
translator.__("example", "nested", "path.for.example", "string"); // -> Result

We can use additional functions:

translator.__("example", "nested.path.for.example.string", console.log); // -> logs: Result
translator.__("example", "nested", "path.for", "example", "string" console.log); // -> logs: Another
translator.__("example", "nested", "path.for", "example.string", console.log, "example"); // -> logs: Another, example
translator.__("example", "nested", "path.for.example", "string", console.log, "works", "fine"); // -> logs: Result, works, fine

__n(dictionary, number, query, fn, args)

  • First argument must be the dictionary or a dictionary name.
  • The number of an element (1 is singular everything else is plural)
  • The query should be a string with dot notation to query, or you are able to use different arguments as string
  • The function (fn) is optional but must be a function!
  • The arguments are optional and get passed to the function (fn). The result of the translation is the first argument!
Example

We have a dictionary called example with the following structure:

var n = i18n.Dictionary.create("enumeration", {
  en:{
    dog:{
      plural: "dogs",
      singular: "dog"
    }
  });

We are able to request the following ways:

translator.__n("enumeration", 1, "dog"); // -> dog
translator.__n(n, 1, "dog"); // -> dog
translator.__n(n, 2, "dog"); // -> dogs

Using functions and arguments for the function works the same way like using __!

__g(dictionary, gender, query, fn, args)

  • First argument must be the dictionary or a dictionary name.
  • The gender of an element (m is masculine, f is feminine)
  • The query should be a string with dot notation to query, or you are able to use different arguments as string
  • The function (fn) is optional but must be a function!
  • The arguments are optional and get passed to the function (fn). The result of the translation is the first argument!
Example

We have a dictionary called example with the following structure:

var g = i18n.Dictionary.create("gender", {
  en:{
    dog:{
      feminine: "bitch",
      masculine: "dog"
    }
  });

We are able to request the following ways:

translator.__g("gender", "m", "dog"); // -> dog
translator.__g(g, "masculine", "dog"); // -> dog
translator.__g("gender", "f", "dog"); // -> bitch
translator.__g("gender", "feminine", "dog"); // -> bitch

Using functions and arguments for the function works the same way like using __!

Integrate in NCE

Config settings

You are able to use the following config-settings (listed with their defaults):

  • fallbackLanguage: "en": Language to use, when no other requested language matches.
  • logger: {}: Settings for logger-extension

Basic methods

ext.createDictionary = function(name, dict, defaultLang)

You can use this methods like described above in "dirctionary" section.

ext.removeDictionary = function(name)

You can use this methods like described above in "dirctionary" section.

ext.getDictionary = function(name)

You can use this methods like described above in "dirctionary" section.

Use in a request-middleware.

  • req.i18n.translator.__(dict, ... [fn], [arguments])
  • req.i18n.translator.__n(dict, n, ... [fn], [arguments])
  • req.i18n.translator.__g(dict, gender, ... [fn], [arguments])

You can use this methods like described above in "translator" section.

ext.dummy(name, cb, opts)

Dummy method.

Arguments
  1. name[String]:
  2. cb[Function]: Callback-function with the arguments:
    1. error[Error]: Used for exceptions
    2. data[Buffer]: Dummy-Data
    3. result[Object]: Result of the query
      • content[String]:
      • query[String]:
  3. opts[Object]: Options: