Giter Club home page Giter Club logo

po-loader's Introduction

PO loader for webpack

Usage

Documentation: Using loaders

// Use it explicitly
var messages = require("json!po!./locale/en_US/LC_MESSAGES/messages.po");

// Or add a loader into your webpack.config.js
loaders: [
   {test: /\.po$/, loader: 'json!po'}
]

// And then require it like this
var messages = require("./locale/en_US/LC_MESSAGES/messages.po");

See po2json for a list of possible options. Use the format option to change the output format, e.g. json!po?format=jed.

Locale module code:

'use strict'

Jed = require 'jed'

langRaw = window.navigator.userLanguage || window.navigator.language
langParts = langRaw.replace('-', '_').split('_')
multipart = (langParts.length > 1)
# The Locale consists of a language and territory
language = langParts[0]
territory = if multipart then langParts[1].toUpperCase()
# Set full
locale = if multipart then "#{language}_#{territory}" else language


# There is only one variable here and it Webpack expands its search using a regex
# to find all of the messages. The constant LOCALE_ROOT is evaluated at compile time
getLangLoader = (locale) ->
  bundleLoader = require "bundle!#{LOCALE_ROOT}/#{locale}/LC_MESSAGES/messages.po"
  return bundleLoader


i18n = null
module.exports =
  gettext: (message) -> i18n.gettext message
  ngettext: (msg1, msg2, n) -> i18n.ngettext msg1, msg2, n

  init: (loadApp) ->
    # Try to load the locale specified by the browser. Webpack will throw an exception
    # if it does not exist since it has been required with a regex. Then if the
    # locale has both both parts then try to load the base language without a territory
    # code (ex. 'es', 'en').  If this fails then load the default language (ex.
    # 'en_US'). If the locale is not multipart then just fallback to the default
    # language. This allows for a single base language to be used without territories
    # or with incomplete coverage for all territories.
    try
      waitForLangChunk = getLangLoader locale
    catch eLocale
      if multipart
        try
          waitForLangChunk = getLangLoader language
        catch eLanguage
          waitForLangChunk = getLangLoader LOCALE_DEFAULT
      else
        waitForLangChunk = getLangLoader LOCALE_DEFAULT

    waitForLangChunk (messages) ->
      i18n = new Jed
        domain: 'messages'
        locale_data:
          messages: messages

      console.log 'messages:', messages
      loadApp()

License

MIT (http://www.opensource.org/licenses/mit-license.php)

po-loader's People

Contributors

dschissler avatar dundalek avatar

Watchers

 avatar James Cloos 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.