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

@pedroloch/loopback-i18n

v0.2.0

Published

Internationalization for LoopBack 4

Downloads

3

Readme

I18n for Loopback 4

This module contains a component to add i18n to Loopback 4. It's made on top of the library i18n for node.

Instalation

npm install @pedroloch/loopback-i18n

Binding into the application

The component should be loaded with it's configuration in the constructor of you Application Class. This example assumes you have a folder called locales in your root folder.

import {BootMixin} from '@loopback/boot';
import {RepositoryMixin} from '@loopback/repository';
import {ServiceMixin} from '@loopback/service-proxy';
import {RestApplication, RestBindings} from '@loopback/rest';
import {I18nBindings, I18nComponent, I18nOptions} from '@pedroloch/loopback-i18n';
import path from 'path';

export class BackendApplication extends BootMixin(ServiceMixin(RepositoryMixin(RestApplication))) {
  constructor(options: ApplicationConfig = {}) {
    super(options);

    //...Your application bindings declarations

    // Should load the configuration first
    this.configure(I18nBindings.COMPONENT).to({
      defaultLocale: 'en',
      locales: ['en', 'pt'],
      directory: path.join(__dirname, '../locales')
    } as I18nOptions); 
    
    // Load the component after the configuration
    this.component(I18nComponent);
    
    }
}

The configuration

For the configuration, you just need to pass the same configuration you would use in the library i18n. Check their specifications for details. You can use the type I18nOptions from this library to help you with typescript autocomplete.

import {I18nOptions} from '@pedroloch/loopback-i18n';

Usage

This example assumes you have these two files, en.json and pt.json, inside the locales folder that you configured when binding to the application

en.json

{
    "greeting": "Hello",
    "greeting_with_name": "Hello, %s"
}

pt.json

{
    "greeting": "Olá",
    "greeting_with_name": "Olá, %s"

}

Now you can inject a function that will translate the messages based on the browsers 'Accept-Language'.

import { inject } from '@loopback/core';
import { get, param } from '@loopback/rest';
import { I18nBindings, I18nApi } from '@pedroloch/loopback-i18n';

export class MyController {
  constructor(
    @inject(I18nBindings.T)
    public t: I18nApi
  ) {}

  @get('/greeting')
  greeting() {
    return { msg: this.t('greeting') };
  }

  @get('/greeting/{name}')
  greetingWithName(@param.path.string('name') name: string) {
    return { msg: this.t('greeting_with_name', name) };
  }
}

Considering you are running the application on port 3000

curl -X 'GET' \
'http://localhost:3000/gretting' \
-H 'accept: application/json' |
-H 'Accept-Language: en'

# Should return
{msg:'Hello'}
curl -X 'GET' \
'http://localhost:3000/gretting' \
-H 'accept: application/json' \
-H 'Accept-Language: pt'

# Should return
{msg:'Olá'}
curl -X 'GET' \
'http://localhost:3000/gretting/John' \
-H 'accept: application/json' |
-H 'Accept-Language: en'

# Should return
{msg:'Hello, John'}
curl -X 'GET' \
'http://localhost:3000/gretting/John' \
-H 'accept: application/json' \
-H 'Accept-Language: pt'

# Should return
{msg:'Olá, John'}

If Accept-Language header is not present, it should return the defaultLocale.

curl -X 'GET' \
'http://localhost:3000/gretting/John' \
-H 'accept: application/json' |

# Should return
{msg:'Hello, John'}