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

catberry-l10n

v6.0.0

Published

Localization plugin for Catberry Framework

Downloads

41

Readme

Localization Plugin for Catberry Framework

Build Status Gitter codecov.io

Installation

npm install catberry-l10n --save

Description

This plugin adds localization support for Cat-components.

It supports two contexts of localization:

  1. Application-based localization - files like en.json, en-us.json, en-gb.json and etc. inside the l10n directory at the root of your application
  2. Component-based localization - the same as above but l10n directory should be placed inside a component's directory

Every localization file is a dictionary of key value pairs. Keys are called "localization keys" and values are strings in specified language or arrays with plural forms.

For example, your project tree can look like this:

catberry_components
	component1/
		l10n/
			en.json
			en-us.json
			en-gb.json
			ru.json
		...
	component2/
		l10n/
			en.json
			en-us.json
			en-gb.json
			ru.json
		...
l10n/
	en.json
	en-us.json
	en-gb.json
	ru.json

Your application-based localization directory should always have a default localization file and a default locale name should be set in a Catberry application's config, for example:

{
	l10n: {
		// default locale used when value for specified locale not found
		// this parameter is required
		defaultLocale: 'en-us',
		// display keys of not founded localization strings
		placeholder: false,
		cookie: {
			// name of locale cookie (Optional, 'locale' by default)
			name: 'locale',
			// max cookie age (Optional, 100 years by default)
			maxAge: 3155692600,
			// cookie path (Optional, empty by default)
			path: '/',
			// cookie domain (Optional, empty by default)
			domain: 'some.domain.org'
		}
	},
	someOtherParameter: 'someOtherValue'
}

It means that l10n/en-us.json file is required at the root of your application.

While localization file is being loaded it is merged with the default localization dictionary adding absent keys. Localization of components is overriding application-based localization values if they have matching keys. If localization set specified by user does not have the localization key then value from the default localization set is returned or empty string if default localization also does not have such key.

Usage

To use the localization plugin you should register its components into Catberry's Service Locator like this:

In server.js

const l10n = require('catberry-l10n');
const catberry = require('catberry');
const config = require('./config-server');
const app = connect();
const cat = catberry.create(config);

// register localization components as singletons
l10n.register(cat.locator);

// then resolve loader to get middleware
const localizationLoader = cat.locator.resolve('localizationLoader');

// use middleware to setup locale in user's cookie
// and get /l10n.js file for user's locale
app.use(localizationLoader.getMiddleware());

// localization middleware should be before Catberry
app.use(cat.getMiddleware());
...

In browser.js/build.js

const l10n = require('catberry-l10n');
const catberry = require('catberry');
const config = require('./config-client');
const cat = catberry.create(config);

// register localization components in locator
l10n.register(cat.locator);

As you might notice, catberry-l10n has a server-side middleware that automatically sets a browser's locale to the user's cookie and you can use this value from $context.cookie.get in your stores and components.

Also, you should include /l10n.js script into your HEAD element. This URL is served by catberry-l10n middleware.

Pluralization

Pluralization support was implemented using these rules. For pluralization of localized value, it should be set as an array with all required plural forms for locale's language.

How to use

Localization dictionary:

{
	"EAT": "eat",
	"APPLE": ["%d apple", "%d apples"]
}

Component code:

class Component {
	constructor(locator) {
		this._l10n = locator.resolve('localizationProvider');
	}

	render() {
		// user always has locale in context (thanks to l10n middleware)
		const locale = this._l10n.getCurrentLocale(this.$context);
		const appleCount = Number(this.$context.state.apples);
		return {
			localizedEat: this._l10n.get(locale, 'EAT'),
			localizedApple: util.format(
				this._l10n.pluralize(locale, 'APPLE', appleCount),
				appleCount
			)
		};
	}
}

Component's template (using Dust for example):

{localizedEat} {localizedApple}

For 1 apple it will be eat 1 apple

For 5 apples it will be eat 5 apples

Also, you can change current locale using changeLocale method of localizationProvider like this:

this._l10n.changeLocale('en-gb', this.$context);

it changes the locale value in cookie and reloads the page.

Contributing

There are a lot of ways to contribute:

Denis Rechkunov [email protected]