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

i18n-mongo

v1.1.5

Published

Node language manager with t-function

Downloads

75

Readme

i18n-mongo

This README document is under construction

License: EUPL v1.1 NPM version Downloads Build Status Coverage Status

Install me

$ npm i i18n-mongo --save

How to use it

To create a translatable schema, you can use localizableModel:

server.js

import express from 'express';
import http from 'http';
import { i18nMongo } from 'i18n-mongo';

const mongodbUrl = 'mongodb://localhost:27017/my-db';
const user = '';
const pass = '';

mongoose.connect(mongodbUrl, { user, pass }, (err) => {
    mongoose.Promise = q.Promise;
    const app = express();

    app.use(i18nMongo());
    http.createServer(app).listen(3000);
});

mycollection.js:

import { localizableModel, Localized } from 'i18n-mongo';

const Schema = mongoose.Schema;
const mycollection = new Schema({
    translatableString: Localized
    someObject: {
        translatableObjString: Localized
    }
});

export const MyModel = localizableModel('mycollection', mycollection);

router.js:

import express from 'express';
import mongoose from 'mongoose';
import { createRouter } from 'i18n-mongo';
import { MyModel } from './mycollection';

const app = express();
const mw = i18nMongo({
    logger: console, // Optional
    email: { // Optional
        transport: nodemailer.createTransport(...),
        from: '[email protected]',
        to: '[email protected]',
    },
    defaultLanguage: '--', // Optional, strings in defaultLanguage will not be inserted
}, (err) => {
    console.log('Available languages loaded and i18n is ready to be used');
});
app.use(mw);


const router = createRouter(new express.Router(), { auth });
app.use('/lang', router);


router.get('/mycollection', (req, res) => {
    MyModel.findLocalized({}, req.lang)
        .then(res.json.bind(res))
        .catch(err => res.status(500).send(err));
});

router.get('/mycollection/:_id', (req, res) => {
    MyModel.findLocalized({ _id: req.params._id }, req.lang)
        .then(docs => ((docs && docs.length) ? res.json(doc[0]) : res.status(404).end())
        .catch(err => res.status(500).send(err));
});

router.post('/mycollection', async (req, res) => {
    MyModel.saveLocalized(req.body)
        .then((savedDoc) => {
            const uri = `/mycollection/${savedDoc._id}`;
            res.setHeader('Location', uri); // res.location(uri);
            res.status(201).send(uri);
        })
        .catch(err => res.status(500).send(err));
});

router.put('/mycollection/:_id', async (req, res) => {
    MyModel.saveLocalized(req.body, req.params._id)
        .then(() => res.status(204).end())
        .catch(err => res.status(500).send(err));
});

router.delete('/mycollection/:_id', async (req, res) => {
    MyModel.findOne({ _id: req.params._id }).exec()
        then((doc) => {
            if (doc) {
                doc.remove();
                res.status(204).end()
            } else {
                res.status(404).end()
            }
        })
        .catch(err => res.status(500).send(err));
});

router.get('/admin/mycollection', async (req, res) => {
    MyModel.findLocalized({}, '')
        .then(res.json.bind(res))
        .catch(err => res.status(500).send(err));
});

When requesting GET /mycollection/IDHERE?lang=en it will return the collection populated:

{
    "translatableString": "The value for 'en' language",
    "someObject": {
        "translatableObjString": "Other string for 'en' language"
    }
}

When requesting GET /admin/mycollection?lang=en, the language will be ignored (as defined in router example) and it will return all languages:

{
    "translatableString": {
        "en": "The value for 'en' language",
        "ca": "El valor traduït al 'ca'"
    },
    "someObject": {
        "translatableObjString": {
            "en": "Other string for 'en' language",
            "ca": "Una altra string en 'ca'",
    }
}