Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'make-plural' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
{
// // 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
}
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.
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);
}
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"
}`
// @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))
// 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}`
}
// 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);
}
export const loadLanguageData = (language: string) => {
const plurals = new MakePlural(language, {
cardinals: true,
ordinals: true
})
return { plurals }
}