Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement app example using webpack #481

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a5cddc2
Examples: Include app example using webpack
rxaviers Aug 14, 2015
38b5a7d
fixup! Examples: Include app example using webpack
rxaviers Aug 24, 2015
2573e2b
fixup! Examples: Include app example using webpack
rxaviers Aug 24, 2015
ef8dd29
fixup! Examples: Include app example using webpack
rxaviers Aug 24, 2015
a85966e
fixup! Examples: Include app example using webpack
rxaviers Aug 24, 2015
95f42bf
fixup! Examples: Include app example using webpack
rxaviers Aug 24, 2015
17089fb
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
e41c0a4
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
7204957
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
014e900
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
e1adbdc
Examples: Update and rename en.json to de.json
jzaefferer Aug 25, 2015
7ace189
fixup! Update and rename en.json to de.json
rxaviers Aug 25, 2015
ecbfa93
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
c2affb6
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
4c8dd9c
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
1e442ab
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
77e613c
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
e3c17bf
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
a77b1ce
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
9f3acdc
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
a9643a6
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
15f2905
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
a219f72
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
0e9a073
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
092dd56
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
528d481
fixup! Examples: Include app example using webpack
rxaviers Aug 25, 2015
92a4bc9
fixup! Examples: Include app example using webpack
rxaviers Aug 26, 2015
d6d91a9
fixup! Examples: Include app example using webpack
rxaviers Aug 26, 2015
b612f25
Examples: Update es.json
isaacdurazo Aug 25, 2015
9b6414a
Examples: zh (using Google Translator)
rxaviers Aug 26, 2015
20b49eb
fixup! Examples: Include app example using webpack
rxaviers Aug 26, 2015
cc621b9
Examples: ar (using Google translate)
rxaviers Aug 26, 2015
e20b6e4
Example: add Russian locale to webpack example
markelog Aug 26, 2015
f2ef378
fixup! Example: add Russian locale to webpack example
rxaviers Aug 27, 2015
4e3687c
fixup! Examples: Include app example using webpack
rxaviers Aug 27, 2015
4a07c28
fixup! Examples: ar (using Google translate)
rxaviers Aug 27, 2015
8be8f18
fixup! Examples: zh (using Google Translator)
rxaviers Aug 27, 2015
85cd21f
fixup! Examples: Update es.json
rxaviers Aug 27, 2015
e2057e3
fixup! Examples: Include app example using webpack
rxaviers Aug 27, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Node.js module.
- [Usage](#usage)
- [Performance](#performance)
- [Compilation and the Runtime modules](#compilation-and-the-runtime-modules)
- [Examples](#examples)
- [API](#api)
- [Core](#core-module)
- [Date module](#date-module)
Expand Down Expand Up @@ -248,12 +249,8 @@ can cherry-pick the pieces you need, eg. load `dist/globalize.js` to get
Globalize core, load `dist/globalize/date.js` to extend Globalize with Date
functionalities, etc.

An example is worth a thousand words. Check out our Hello World demo (available
to you in different flavors):
- [Hello World (AMD + bower)](examples/amd-bower/).
- [Hello World (Node.js + npm)](examples/node-npm/).
- [Hello World (plain JavaScript)](examples/plain-javascript/).

An example is worth a thousand words. Check out our [Examples](#examples)
section below.

### Performance

Expand Down Expand Up @@ -290,7 +287,6 @@ created (see [Performance](#performance) above for more information).

To illustrate, see our [Basic Globalize Compiler example][].

[Basic Globalize Compiler example]: examples/globalize-compiler/

#### Globalize Compiler

Expand All @@ -299,6 +295,36 @@ For information about the Globalize Compiler CLI or its JavaScript API, see the

[Globalize Compiler documentation]: https://github.com/jquery-support/globalize-compiler#README

### Examples

The fastest and easiest way to use Globalize is by integrating it into your
existing tools.

- [Application example using webpack and npm](examples/app-npm-webpack/): easy to
get started, automated CLDR loading and precompilation for production, but
requires npm and webpack knowledge.

If you're using a different tool than the one above, but you're comfortable
using JavaScript modules (such as ES6 modules, CommonJS, or AMD) and package
managers like npm or bower, you may want to check out the following examples.
Note you'll need to compile your code for production yourself.

- [Basic example using AMD and bower](examples/amd-bower/): feeding Globalize on
CLDR is not completely transparent.
- [Basic example using Node.js and npm](examples/node-npm/): feeding Globalize
on CLDR is not completely transparent.
- [Basic Globalize Compiler example][]: shows how to use Globalize Compiler CLI.

[Basic Globalize Compiler example]: examples/globalize-compiler/

If you're using none of the tools above, but instead you're using the plain and
old script tags only, the following example may interest you. Note Globalize
allows you to go low level like this. But, acknowledge that you'll need to
handle dependencies and CLDR loading manually yourself.

- [Basic example using plain JavaScript](examples/plain-javascript/): requires
loading CLDR and handling dependencies manually.

## API

### Core module
Expand Down
2 changes: 2 additions & 0 deletions examples/app-npm-webpack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
.tmp-globalize-webpack/
74 changes: 74 additions & 0 deletions examples/app-npm-webpack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Globalize App example using webpack

This example demonstrates how to integrate Globalize with Webpack in your
Application. If you already have an existing Application using Webpack stack,
this example should as well provide you guidance on how to integrate Globalize.
It focuses on the [Globalize Webpack Plugin][], which automates data loading
(CLDR and app messages) during development and automates Globalize compilation
and the usage of Globalize runtime modules for production. It assumes knowledge
of Globalize, npm, and Webpack usage basics.

## Requirements

**1. Install app development dependencies**

This example uses `npm` to download the app development dependencies (i.e.,
Globalize, CLDR data, Cldrjs, Webpack, [Globalize Webpack Plugin][], and
others).

```
npm install
```

## Running the example

### Development mode

```
npm start
```

1. Start a server by running `npm start`, which uses webpack's live reload HMR
(Hot Module Replacement). See `package.json` to understand the actual shell
command that is used.
1. Point your browser at `http://localhost:8080`. Note that your browser will
automatically reload on any changes made to the application code (`app/*.js`
files). Also note that for faster page reload, formatters are created
dynamically and automatically by the [Globalize Webpack Plugin][].
1. Note you can specify the development locale of your choice by setting the
`developmentLocale` property of the Globalize Webpack Plugin on the Webpack
config file.
1. Note that CLDR data and your messages data are automatically loaded by the
[Globalize Webpack Plugin][].
1. Understand the demo by reading the source code. We have comments there for
you.

### Production mode

```
npm run build
```

1. Generate the compiled bundles by running `npm run build`, which will be
created at `./dist`. Note the production bundles are split into three chunks:
(a) vendor, which holds third-party libraries, which in this case means
Globalize Runtime modules, (b) i18n precompiled data, which means the minimum
yet sufficient set of precompiled i18n data that your application needs (one
file for each supported locale), and (c) app, which means your application code.
Also note that all the production code is already minified using UglifyJS. See
`package.json` to understand the actual shell command that is used.
1. Note that your formatters are already precompiled. This is
obvious, but worth emphasizing. It means your formatters are prebuilt, so no client
CPU clock is wasted to generate them and no CLDR or messages data needs to be
dynamically loaded. It means fast to load code (small code) and fast to run
code.
1. Point your browser at `./dist/index.html` to run the application using the
generated production files. Edit this file to display the application using a
different locale (source code has instructions).
1. Understand the demo by reading the source code. We have comments there for
you.

For more information about the plugin, see the [Globalize Webpack Plugin][]
documentation.

[Globalize Webpack Plugin]: https://github.com/rxaviers/globalize-webpack-plugin
55 changes: 55 additions & 0 deletions examples/app-npm-webpack/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var currencyFormatter, dateFormatter, numberFormatter, relativeTimeFormatter, startTime,
Globalize = require( "globalize" );

startTime = new Date();

currencyFormatter = Globalize.currencyFormatter( "USD" );
dateFormatter = Globalize.dateFormatter({ datetime: "medium" });
numberFormatter = Globalize.numberFormatter({ maximumFractionDigits: 2 });
relativeTimeFormatter = Globalize.relativeTimeFormatter( "second" );

document.getElementById( "intro-1" ).innerHTML = Globalize.formatMessage( "intro-1" );

// Standalone table.
document.getElementById( "currency-label" ).innerHTML = Globalize.formatMessage( "currency-label" );
document.getElementById( "currency" ).innerHTML = currencyFormatter( 69900 );

document.getElementById( "date-label" ).innerHTML = Globalize.formatMessage( "date-label" );
document.getElementById( "date" ).innerHTML = dateFormatter( new Date() );

document.getElementById( "number-label" ).innerHTML = Globalize.formatMessage( "number-label" );
document.getElementById( "number" ).innerHTML = numberFormatter( 12345.6789 );

document.getElementById( "relative-time-label" ).innerHTML = Globalize.formatMessage( "relative-time-label" );
document.getElementById( "relative-time" ).innerHTML = relativeTimeFormatter( 0 );

// Messages.
document.getElementById( "message-1" ).innerHTML = Globalize.formatMessage( "message-1", {
currency: currencyFormatter( 69900 ),
date: dateFormatter( new Date() ),
number: numberFormatter( 12345.6789 ),
relativeTime: relativeTimeFormatter( 0 )
});

document.getElementById( "message-2" ).innerHTML = Globalize.formatMessage( "message-2", {
count: 3

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for the demo purposes also increment the count from 0 to some number and back to 0? This would demonstrate how plurals work with different values.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 should be tracked separately by #489

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, great!

});

// Display demo.
document.getElementById( "requirements" ).style.display = "none";
document.getElementById( "demo" ).style.display = "block";

// Refresh elapsed time
setInterval(function() {
var elapsedTime = +( ( startTime - new Date() ) / 1000 ).toFixed( 0 );
document.getElementById( "date" ).innerHTML = dateFormatter( new Date() );
document.getElementById( "relative-time" ).innerHTML = relativeTimeFormatter( elapsedTime );
document.getElementById( "message-1" ).innerHTML = Globalize.formatMessage( "message-1", {
currency: currencyFormatter( 69900 ),
date: dateFormatter( new Date() ),
number: numberFormatter( 12345.6789 ),
relativeTime: relativeTimeFormatter( elapsedTime )
});

}, 1000);

82 changes: 82 additions & 0 deletions examples/app-npm-webpack/index-template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!doctype html>
<html>
<head>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing a <html> tag?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Globalize App example using Webpack</title>
</head>
<body>

<h1>Globalize App example using Webpack</h1>

<div id="requirements">
<h2>Requirements</h2>
<ul>
<li>Read README.md for instructions on how to run the demo.
</li>
</ul>
</div>

<div id="demo" style="display: none">
<p id="intro-1">Use Globalize to internationalize your application.</p>
<table border="1" style="marginBottom: 1em;">
<tbody>
<tr>
<td><span id="number-label">Standalone Number</span></td>
<td>"<span id="number"></span>"</td>
</tr>
<tr>
<td><span id="currency-label">Standalone Currency</span></td>
<td>"<span id="currency"></span>"</td>
</tr>
<tr>
<td><span id="date-label">Standalone Date</span></td>
<td>"<span id="date"></span>"</td>
</tr>
<tr>
<td><span id="relative-time-label">Standalone Relative Time</span></td>
<td>"<span id="relative-time"></span>"</td>
</tr>
</tbody>
</table>
<p id="message-1">
An example of a message using mixed numbers "{number}", currencies "{currency}", dates "{date}", and relative time "{relativeTime}".
</p>
<p id="message-2">
An example of a message with pluralization support:
{count, plural,
one {You have one remaining task}
other {You have # remaining tasks}
}.
</p>
</div>

{%
var hasShownLocaleHelp;
for ( var chunk in o.htmlWebpackPlugin.files.chunks ) {
if ( /globalize-compiled-data-/.test( chunk ) && chunk !== "globalize-compiled-data-en" ) {
if ( !hasShownLocaleHelp ) {
hasShownLocaleHelp = true;
%}
<!--
Load support for the `en` (English) locale.

For displaying the application in a different locale, replace `en` with
whatever other supported locale you want, e.g., `pt` (Portuguese).

For supporting additional locales simultaneously and then having your
application to change it dynamically, load the multiple files here. Then,
use `Globalize.locale( <locale> )` in your application to dynamically set
it.
-->
{% } %}
<!-- <script src="{%=o.htmlWebpackPlugin.files.chunks[chunk].entry %}"></script> -->
{% } else { %}
<script src="{%=o.htmlWebpackPlugin.files.chunks[chunk].entry %}"></script>
{%
}
}
%}

</body>
</html>
21 changes: 21 additions & 0 deletions examples/app-npm-webpack/messages/ar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"ar": {
"intro-1": "استخدام عولمة لتدويل طلبك.",
"number-label": "عدد",
"currency-label": "عملة",
"date-label": "تاريخ",
"relative-time-label": "الوقت النسبي",
"message-1": "مثال على رسالة باستخدام أرقام مختلطة \"{number}\"، \"{currency}\" العملات، يعود \"{date}\"، ووقت النسبي \"{relativeTime}\".",
"message-2": [
"مثال على رسالة بدعم صيغة الجمع:",
"{count, plural,",
" zero {لا يوجد لديك المهام المتبقية}",
" one {لديك واحد المتبقية المهام}",
" two {لديك اثنين من المهام المتبقية}",
" few {لديك # المهام المتبقية}",
" many {لديك # المهام المتبقية}",
" other {لديك # المهام المتبقية}",
"}."
]
}
}
17 changes: 17 additions & 0 deletions examples/app-npm-webpack/messages/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"de": {
"intro-1": "Verwenden Sie Globalize um Ihre Anwendung zu internationalisieren.",
"number-label": "Zahl",
"currency-label": "Währung",
"date-label": "Datum",
"relative-time-label": "Relative Zeit",
"message-1": "Ein Beispieltext der eine Mischung aus Zahl \"{number}\", Währung \"{currency}\", Datum \"{date}\", und relativer Zeit \"{relativeTime}\" verwendet.",
"message-2": [
"Ein Beispieltext mit Unterstützung von Plural Formen: ",
"{count, plural,",
" one {Sie haben noch eine Aufgabe}",
" other {Sie haben noch # verbliebende Aufgaben}",
"}."
]
}
}
17 changes: 17 additions & 0 deletions examples/app-npm-webpack/messages/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"en": {
"intro-1": "Use Globalize to internationalize your application.",
"number-label": "Number",
"currency-label": "Currency",
"date-label": "Date",
"relative-time-label": "Relative Time",
"message-1": "An example of a message using mixed numbers \"{number}\", currencies \"{currency}\", dates \"{date}\", and relative time \"{relativeTime}\".",
"message-2": [
"An example of a message with pluralization support:",
"{count, plural,",
" one {You have one remaining task}",
" other {You have # remaining tasks}",
"}."
]
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jzaefferer would you like to add support for de? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@isaacdurazo would you like to add support for es? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record, Jörn has provided de translations.

17 changes: 17 additions & 0 deletions examples/app-npm-webpack/messages/es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"es": {
"intro-1": "Usa Globalize para internacionalizar tu aplicación.",
"number-label": "Número",
"currency-label": "Moneda",
"date-label": "Fecha",
"relative-time-label": "Tiempo Relativo",
"message-1": "Un ejemplo de mensaje usando números mixtos \"{number}\", monedas \"{currency}\", fechas \"{date}\", y tiempo relativo \"{relativeTime}\".",
"message-2": [
"Un ejemplo de mensaje con soporte de pluralizaciónt:",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small typo, extra t at the end of pluralización.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, d317164.

"{count, plural,",
" one {Tienes una tarea restante}",
" other {Tienes # tareas restantes}",
"}."
]
}
}
17 changes: 17 additions & 0 deletions examples/app-npm-webpack/messages/pt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"pt": {
"intro-1": "Use o Globalize para internacionalizar sua aplicação.",
"number-label": "Número",
"currency-label": "Moeda",
"date-label": "Data",
"relative-time-label": "Tempo relativo",
"message-1": "Um exemplo de mensagem com mistura de números \"{number}\", moedas \"{currency}\", datas \"{date}\", e tempo relativo \"{relativeTime}\".",
"message-2": [
"Um exemplo de message com suporte a pluralização:",
"{count, plural,",
" one {Você tem uma tarefa restante}",
" other {Você tem # tarefas restantes}",
"}."
]
}
}
19 changes: 19 additions & 0 deletions examples/app-npm-webpack/messages/ru.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"ru": {
"intro-1": "Используйте Globalize для интернационализиции вашего приложения.",
"number-label": "Число",
"currency-label": "Валюта",
"date-label": "Дата",
"relative-time-label": "Относительное время",
"message-1": "Пример сообщения со смешанными числами \"{number}\", валютой \"{currency}\", датами \"{date}\" и относительным временем \"{relativeTime}\".",
"message-2": [
"Пример сообщения с поддержкой множественного числа:",
"{count, plural,",
" one {У вас осталась одна задача}",
" many {У вас осталось # задач}",
" few {У вас осталось # задачи}",
" other {У вас осталось # задачи}",
"}."
]
}
}
Loading