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

mongoose-translation-plugin

v1.0.3

Published

Auto-translation plugin for mongoose models

Downloads

8

Readme

Mongoose Translation Plugin

This translation plugin is designed for Mongoose together with a translation provider such as Google Translate or DeepL.

It is a simple plugin that allows you to translate your Mongoose models by simply adding translatable to any attribute of a mongoose Schema and store overrides making contents searchable in any translated language.

codecov Contributor Covenant License: AGPL v3

How it works

The plugin will automatically create a new attribute translation for the translations and will handle the translation of the attributes of the schema that have been flagged.

The translations are stored within each document in the translation attribute.

If a translation is not found for a given locale, the plugin allows you to retrieve the translation from the original document to that locale by requesting a translation provider

The translations provided can be overridden by the user.

The original document's translatable fields are hashed, in order to re-fetch the translation from the original document if any change occur in the native document's translatable attributes.

Installation

> npm install mongoose-translation-plugin

Mongoose Translation Plugin does not require mongoose dependencies directly but expects you to have the dependency installed.

Usage

The plugin is installed directly on the schema you want to translate. The Schema attributes that you want to translate should be flagged with translatable as shown below.

Prerequisites

The plugin require a translation provider to be passed as an argument. The translation provider must be of type TranslatorFunction.

(translationParams: TranslatablePayload) => Promise<string[]>;

The translation params (of type TranslatablePayload) are as follows:

  • from: The original locale string of the text supported by the translation provider
  • to: The target locale string of the text supported by the translation provider
  • text: The text to be translated, as an array of strings.

The function must preserve the order of the strings. A sanitizer function can be passed as an option to sanitize the text before sending it to the translation provider.

An example with Google Translate is given in the repository.

Plugin Mongoose Schema

First you need to plugin into the schema you want translatable, and define the attributes you want translatable by adding translatable: true in the schema definition. If the Schema contains nested objects, you can also define the nested object as translatable.

import { Schema } from 'mongoose';
import { type TranslatableDocument, translationPlugin } from 'mongoose-translation-plugin';
import { translator } from './path/to/translator';

interface ISimple {
  translatableStringField: string;
  nonTranslatableStringField: string;
  other: number;
}

type ISimpleDocument = ISimple & TranslatableDocument<ISimple>;

const schema = new Schema({
  translatableStringField: { type: String, translatable: true },
  nonTranslatableStringField: String,
  other: Number
});

schema.plugin(translationPlugin, { translator });

export const SimpleModel = mongoose.model<ISimpleDocument>('SimpleModel', schema);

You're free to define your model how you like. Mongoose Translation Plugin will add :

  • a language attribute (can be renamed with options)
  • a sourceHash attribute (can be renamed with options)
  • a translation attribute that contains all the translations
  • a translate method to retrieve the document in a specific language as plain object
  • a updateOrReplaceTranslation method to manually manage the translation of a locale.

Additionally, Mongoose Translation Plugin adds some other utility methods to your Schema. See the API Documentation section for more details.

Options

The plugin accepts an options object as a second argument. The options are as follows:

  • languageField (default: 'language'): The name of the attribute that contains the language of the document.
  • hashField (default: 'sourceHash'): The name of the attribute that contains the hash of the translatable fields.
  • ~~translationField (default: 'translation'): The name of the attribute that contains the translations.~~ (To be implemented)
  • translator: The translation provider function.
  • sanitizer: A function that will be called to sanitize the text before sending it to the translation provider.

Translation

The plugin will automatically translate the translatable fields of the document when the function translate is called. Nested objects are also supported.

const document = await SimpleModel.create({ translatableStringField: 'Hello', nonTranslatableStringField: 'World', other: 42 });
const documentTranslation = await document.translate('fr');

Output

The documentTranslation is a plain object as follows:

{
  "nativeLanguage": "en",
  "supportedLanguages": ["en", "fr"],
  "language": "fr",
  "sourceHash": "<The hash of all the translatable fields>",
  "autoTranslated": true,
  "translatableStringField": "Bonjour",
  "nonTranslatableStringField": "World",
  "other": 42
}

Override an Automatic Translation

This feature is not implemented yet in the plugin, but as a workaround, it can be done manually by calling the updateOrReplaceTranslation method, and ensuring that autoTranslate is set to false.

Limitations

The plugin does not support populated fields, and will not translate the populated fields of the document. The models we had implemented so far did not require this feature, as they where pretty simple, and could be combined separately, but it could be implemented in the future.

Contribute

Please respect the Code of Conduct to submit your improvement change requests.

License

Mongoose Translation Plugin is licensed under the AGPL license.