imajus:simple-schema-i18n

v0.0.5Published 7 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Overview

Support Node SimpleSchema package error messages i18n using TAPi18n package. Reactively depends on TAPi18n.getLanguage() on the Client.

Installation

meteor add imajus:simple-schema-i18n

And, if tap:i18n is not in the project yet:

meteor add tap:i18n

Features

  1. Fetch SimpleSchema MessageBox messages translation for each langauge from TAPi18n.getLanguages() on Meteor.startup().
  2. (Client only) All SimpleSchemai18n instances depend reactively on TAPi18n.getLanguage().
  3. Extend schema options with { tracker: Tracker } so you don't need to provide this option to every SimpleSchema constructor call anymore.
  4. Initially extend SimpleSchema with ['autoform', 'denyInsert', 'denyUpdate'] options.
  5. Set SimpleSchema.debug to Meteor.isDevelopment.

Usage

i18n/en.i18n.json

1{
2  "simple-schema-i18n": {
3    "required": "{{{label}}} is required"
4  }
5}

Example, you actually need to fill the "simple-schema-i18n" section with all messages.

i18n/ru.i18n.json

1{
2  "simple-schema-i18n": {
3    "required": "{{{label}}} обязательное поле"
4  }
5}

Example, you actually need to fill the "simple-schema-i18n" section with all messages.

project-tap.i18n

1{
2    "helper_name": "_",
3    "supported_languages": null,
4    "i18n_files_route": "/i18n",
5    "cdn_path": null,
6    "preloaded_langs": ["*"]
7}

Here, "preloaded_langs" option is crucial for package to work properly. Other options could be changed as you like.

1import { SimpleSchema } from 'meteor/imajus:simple-schema-i18n';
2
3const schema = new SimpleSchema({ /* Fields definition */ });
4
5const message = { type: 'required', label: 'Field' };
6console.log(schema.messageBox.message(message, { language: 'en' }));
7// Output: Field is required
8console.log(schema.messageBox.message(message, { language: 'ru' })); 
9// Output: Field обязательное поле
10
11/**
12 * Уou can use this SimpleSchema as usual, the language of validation errors
13 * will reactively depend on TAPi18n.getLanguage() and change accordingly.
14 */

Converting existing code

Let's assume your current code look like this:

1import { Tracker } from 'meteor/tracker';
2import SimpleSchema from 'simpl-schema';
3
4const schema = new SimpleSchema({ /*…*/ }, { tracker: Tracker });

To support SimpleSchema i18n, the code above must be change to this:

1import { SimpleSchema } from 'meteor/imajus:simple-schema-i18n';
2
3const schema = new SimpleSchema({ /*…*/ })

Obviously, you also need to add tap:i18n Meteor package dependency and create actual i18n files.