Permalink
Browse files

Replace node-gettext with using JSON files. Fixes #12

* Adds Gobbledygook Fixes #9
* Brings code closer to browserid's changes to i18n.js
* Replaces `locale_directory` with `translation_directory` in options
* Loses ability to do ngettext :(
  • Loading branch information...
1 parent 2bec592 commit f15b96a6215d0ec4350eedb407e89fc4cfcc5509 @ozten ozten committed Jan 31, 2013
View
@@ -27,15 +27,15 @@ In your app where you setup express or connect:
supported_languages: ['en-US', 'de', 'es', 'db-LB', 'it-CH'],
default_lang: 'en-US',
debug_lang: 'it-CH',
- locale_directory: 'locale'
+ translation_directory: 'i18n'
}));
This block sets up the middleware and views with gettext support. We declare
support for English, German, Spanish, and two debug locales (more on this later).
In your routes, you can use the gettext function in ``.js`` files.
- exports.homepage = function (req, resp) {
+ exports.homepage = function(req, resp) {
resp.render('home', {title: req.gettext("Hey, careful, man, there's a beverage here!")});
};
@@ -53,7 +53,10 @@ In your templates files, you can use the gettext function in ``.ejs`` files:
i18n-abide also provides a ``format`` function for string interpolation.
-These are both server side translations with ``node-gettext``. If you also want to do client-side translations,
+These are both server side translations and client side translations. Server side works out of the box
+and is the most common use case.
+
+If you also want to do client-side translations,
i18n-abide provides ``lib/gettext.js`` and you can do the same in ``.js`` and ``.ejs`` files.
## Setup Gettext
View
@@ -11,7 +11,7 @@ Any copy, label, or error message that will be shown to users **should** be wrap
These strings must be evaluated in the scope of a request, so we know which locale the user has.
-In JavaScript or EJS templates use `gettext` or `ngettext`. If you need to do string interpolation, use the
+In JavaScript or EJS templates use `gettext`. If you need to do string interpolation, use the
[format](../lib/i18n.js) function, which is kind of like node.js' util.format, except crappier.
Using `_` is more idiomatic, but conflicts with `underscore.js` on the client side JS and EJS files.
@@ -26,7 +26,7 @@ The request object and the response's template context have the following variab
* lang - User's preferred language
* lang_dir - rtl or ltr (BIDI language support)
* locale - OS level locale code
- * gettext, ngettext - Gettext functions
+ * gettext - Gettext function
* format - for string interpolation
## Tools
@@ -46,16 +46,20 @@ config file which you'll like to validate. Examples:
## Debugging
-If code is evaluated in node.js (server-side) then node-gettext is providing the string
-translation. Strings are from the messages.mo files under the locale directory. MO files
-are binary, compiled from the PO files.
+If code is evaluated in node.js (server-side) then translation is provided from the
+`i18n/{locale}/messages.json` file which contains the translation. These JSON files come
+from PO files.
-If code is evaluated on the client-side, then resources/static/shared/gettext.js is in
-the house... strings are from resources/static/i18n JSON files.
+If code is evaluated on the client-side, then `static/gettext.js` is in
+the house... strings are `i18n/{locale}/client.json` files.
If code is evaluated in your head, then clearly we are post-singularity. Why are you
still using gettext?
+You can change `i18n` in the above to examples by setting the `translation_directory` in your
+options to i18n-abide. If you do server side and client side strings, it is recommended that you
+put your translation_directory under a web accessible directory like `static`.
+
Use the eo locale for development and debugging. It is auto-translated with:
for catalog in messages client; do
echo "Translating ${catalog}.po"
@@ -18,7 +18,7 @@ app.configure(function(){
supported_languages: ['en-US', 'de', 'es', 'db-LB', 'it-CH'],
default_lang: 'en-US',
debug_lang: 'it-CH',
- locale_directory: 'locale'
+ translation_directory: 'i18n'
}));
app.use(express.bodyParser());
app.use(express.methodOverride());
@@ -37,7 +37,6 @@ app.configure('production', function(){
// Routes
app.get('/', routes.index);
-
-app.listen(3001, function(){
+app.listen(process.env.PORT || 3000, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
@@ -1,11 +1,11 @@
;var json_locale_data = {
"messages": {
"": {
- "Project-Id-Version": " express 2\nReport-Msgid-Bugs-To: \nPOT-Creation-Date: 2012-06-24 09:50+0200\nPO-Revision-Date: 2012-06-24 09:50+0200\nLast-Translator: Automatically generated\nLanguage-Team: none\nLanguage: db_LB\nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\n"
+ "Project-Id-Version": " PACKAGE VERSION\nReport-Msgid-Bugs-To: \nPOT-Creation-Date: 2012-06-24 09:50+0200\nPO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\nLast-Translator: FULL NAME <EMAIL@ADDRESS>\nLanguage-Team: LANGUAGE <LL@li.org>\nLanguage: \nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\n"
},
"Hello, world!": [
null,
- ""
+ "‮Hǝʅʅo´ ʍoɹʅp¡"
]
}
}
@@ -1,11 +1,11 @@
;var json_locale_data = {
"messages": {
"": {
- "Project-Id-Version": " express 2\nReport-Msgid-Bugs-To: \nPOT-Creation-Date: 2012-06-24 09:50+0200\nPO-Revision-Date: 2012-06-24 09:50+0200\nLast-Translator: Automatically generated\nLanguage-Team: none\nLanguage: de\nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\nPlural-Forms: nplurals=2; plural=(n != 1);\n"
+ "Project-Id-Version": " express 2\nReport-Msgid-Bugs-To: \nPOT-Creation-Date: 2012-06-24 09:50+0200\nPO-Revision-Date: 2012-12-14 10:48-0800\nLast-Translator: Austin King <shout@ozten.com>\nLanguage-Team: German\nLanguage: de\nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\nPlural-Forms: nplurals=2; plural=(n != 1);\n"
},
"Hello, world!": [
null,
- ""
+ "Hallo Welt"
]
}
}
@@ -1,7 +1,7 @@
;var json_locale_data = {
"messages": {
"": {
- "Project-Id-Version": " express 2\nReport-Msgid-Bugs-To: \nPOT-Creation-Date: 2012-06-24 09:50+0200\nPO-Revision-Date: 2012-06-24 09:50+0200\nLast-Translator: Automatically generated\nLanguage-Team: none\nLanguage: en_US\nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\nPlural-Forms: nplurals=2; plural=(n != 1);\n"
+ "Project-Id-Version": " express 2\nReport-Msgid-Bugs-To: \nPOT-Creation-Date: 2012-06-24 09:50+0200\nPO-Revision-Date: 2012-12-14 10:49-0800\nLast-Translator: Austin King <shout@ozten.com>\nLanguage-Team: English\nLanguage: en_US\nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\nPlural-Forms: nplurals=2; plural=(n != 1);\n"
},
"Hello, world!": [
null,
@@ -1,11 +1,11 @@
;var json_locale_data = {
"messages": {
"": {
- "Project-Id-Version": " express 2\nReport-Msgid-Bugs-To: \nPOT-Creation-Date: 2012-06-24 09:50+0200\nPO-Revision-Date: 2012-06-24 09:50+0200\nLast-Translator: Automatically generated\nLanguage-Team: none\nLanguage: es\nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\nPlural-Forms: nplurals=2; plural=(n != 1);\n"
+ "Project-Id-Version": " express 2\nReport-Msgid-Bugs-To: \nPOT-Creation-Date: 2012-06-24 09:50+0200\nPO-Revision-Date: 2012-12-14 10:49-0800\nLast-Translator: Austin King <shout@ozten.com>\nLanguage-Team: Spanish\nLanguage: es\nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\nPlural-Forms: nplurals=2; plural=(n != 1);\n"
},
"Hello, world!": [
null,
- ""
+ "¡Hola mundo!"
]
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -13,14 +13,14 @@ var express = require('express'),
var app = express();
app.configure(function(){
- app.set('port', process.env.PORT || 3000);
+ app.set('port', process.env.PORT || 3001);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(i18n.abide({
supported_languages: ['en-US', 'de', 'es', 'db-LB', 'it-CH'],
default_lang: 'en-US',
debug_lang: 'it-CH',
- locale_directory: 'locale'
+ translation_directory: 'i18n'
}));
app.use(express.favicon());
app.use(express.logger('dev'));
@@ -0,0 +1,12 @@
+;var json_locale_data = {
+ "messages": {
+ "": {
+ "Project-Id-Version": " express 3\nReport-Msgid-Bugs-To: \nPOT-Creation-Date: 2012-06-24 09:50+0200\nPO-Revision-Date: 2012-06-24 09:50+0200\nLast-Translator: Automatically generated\nLanguage-Team: none\nLanguage: db_LB\nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\n"
+ },
+ "Hello, world!": [
+ null,
+ ""
+ ]
+ }
+}
+;
@@ -0,0 +1,12 @@
+;var json_locale_data = {
+ "messages": {
+ "": {
+ "Project-Id-Version": " express 3\nReport-Msgid-Bugs-To: \nPOT-Creation-Date: 2012-06-24 09:50+0200\nPO-Revision-Date: 2012-06-24 09:50+0200\nLast-Translator: Automatically generated\nLanguage-Team: none\nLanguage: de\nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\nPlural-Forms: nplurals=2; plural=(n != 1);\n"
+ },
+ "Hello, world!": [
+ null,
+ ""
+ ]
+ }
+}
+;
@@ -0,0 +1,12 @@
+;var json_locale_data = {
+ "messages": {
+ "": {
+ "Project-Id-Version": " express 3\nReport-Msgid-Bugs-To: \nPOT-Creation-Date: 2012-06-24 09:50+0200\nPO-Revision-Date: 2012-06-24 09:50+0200\nLast-Translator: Automatically generated\nLanguage-Team: none\nLanguage: en_US\nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\nPlural-Forms: nplurals=2; plural=(n != 1);\n"
+ },
+ "Hello, world!": [
+ null,
+ "Hello, world!"
+ ]
+ }
+}
+;
@@ -0,0 +1,12 @@
+;var json_locale_data = {
+ "messages": {
+ "": {
+ "Project-Id-Version": " express 3\nReport-Msgid-Bugs-To: \nPOT-Creation-Date: 2012-06-24 09:50+0200\nPO-Revision-Date: 2012-06-24 09:50+0200\nLast-Translator: Automatically generated\nLanguage-Team: none\nLanguage: es\nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\nPlural-Forms: nplurals=2; plural=(n != 1);\n"
+ },
+ "Hello, world!": [
+ null,
+ ""
+ ]
+ }
+}
+;
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+echo "Extracting strings"
+mkdir -p locale/templates/LC_MESSAGES
+./node_modules/i18n-abide/bin/extract-pot -l ./locale .
+
+# Create PO files
+for locale in en_US es de db_LB; do
+ echo "Creating ${locale} language files"
+ mkdir -p locale/${locale}/LC_MESSAGES
+ mkdir -p i18n/${locale}
+ msginit --input=./locale/templates/LC_MESSAGES/messages.pot \
+ --output-file=./locale/${locale}/LC_MESSAGES/messages.po \
+ -l ${locale} --no-translator
+ echo "You can translate i18n/${locale}/messages.json"
+done
+
+# Convert PO files to JSON
+./node_modules/i18n-abide/bin/compile-json.sh locale i18n
+
+# Make current version of the code happy
+./node_modules/i18n-abide/bin/compile-mo.sh locale
Oops, something went wrong.

0 comments on commit f15b96a

Please sign in to comment.