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

@joinbox/loopback-component-translations

v1.1.0

Published

A simple implementation of translations for loopback

Downloads

5

Readme

Loopback Component translations

The translation functionality shall be implemented as a npm package. The module shall implement generic translations to loopback based project.

Features:

  • CRUD for Translations
  • Field based translations for entities
  • Persists own translations: An entity shall persist its own translations on Create, Update, Delete.
  • Language Fall-back ( TBD: entity or filed based or verified) (config/flag)
  • Translation entity on objects (Backward compat); An array with all existing translations for a given entity. (Flag)
  • Locales: Map the available locales to the locale service; First draft make this static
  • Search

Behaviour:

  • For each loopback model having translatable properties a translation model in the format loopbackmode_locales shall be created. The translatable properties shall be configured in the translation mode and referenced by configuration in the original model.
  • GET Calls are returning the translation in the required language on the model property and in the translations array.
  • POST Calls are creating the given entity as well as it’s translations. Translatable properties on the model are dropped.
  • PATCH Calls are updating the entity as any given translation
  • DELETE Deletes the given entity and its related translations (Delete cascade)
  • The fallback is created based on the requests Accept Header

Usage:

Install the package @joinbox/loopback-component-translations as an npm dependency. A working example can be found in the test directory.

Crate a Loopback Module for the translations Register all properties you want to translate.

{
    "name": "TranslationDummyLocale",
    ...
    "properties": {
        "name": {
            "type": "String",
            "required": true
        },
        "description": {
            "type": "String"
        }
    },
    "relations": {
        "translationDummy": {
            "type": "belongsTo",
            "required": true,
            "model": "TranslationDummy",
            "foreignKey": "translationdummy_id"
        },
        "locale": {
            "type": "belongsTo",
            "required": true,
            "model": "Locale",
            "foreignKey": "locale_id"
        }
    },
    ...
}

Add your model to the model-config.json

Update your Model you want to use translations.

  1. Add the fields to be translated in a array to the options property.
  2. Link the Module you crated for the translations as relation translations
{
    "name": "TranslationDummy",
    ...
    "options": {
        ...
        "translations": [
            "title",
            "description"
        ]

    },
    ...
    "relations": {
        "translations": {
            "type": "hasMany",
            "model": "TranslationDummyLocale",
            "foreignKey": "translationdummy_id"
        }
    },
    ...
}

Extend the LoopbackModelBase in your model class as follows:

const Microservice = require('@joinbox/loopback-microservice');

const { LoopbackModelBase } = Microservice;

class TranslationDummyModel extends LoopbackModelBase {
    constructor({ model }) {
        super({ model });
    }
}

module.exports = function(model) {
    return new TranslationDummyModel({ model });
};

Add the package to the component-config.json

{
  ...
  "@joinbox/loopback-component-translations": {

  }
}

Create a boot script to parse the language header for all requests. Place the boot script in your applications boot directory. The boot script must contain the following snippet.

Further readings: https://loopback.io/doc/en/lb3/Using-current-context.html#use-a-custom-strong-remoting-phase

const { TranslationsUtil } = require('@joinbox/loopback-component-translations');

module.exports = function(app) {
    TranslationsUtil.registerLanguageParsingPhase(app);
};