How to use the intl-messageformat.default function in intl-messageformat

To help you get started, we’ve selected a few intl-messageformat 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 format-message / format-message / scripts / benchmark.src.js View on Github external
onComplete: function () {
      console.log('Fastest is ' + this.filter('fastest').map('name').join(' or '))
      console.log()
    }
  }

  var suite = new Benchmark.Suite(name, suiteOptions)
  Object.keys(cases).forEach(function (name) {
    suite.add(name, cases[name])
  })
  suite.run()
}

pattern = 'Simple string with nothing special'
formatMessage(pattern, args, 'en') // prime cache
intlMF = new IntlMF(pattern, 'en').format
mf2 = new MessageFormat2('en').compile(pattern)
mf = new MessageFormat(pattern, 'en').format
BENCHMARK_PARSE && benchmark(
  'Parse simple message', {
    'intl-messageformat-parser': function () { return intlParse(pattern) },
    'messageformat-parser': function () { return parse2(pattern) },
    'format-message-parse': function () { return parse(pattern) }
  }
)
benchmark(
  'Format simple message', {
    'intl-messageformat (reuse object)': function () { return intlMF(args) },
    'messageformat (reuse object)': function () { return mf2(args) },
    'message-format (reuse object)': function () { return mf(args) },
    'format-message': function () { return formatMessage(pattern, args, 'en') },
    'format-message (inlined)': function () { return formatMessage('Simple string with nothing special', args, 'en') }
github GoogleChrome / lighthouse / lighthouse-core / lib / i18n / i18n.js View on Github external
function _formatIcuMessage(locale, icuMessageId, icuMessage, values) {
  const localeMessages = LOCALES[locale];
  const localeMessage = localeMessages[icuMessageId] && localeMessages[icuMessageId].message;
  // fallback to the original english message if we couldn't find a message in the specified locale
  // better to have an english message than no message at all, in some number cases it won't even matter
  const messageForMessageFormat = localeMessage || icuMessage;
  // when using accented english, force the use of a different locale for number formatting
  const localeForMessageFormat = locale === 'en-XA' ? 'de-DE' : locale;
  // pre-process values for the message format like KB and milliseconds
  const valuesForMessageFormat = _preprocessMessageValues(icuMessage, values);

  const formatter = new MessageFormat(messageForMessageFormat, localeForMessageFormat, formats);
  const formattedString = formatter.format(valuesForMessageFormat);

  return {formattedString, icuMessage: messageForMessageFormat};
}
github format-message / format-message / scripts / benchmark.src.js View on Github external
other {#th}
  } banana today, which makes {
  numBananas, plural,
       =0 {no bananas}
       =1 {a banana}
    other {lots of bananas}
  } total.`
args = {
  date: new Date(),
  name: 'Curious George',
  gender: 'male',
  numBananas: 300,
  nth: 3
}
formatMessage(pattern, args, 'en') // prime cache
intlMF = new IntlMF(pattern, 'en').format
mf2 = new MessageFormat2('en').compile(pattern)
mf = new MessageFormat(pattern, 'en').format
BENCHMARK_PARSE && benchmark(
  'Parse complex message (no numbers or dates)', {
    'intl-messageformat-parser': function () { return intlParse(pattern) },
    'messageformat-parser': function () { return parse2(pattern) },
    'format-message-parse': function () { return parse(pattern) }
  }
)
benchmark(
  'Format complex message (no numbers or dates)', {
    'intl-messageformat (reuse object)': function () { return intlMF(args) },
    'messageformat (reuse object)': function () { return mf2(args) },
    'message-format (reuse object)': function () { return mf(args) },
    'format-message': function () { return formatMessage(pattern, args) },
    'format-message (inlined)': function () {
github format-message / format-message / scripts / benchmark.src.js View on Github external
}
)
benchmark(
  'Format simple message', {
    'intl-messageformat (reuse object)': function () { return intlMF(args) },
    'messageformat (reuse object)': function () { return mf2(args) },
    'message-format (reuse object)': function () { return mf(args) },
    'format-message': function () { return formatMessage(pattern, args, 'en') },
    'format-message (inlined)': function () { return formatMessage('Simple string with nothing special', args, 'en') }
  }
)

pattern = 'Simple string with { placeholder }.'
args = { placeholder: 'replaced value' }
formatMessage(pattern, args, 'en') // prime cache
intlMF = new IntlMF(pattern, 'en').format
mf2 = new MessageFormat2('en').compile(pattern)
mf = new MessageFormat(pattern, 'en').format
BENCHMARK_PARSE && benchmark(
  'Parse common one arg message', {
    'intl-messageformat-parser': function () { return intlParse(pattern) },
    'messageformat-parser': function () { return parse2(pattern) },
    'format-message-parse': function () { return parse(pattern) }
  }
)
benchmark(
  'Format common one arg message', {
    'intl-messageformat (reuse object)': function () { return intlMF(args) },
    'messageformat (reuse object)': function () { return mf2(args) },
    'message-format (reuse object)': function () { return mf(args) },
    'format-message': function () { return formatMessage(pattern, args, 'en') },
    'format-message (inlined)': function () { return formatMessage('Simple string with { placeholder }.', { placeholder: 'replaced value' }, 'en') }
github alibaba / kiwi / kiwi-intl / built / src / index.js View on Github external
I18N.prototype.get = function (str, args) {
        var msg = lodashGet(this.__data__, str);
        if (!msg) {
            msg = lodashGet(this.__metas__['zh-CN'], str, str);
        }
        if (args) {
            try {
                msg = new intl_messageformat_1.default(msg, this.__lang__);
                msg = msg.format(args);
                return msg;
            }
            catch (err) {
                console.warn("kiwi-intl format message failed for key='" + str + "'", err);
                return '';
            }
        }
        else {
            return msg;
        }
    };
    return I18N;
github xpepermint / translatedjs / dist / index.js View on Github external
I18n.prototype.formatMessage = function (key, vars) {
        if (vars === void 0) { vars = {}; }
        var message = this.messages[key] || key;
        return new intl_messageformat_1["default"](message, this.locale, this.formats).format(vars);
    };
    return I18n;
github onap / sdc / openecomp-ui / src / nfvo-utils / i18n / i18n.js View on Github external
* distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
import IntlObj from 'intl';
import IntlMessageFormatObj from 'intl-messageformat';
import IntlRelativeFormatObj from 'intl-relativeformat';
import createFormatCacheObj from 'intl-format-cache';
import i18nJson from 'i18nJson';
/*
 Intl libs are using out dated transpailer from ecmascript6.
 *  TODO: As soon as they fix it, remove this assignments!!!
 * */
var Intl = window.Intl || IntlObj.default,
    IntlMessageFormat = IntlMessageFormatObj.default,
    IntlRelativeFormat = IntlRelativeFormatObj.default,
    createFormatCache = createFormatCacheObj.default;

/*extract locale*/
var _locale = window.localStorage && localStorage.getItem('user_locale');
if (!_locale) {
    if (window.navigator) {
        _locale = navigator.language || navigator.userLanguage;

        //For now removing the dashes from the language.
        let indexOfDash = _locale.indexOf('-');
        if (-1 !== indexOfDash) {
            _locale = _locale.substr(0, indexOfDash);
        }
    }
    if (!_locale) {

intl-messageformat

Formats ICU Message strings with number, date, plural, and select placeholders to create localized messages.

BSD-3-Clause
Latest version published 4 days ago

Package Health Score

100 / 100
Full package analysis