How to use make-plural - 9 common examples

To help you get started, we’ve selected a few make-plural examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github catamphetamine / javascript-time-ago / bin / generate-quantifiers.js View on Github external
{
	// // Some keys are locales, e.g. "pt-PT".
	// // (whatever that means)
	// const language = locale.split('-')[0]

	// Don't know what the "root" key is for so skip it.
	if (locale === 'root')
	{
		continue
	}

	// `make-plural` library converts
	// CLDR pluralization rules
	// into a javascript function.
	// https://github.com/eemeli/make-plural.js
	const make_plural = MakePlural.load
	(
		CLDR('supplemental/plurals'),
		// Ordinals aren't needed for relative date/time formatting
		// CLDR('supplemental/ordinals')
	)

	// Pluralization function code
	const function_code = new make_plural(locale).toString('classify')

	// Minify pluralization function code
	let { error, code } = UglifyJS.minify(function_code)

	if (error)
	{
		throw error
	}
github catamphetamine / relative-time-format / bin / generate-locale-messages.js View on Github external
function getPluralRuleFunctionCode(locale) {
	if (plurals.hasOwnProperty(locale)) {
		const expectedLocale = getPluralRulesLocale(locale)
		if (locale !== expectedLocale) {
			throw new Error(`Expected to find pluralization rules for "${expectedLocale}" locale but found them for "${locale}" locale`)
		}
		return {
			locale,
			quantify: new MakePlurals(locale).toString('classify')
		}
	}
	const parts = locale.split('-')
	parts.pop()
	locale = parts.join('-')
	if (locale) {
		return getPluralRuleFunctionCode(locale)
	}
	// Not found.
github TrigenSoftware / i18n-for-browser / src / index.js View on Github external
import { vsprintf }  from 'sprintf-js';
import ParseInterval from 'math-interval-parser';
import MakePlural    from 'make-plural/make-plural';
import Plurals       from 'make-plural/data/plurals.json';
import MessageFormat from 'messageformat';
import Mustache      from 'mustache';
import Url           from 'url';

MakePlural.load(Plurals);

let localeChangeListener = () => {},
	defaultLocale  = 'en',
	locales        = {},
	fallbacks      = {},
	cookiename     = null,
	objectNotation = false;

const MessageFormatInstanceForLocale = {},
	PluralsForLocale = {};

// Silent configure if I18N object is exist.
if (typeof global.I18N == 'object') {
	configure(global.I18N);
}
github catamphetamine / relative-time-format / bin / generate-locale-messages.js View on Github external
import { isEqual } from 'lodash'

// `make-plural` library converts CLDR pluralization rules into javascript code.
// https://github.com/eemeli/make-plural
import MakePlural from 'make-plural/make-plural'
import plurals from 'make-plural'

// CLDR packages should be periodically updated as they release new versions.
// `npm install cldr-data@latest cldr-dates-full@latest --save-dev`
import CLDR from 'cldr-data'

import extractRelativeTimeMessages from '../source/CLDR/extractRelativeTimeMessages'
import getLocalesListInCLDR from '../source/CLDR/getLocalesList'
import getPluralRulesLocale from '../source/getPluralRulesLocale'

const MakePlurals = MakePlural.load(
	CLDR('supplemental/plurals'),
	// Ordinals aren't needed for relative date/time formatting
	// CLDR('supplemental/ordinals')
)

// CLDR replaces missing translations with an English stub.
// This can be used to find out whether a translation is missing.
const LONG_STYLE_TRANSLATION_STUB = `{
	"year": {
		"previous": "last year",
		"current": "this year",
		"next": "next year",
		"past": "-{0} y",
		"future": "+{0} y"
	}`
github lingui / js-lingui / packages / lingui-i18n / src / utils.dev.js View on Github external
// @flow
import { parse } from 'messageformat-parser'

import MakePlural from 'make-plural/make-plural'
MakePlural.load(
  require('make-plural/data/plurals.json'),
  require('make-plural/data/ordinals.json')
)

const isString = s => typeof s === 'string'

export const loadLanguageData = (language: string) => {
  const plurals = new MakePlural(language, {
    cardinals: true,
    ordinals: true
  })

  return { plurals }
}

export const compileMessage = (message: string) => processTokens(parse(message))
github catamphetamine / relative-time-format / bin / generate-locale-quantifiers.js View on Github external
// Don't know what the "root" key is for so skip it.
	if (locale === 'root') {
		continue
	}

	// If this locale has no relative time labels
	// in CLDR data then skip it.
	if (CLDR_LOCALES.indexOf(locale) < 0) {
		continue
	}

	// `make-plural` library converts
	// CLDR pluralization rules
	// into a javascript function.
	// https://github.com/eemeli/make-plural.js
	const MakePlurals = MakePlural.load(
		CLDR('supplemental/plurals'),
		// Ordinals aren't needed for relative date/time formatting
		// CLDR('supplemental/ordinals')
	)

	// Pluralization function code
	let code = new MakePlurals(locale).toString('classify')

	// Minify the code.
	code = minify(code)

	if (code) {
		// Write pluralization function to a file.
		fs.outputFileSync(
			path.join(__dirname, '../locale', locale, 'quantify.js'),
			`module.exports=${code}`
github TrigenSoftware / i18n-for-browser / src / index.js View on Github external
}

	// enforce number
	count = parseInt(count, 10);

	// find the correct plural rule for given locale
	if (typeof translated === 'object') {

		let p = null;

		// create a new Plural for locale
		// and try to cache instance
		if (PluralsForLocale[targetLocale]) {
			p = PluralsForLocale[targetLocale];
		} else {
			p = new MakePlural(targetLocale);
			PluralsForLocale[targetLocale] = p;
		}

		// fallback to 'other' on case of missing translations
		translated = translated[p(count)] || translated.other;
	}

	return postProcess(translated, namedValues, params, count);
}
github lingui / js-lingui / packages / lingui-i18n / src / utils.dev.js View on Github external
export const loadLanguageData = (language: string) => {
  const plurals = new MakePlural(language, {
    cardinals: true,
    ordinals: true
  })

  return { plurals }
}

make-plural

Unicode CLDR pluralization rules as JavaScript functions

ISC
Latest version published 8 months ago

Package Health Score

73 / 100
Full package analysis