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

flitter-i18n

v0.1.1

Published

Localization and internationalization for Flitter.

Downloads

8

Readme

flitter-i18n

Flitter-i18n provides localization and internationalization services for Flitter.

Installation

flitter-i18n ships with Flitter by default, but you can install it manually on existing Flitter applications:

yarn add flitter-i18n

Edit the Units.flitter.js file to add the LocaleUnit under the "Pre-Routing Custom Units" section.

'Locale'        : require('flitter-i18n/src/LocaleUnit'),

Now, deploy the default locales, configs, and middleware:

./flitter deploy locale

Basic Usage

Defining locales

Locales are defined in .locale.js files under the locale/{locale name} directory. Each locale name subdirectory should should have identical files for resolving translations.

Here's an example of the structure:

  • locale
    • en_US
      • common.locale.js
      • dash.locale.js
    • es_MX
      • common.locale.js
      • dash.locale.js
    • default
      • brand.locale.js

The default locale is resolved by ALL locales, so it can be used to define translations that are language agnostic.

The translation containers resolve the phrases from the appropriate locale's sub-directory.

Locale Files

Locale files are similar to normal config files that should export an object that maps some translation key that is common across files to a translation.

Here's an example of a hypothetical auth.locale.js:

en_US/auth.locale.js:

module.exports = exports = {
    // Basic translations can map name -> string
    log_in: 'Log in',
    log_out: 'Log out',

    // You can also define them as objects
    user: { one: 'User' },

    // If you need to customize the plural version
    // flitter-i18n supports automatic pluralization
    account: { one: 'Account', many: 'Accounts' },
}

flitter-i18n uses Pluralize under the hood to automatically pluralize translations when possible, but if you find it's getting one wrong, you can override it manually in the config file.

es_MX/auth.locale.js:

module.exports = exports = {
    log_in: 'Iniciar sesión',
    log_out: 'Cerrar sesión',
    user: { one: 'Usuario' },
    account: { one: 'Cuenta', many: 'Cuentas' },
}

default/auth.locale.js:

module.exports = exports = {
    provider_coreid: 'Starship CoreID',
}

Translation Containers

A translation container is a specialized function that points to a particular locale and resolves the translation keys to the translations. You can manually get a translation container for a particular locale using the locale service:

(flitter) ➤ T = _services.locale.get_T('es_MX')
[Function (anonymous)]
(flitter) ➤ T('auth.log_in')
'Iniciar sesión'
(flitter) ➤ T('auth.provider_coreid')
'Starship CoreID'

Scopes

To make translation more palatable to developers, you can set the scope of a translation container to make it easier to access translations. For example:

(flitter) ➤ T = _services.locale.get_T('en_US:auth')
[Function (anonymous)]
(flitter) ➤ T('log_in')
'Log in'
(flitter) ➤ T('provider_coreid')
'Starship CoreID'

Pluralization

The translation container will automatically pluralize the translation, where possible. You can invoke this by passing a number of items to the container call:

(flitter) ➤ T = _services.locale.get_T('en_US:auth')
[Function (anonymous)]
(flitter) ➤ T('user', true)
'Users'
(flitter) ➤ T('user', 5)
'Users'
(flitter) ➤ T('user', 1)
'User'
(flitter) ➤ T(5, 'user')
'Users'

Request-specific localization

A global translation container is not particularly useful on its own, however. Rather, every request that comes into the system will have a RequestLocalizationHelper injected into it by the i18n:Localize middleware. You can access this instance as req.i18n.

One particular benefit of this is that the locale of the user is stored in the session, so a translation container is automatically created for you when the request is made. For example:

// SomeAuth.controller.js
class SomeAuth extends Controller {
    login_page(req, res, next) {
        return res.page('auth:login', {
            title: req.T('auth:log_in'),
        })
    }
}

Changing the locale

You can change the locale from code by storing it in request.session.i18n.locale. However, flitter-i18n provides a clean mechanism for changing the locale.

If any request is made to the server with a locale parameter in the query string, the value of that string will be set as the locale in the session.

This means that the query string only needs to be set once, then the server will remember the locale from the session:

https://your.app/home?locale=es_MX

Setting scopes

You can also specify scopes for the request-specific translation container using the i18n:Scope middleware. For example:

routing/routers/auth.routes.js:

module.exports = exports = {
    prefix: '/auth',

    middleware: [
        ['i18n:Scope', { scope: 'auth'}],
    ],

    get: {
        '/login': ['controller::SomeAuth.login_page'],
    },
}

Now, the translation container will be automatically scoped:

// SomeAuth.controller.js
class SomeAuth extends Controller {
    login_page(req, res, next) {
        return res.page('auth:login', {
            title: req.T('log_in'),
        })
    }
}