Skip to content

lamualfa/dn-i18next-mongo-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ci License Maintainability Release

Are you a NodeJS user? If true, we provide the same library for NodeJS. See: Mongo Backend for NodeJS

Integrate i18next with MongoDB in Deno 🦖

Introduction

This is a i18next backend to be used in Deno. It will load resources from a MongoDB database.

Usage

IMPORTANT: Because we use mongo@v0.7.0 as a driver to connect to MongoDB, please also follow the guidelines and requirements of the library. See Mongo in Deno for further information.

import i18next from 'https://deno.land/x/i18next/index.js'
import { Backend } from 'https://deno.land/x/i18next_mongo_backend/mod.ts'

i18next.use(Backend).init({
  // Backend Options
  backend: options,
})

Backend Options

{
  // Database Name
  dbName: '<DB Name>', // Required

  // Or

   // MongoDB standard configuration
  host: '<DB Host>',
  port: 27017,

  // Or

  // Use collection - See: https://doc.deno.land/https/deno.land/x/mongo/mod.ts#Collection
  collection: Collection,

  // MongoDB authentication. Remove it if not needed
  user: '<DB User>',
  password: '<DB Password>',

  // Collection name in database will be used to store i18next data
  colName: 'i18n',

  // MongoDB field name
  // Language data
  langFieldName: 'lang',
  // Namespace data
  nsFieldName: 'ns',
  // Data
  dataFieldName: 'data',

  // Remove MongoDB special character from field name - See https://jira.mongodb.org/browse/SERVER-3229
  sanitizeFieldName: true,

  // Error handlers
  readOnError: console.error,
  readMultiOnError: console.error,
  createOnError: console.error,

  // MongoClient Options - See: https://doc.deno.land/https/deno.land/x/mongo/mod.ts#ClientOptions
  mongodb: ClientOptions
};

Example Backend Options

Connect with host and port:

{
  host: 'localhost',
  port: 27017,
  dbName: 'test' // Required field
}

Connect with collection instance (Recommended):

If you already have your own connection, use this to avoid useless connections

{
  collection: Collection, // See: https://doc.deno.land/https/deno.land/x/mongo/mod.ts#Collection
  dbName: 'test', // Required field
}

Example of the MongoDB document that will be created:

// Key name is according to provided in options
{
  "lang": "en-US",
  "ns": "translations",
  "data": {
    "key": "Thank you!"
  }
}

About

i18next mongodb backend https://www.i18next.com/ - Only for Deno 🦖

Topics

Resources

License

Stars

Watchers

Forks