Coder Social home page Coder Social logo

messageformat / yaml-loader Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 75.0 11 KB

Deprecated; use messageformat-loader instead

Home Page: https://github.com/messageformat/messageformat/tree/master/packages/loader

License: MIT License

JavaScript 100.00%
messageformat yaml webpack

yaml-loader's Introduction

YAML i18n loader for Webpack

Loads YAML files into JavaScript as precompiled functions using yaml-to-messageformat and messageformat.

With the default options, will load Rails i18n YAML files, but can be configured for other contents as well. E.g. for YAML with contents already in ICU MessageFormat, use with options { pluralVariable: null, replacements: [] }.

By default, variables of the form %{foo} and %b will be detected in the input strings, and an object with keys matching the pluralisation classes of the current language (set by having a parent key such as en or fi) will be handled as a proper plural.

Installation

npm install messageformat-yaml-loader

or

yarn add messageformat-yaml-loader

Usage

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

Webpack configuration

{
  test: [/\.yaml$/, /\.yml$/],
  loader: require.resolve('messageformat-yaml-loader'),
  options: {
    biDiSupport: false,
    defaultLocale: 'en',
    includeLocales: null,
    pluralVariable: 'count',
    verbose: false
  }
}

Some of the default option values are shown, but none is required. Most options are passed on to yaml-to-messageformat (see there for their documentation); biDiSupport enables bi-directional text support in messageformat.

messages.yml

en:
  errors:
    format: "%{attribute} %{message}"
    messages:
      confirmation: "doesn't match %{attribute}"
      accepted: "must be accepted"
      wrong_length:
        one: "is the wrong length (should be 1 character)"
        other: "is the wrong length (should be %{count} characters)"
      equal_to: "must be equal to %{count}"

example.js

import messages from './messages.yml'
const { format, messages: errors } = messages.en.errors

errors.accepted()
// 'must be accepted'

format({
  attribute: 'Your message',
  message: errors.wrong_length({ count: 42 })
})
// 'Your message is the wrong length (should be 42 characters)'

yaml-loader's People

Contributors

eemeli avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.