Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including messageformat-properties-loader with all npm packages installed. Try it out:

require("messageformat/package.json"); // messageformat is a peer dependency. var messageformatPropertiesLoader = require("messageformat-properties-loader")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

messageformat-properties-loader v0.4.0

Locale Property Resource Bundle Loader for Webpack

Property Resource Bundle Loader for Webpack

Loads .properties files into JavaScript as precompiled functions using dot-properties and messageformat.

Property values are parsed directly as ICU MessageFormat. With the default options, will assume that the filename has _ separated parts, of which the second is the two- or three-letter language code as in Java Resource Bundles.

Installation

npm install messageformat@next messageformat-properties-loader

Starting from version 0.4.0, the loader requires messageformat v3, currently in beta.

Usage

For a working demo of the following, run npm install && npm run build in the example/ directory, and then open example/dist/index.html in a browser.

Webpack configuration

{
  test: /\.properties$/,
  loader: 'messageformat-properties-loader',
  options: {
    biDiSupport: false,  // enables bi-directional text support
    defaultLocale: 'en', // used if resolution from filename fails
    encoding: 'auto',    // .properties file encoding, use one of
                         // 'auto', 'latin1', or 'utf8'
    keyPath: false,      // if true, dots '.' key names will result
                         // in multi-level objects -- use a string
                         // value to customize
    pathSep: '_'         // separator for parsing locale from filename
  }
}

Default option values are shown above, though none are required.

messages_en.properties

errors.confirmation: {src} doesn't match {attribute}
errors.accepted: {src} must be accepted
errors.wrong_length: {src} is the wrong length (should be {count, plural, one{1 character} other{# characters}})
errors.equal_to: {src} must be equal to {count}

example.js

import messages from './messages_en.properties'

messages.errors.accepted({ src: 'Terms' })
// 'Terms must be accepted'

messages.errors.wrong_length({ src: 'Foo', count: 42 })
// 'Foo is the wrong length (should be 42 characters)'
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free