1.0.3 • Published 1 year ago

@xirelogy/rollup-plugin-xwts-i18n v1.0.3

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

Rollup plugin to compile i18n resources

Introduction

This is a rollup plugin to compile the i18n resources defined using XW (Typescript) framework.

Usage

This plugin is used in corresponding vite.config.ts to control how i18n resources (locales) are compiled.

The plugin can be included using:

import xwtsI18n from '@xirelogy/rollup-plugin-xwts-i18n';

And configured like:

const i18n = xwtsI18n({
  output: { ... },
  roots: {
    'locales': [],
  },
  include: 'locales/**',
});

with following configuration options:

KeyUsage
outputDefine the output type. Currently virtual and file supported.
rootsModule prefix of the locales defined. This correspond to the names where the i18n module is initialized using xw.i18n.initModule(...)
includeSearch folder for locale translation files

Usage in projects (virtual output)

This is normally used for projects, whereby the deliverable is the project output. Locales will be compiled into a virtual module defined using the moduleName argument.

KeyUsage
moduleNameThe module name whereby the locales can be included.

Example:

import { defineConfig } from 'vite';

import xwtsI18n from '@xirelogy/rollup-plugin-xwts-i18n';

const i18n = xwtsI18n({
  output: {
    type: 'virtual',
    moduleName: 'locale:compiled',
  },
  roots: {
    'locales': [],
  },
  include: 'locales/**',
});

export default defineConfig({
  plugins: [
    i18n,
  ],
  ...
});

Usage in libraries (file output)

This is normally used for libraries, whereby its outputs will be used by other projects. Locales will be compiled into file supporting the following distribution target:

KeyUsage
fileNameThe filename for output in the ESM format.
cjsFileNameThe filename for output in the CommonJS/UMD format.
dtsFilenameThe filename for output containing the Typescript definition (.d.ts).

Example:

import { defineConfig } from 'vite';

import xwtsI18n from '@xirelogy/rollup-plugin-xwts-i18n';

const i18n = xwtsI18n({
  output: {
    type: 'file',
    fileName: 'locales.es.js',
    cjsFileName: 'locales.umd.js',
    dtsFilename: 'locales.d.ts',
  },
  roots: {
    'locales': [ '<module>', '<names>' ],
  },
  include: 'locales/**'
});

export default defineConfig({
  plugins: [
    i18n,
  ],
  ...
});

Importing the locales

Compiled locales are normally imported and used in the the entrance file (like main.ts), in a section of code something like below:

import { xw } from '@xirelogy/xwts';

// Import current project locales (virtual output)
import locales from 'locale:compiled';
locales(xw.i18nSetup);

// Import libraries locales (file output)
import xwLocales from '@xirelogy/xwts/locales';
xwLocales(xw.i18nSetup);

...

Working with Typescript

When importing current project locales like below:

import locales from 'locale:compiled';
locales(xw.i18nSetup);

The corresponding module might not be recognized and causes a warning. Create a shim file shims-locale.d.ts with the following content shall overcome this:

/* eslint-disable */
declare module 'locale:compiled' {
  import { XwI18nModuleDefinable } from '@xirelogy/xwts';
  export default function locales(modDef: XwI18nModuleDefinable): void;
}
1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago