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

localeresolver

v0.1.1

Published

locale resolution for base on configurable rules effort

Downloads

4

Readme

This module contains a locale resolver which is designed as middleware to be used with Express. Locale resolver uses a popular open source rules engine called Nools (JS) / Drools (Java ).

It is envisioned that your rules will have a dependency on the locale factory much like the default rules. The locale factory should be created with files which contain the locales that your application can supported. The resources directory contains examples of the files and their formats expected by locale factory.

Here is an example of creating the locale factory with those files:

localeFactory = new G11nLocaleFactory( path.resolve(__dirname, '../example/resources/BCP47LocaleMapping.json'),
    path.resolve(__dirname, '../example/resources/CountryTimeZoneMapping.json'),
    path.resolve(__dirname, '../example/resources/SupportedLanguageMapping.json'),
    path.resolve(__dirname, '../example/resources/g11nProperties.json') );  OR

and then passing in the locale factory to the locale resolver middleware for your app:

app.all(appPath, userMiddleware(), localeResolver.requestHandler(localeFactory), renderMiddlewareHandler() );

Please note that the format of the configuration files (BCP47LocaleMapping.json, CountryTimeZoneMapping.json, SupportedLanguageMapping.json, and g11nProperties.json) used to construct the locale factory must be adhered to.

You can use the localeResolver directly as middleware or wrap the call to localeResolver in additional middleware.

After the locale resolver middleware executes, the response.locals object will have a locale object attached:

res.locals.locale = { bcp47Locale: 'es-ES-x-ES',
                      countryCode: 'ES',
                      language: 'es-ES',
                      locale: 'es_ES',
                      cldrLocale: 'es-ES',
                      supportedLanguages: [ 'es-ES', 'en-US' ],
                      timeZones:
                          [ 'Africa/Ceuta',
                            'Atlantic/Canary',
                            'Europe/Madrid',
                            'Europe/Berlin' ],
                      primaryTimeZone: 'Europe/Madrid' }

The locale resolver passes in a locale factors object and set of rules to the rule engine. The default rules and locale factors object function expect that if there is a logged in user then the request object has a user object attached with the following attributes set:

req.user = {
    preferredCountry: 'some iso 3166 country code value you previously retrieved',
    preferredLang: 'some  BCP-47 language value you previously retrieved'
}

You can override the rules file and locale factors object used by the rules by passing in options:

var options = { ruleFile: path.resolve(__dirname, '<you RulesFile named whatever you like>.nools'),
                localeFactorsModule: path.resolve(__dirname, '<your LocaleFactors file named LocaleFactors>') };

localeResolver.requestHandler(options);

```
If you do override the defaults, then your locale factors object should be exported/named 'LocaleFactors' and you must
have the populateFactors function defined:

 ```javascript
 LocaleFactors.prototype.populateFactors = function populateFactors(req) {
 // your custom logic that populates a locale factors properties that are used by your custom rules
 }


```
Default locale rules is located in example/resources/localeResolution.nools and uses the following properties:


- If user is signed-in, user preferred country and language
- 'country' and/or 'lang' URL params http://<your host:port/your applicaton>?country=DE&lang=de_DE
- lang query param can be passed in as BCP47 format.
- browser 'accept-language' header