项目作者: i18next

项目描述 :
i18next-fs-backend is a backend layer for i18next using in Node.js and for Deno to load translations from the filesystem.
高级语言: JavaScript
项目地址: git://github.com/i18next/i18next-fs-backend.git
创建时间: 2020-04-18T19:23:58Z
项目社区:https://github.com/i18next/i18next-fs-backend

开源协议:MIT License

下载


Introduction

Actions
Actions deno
npm version

This is an i18next backend to be used in Node.js and Deno. It will load resources from the file system.

It’s based on the deprecated i18next-node-fs-backend and can mostly be used as a drop-in replacement.

It will load resources from filesystem. Right now it supports following filetypes:

  • .json
  • .json5
  • .jsonc
  • .yml/.yaml
  • .js (very limited, checks for exports or export default)

Getting started

  1. # npm package
  2. $ npm install i18next-fs-backend

Wiring up:

  1. import i18next from 'i18next';
  2. import Backend from 'i18next-fs-backend';
  3. i18next.use(Backend).init(i18nextOptions);

for Deno:

  1. import i18next from 'https://deno.land/x/i18next/index.js'
  2. import Backend from 'https://deno.land/x/i18next_fs_backend/index.js'
  3. i18next.use(Backend).init(i18nextOptions);
  • As with all modules you can either pass the constructor function (class) to the i18next.use or a concrete instance.

Backend Options

  1. {
  2. // path where resources get loaded from, or a function
  3. // returning a path:
  4. // function(lngs, namespaces) { return customPath; }
  5. // the returned path will interpolate lng, ns if provided like giving a static path
  6. loadPath: '/locales/{{lng}}/{{ns}}.json',
  7. // path to post missing resources
  8. addPath: '/locales/{{lng}}/{{ns}}.missing.json',
  9. // if you use i18next-fs-backend as caching layer in combination with i18next-chained-backend, you can optionally set an expiration time
  10. // an example on how to use it as cache layer can be found here: https://github.com/i18next/i18next-fs-backend/blob/master/example/caching/app.js
  11. // expirationTime: 60 * 60 * 1000
  12. }

Options can be passed in:

preferred - by setting options.backend in i18next.init:

  1. import i18next from 'i18next';
  2. import Backend from 'i18next-fs-backend';
  3. i18next.use(Backend).init({
  4. backend: options,
  5. });

on construction:

  1. import Backend from 'i18next-fs-backend';
  2. const Backend = new Backend(null, options);

via calling init:

  1. import Backend from 'i18next-fs-backend';
  2. const Backend = new Backend();
  3. Backend.init(null, options);

TypeScript

To properly type the backend options, you can import the FsBackendOptions interface and use it as a generic type parameter to the i18next’s init method, e.g.:

  1. import i18n from 'i18next'
  2. import FsBackend, { FsBackendOptions } from 'i18next-fs-backend'
  3. i18n
  4. .use(FsBackend)
  5. .init<FsBackendOptions>({
  6. backend: {
  7. // fs backend options
  8. },
  9. // other i18next options
  10. })

If set i18next initAsync option to false it will load the files synchronously

  1. // i18n.js
  2. const { join } = require('path')
  3. const { readdirSync, lstatSync } = require('fs')
  4. const i18next = require('i18next')
  5. const Backend = require('i18next-fs-backend')
  6. i18next
  7. .use(Backend)
  8. .init({
  9. // debug: true,
  10. initAsync: false,
  11. fallbackLng: 'en',
  12. lng: 'en',
  13. preload: readdirSync(join(__dirname, '../locales')).filter((fileName) => {
  14. const joinedPath = join(join(__dirname, '../locales'), fileName)
  15. const isDirectory = lstatSync(joinedPath).isDirectory()
  16. return isDirectory
  17. }),
  18. ns: 'backend-app',
  19. defaultNS: 'backend-app',
  20. backend: {
  21. loadPath: join(__dirname, '../locales/{{lng}}/{{ns}}.json')
  22. }
  23. })

Gold Sponsors