Skip to content
Browse files

Merge pull request #40 from stasm/locale_dir

Provide a sample localization
  • Loading branch information...
2 parents 9908571 + e28a642 commit 87abd66456d22d528cb75b84114e834fb0fdb003 @fwenzel fwenzel committed Jun 11, 2011
View
2 .gitignore
@@ -14,4 +14,4 @@ vendor
.noseids
tmp/*
*~
-locale/*
+*.mo
View
94 apps/commons/tests/test_accepted_locales.py
@@ -0,0 +1,94 @@
+import os
+import shutil
+
+from django.conf import settings
+import test_utils
+
+import manage
+
+
+class AcceptedLocalesTest(test_utils.TestCase):
+ """Test lazy evaluation of locale related settings.
+
+ Verify that some localization-related settings are lazily evaluated based
+ on the current value of the DEV variable. Depending on the value,
+ DEV_LANGUAGES or PROD_LANGUAGES should be used.
+
+ """
+ locale = manage.path('locale')
+ locale_bkp = manage.path('locale_bkp')
+
+ @classmethod
+ def setup_class(cls):
+ """Create a directory structure for locale/.
+
+ Back up the existing locale/ directory and create the following
+ hierarchy in its place:
+
+ - locale/en-US/LC_MESSAGES
+ - locale/fr/LC_MESSAGES
+ - locale/templates/LC_MESSAGES
+ - locale/empty_file
+
+ Also, set PROD_LANGUAGES to ('en-US',).
+
+ """
+ if os.path.exists(cls.locale_bkp):
+ raise Exception('A backup of locale/ exists at %s which might '
+ 'mean that previous tests didn\'t end cleanly. '
+ 'Skipping the test suite.' % cls.locale_bkp)
+ cls.DEV = settings.DEV
+ cls.PROD_LANGUAGES = settings.PROD_LANGUAGES
+ cls.DEV_LANGUAGES = settings.DEV_LANGUAGES
+ settings.PROD_LANGUAGES = ('en-US',)
+ os.rename(cls.locale, cls.locale_bkp)
+ for loc in ('en-US', 'fr', 'templates'):
+ os.makedirs(os.path.join(cls.locale, loc, 'LC_MESSAGES'))
+ open(os.path.join(cls.locale, 'empty_file'), 'w').close()
+
+ @classmethod
+ def teardown_class(cls):
+ """Remove the testing locale/ dir and bring back the backup."""
+
+ settings.DEV = cls.DEV
+ settings.PROD_LANGUAGES = cls.PROD_LANGUAGES
+ settings.DEV_LANGUAGES = cls.DEV_LANGUAGES
+ shutil.rmtree(cls.locale)
+ os.rename(cls.locale_bkp, cls.locale)
+
+ def test_build_dev_languages(self):
+ """Test that the list of dev locales is built properly.
+
+ On dev instances, the list of accepted locales should correspond to
+ the per-locale directories in locale/.
+
+ """
+ settings.DEV = True
+ assert (settings.DEV_LANGUAGES == ['en-US', 'fr'] or
+ settings.DEV_LANGUAGES == ['fr', 'en-US']), \
+ 'DEV_LANGUAGES do not correspond to the contents of locale/.'
+
+ def test_dev_languages(self):
+ """Test the accepted locales on dev instances.
+
+ On dev instances, allow locales defined in DEV_LANGUAGES.
+
+ """
+ settings.DEV = True
+ # simulate the successful result of the DEV_LANGUAGES list
+ # comprehension defined in settings.
+ settings.DEV_LANGUAGES = ['en-US', 'fr']
+ assert settings.LANGUAGE_URL_MAP == {'en-us': 'en-US', 'fr': 'fr'}, \
+ ('DEV is True, but DEV_LANGUAGES are not used to define the '
+ 'allowed locales.')
+
+ def test_prod_languages(self):
+ """Test the accepted locales on prod instances.
+
+ On stage/prod instances, allow locales defined in PROD_LANGUAGES.
+
+ """
+ settings.DEV = False
+ assert settings.LANGUAGE_URL_MAP == {'en-us': 'en-US'}, \
+ ('DEV is False, but PROD_LANGUAGES are not used to define the '
+ 'allowed locales.')
View
37 bin/autol10n.sh
@@ -1,37 +0,0 @@
-#!/bin/bash
-
-# Automatically pull L10n dirs from SVN, compile, then push to git.
-# Runs on all project dirs named *-autol10n.
-
-# Settings
-GIT=`/usr/bin/which git`
-FIND=`/usr/bin/which find`
-DEVDIR=$HOME/dev
-
-# Update everything
-for dir in `$FIND "$DEVDIR" -maxdepth 1 -name '*-autol10n'`; do
-cd $dir
- $GIT pull -q origin master
- cd locale
- $GIT svn rebase
-
- # Compile .mo, commit if changed
- ./compile-mo.sh .
- $FIND . -name '*.mo' -exec $GIT add {} \;
- $GIT status
- if [ $? -eq 0 ]; then
- $GIT commit -m 'compiled .mo files (automatic commit)'
- fi
-
- # Push to SVN and git
- $GIT svn dcommit && $GIT push -q origin master
-
- cd ..
-
- $GIT add locale
- $GIT status locale
- if [ $? -eq 0 ]; then
- $GIT commit -m 'L10n update (automatic commit)'
- $GIT push -q origin master
- fi
-done
View
19 bin/compile-mo.sh
@@ -1,19 +1,28 @@
#!/bin/bash
-# syntax:
-# compile-mo.sh locale-dir/
+TARGET=$1
+LOCKFILE="/tmp/compile-mo-${2}.lock"
function usage() {
echo "syntax:"
- echo "compile.sh locale-dir/"
+ echo " compile-mo.sh locale-dir/ [unique]"
+ echo "unique is an optional string that will be used as the name of the lockfile"
exit 1
}
# check if file and dir are there
-if [[ ($# -ne 1) || (! -d "$1") ]]; then usage; fi
+if [[ ($# -gt 2) || (! -d "$TARGET") ]]; then usage; fi
-for lang in `find $1 -type f -name "*.po"`; do
+# check if the lockfile exists
+if [ -e $LOCKFILE ]; then
+ echo "$LOCKFILE present, exiting"
+ exit 99
+fi
+
+touch $LOCKFILE
+for lang in `find $TARGET -type f -name "*.po"`; do
dir=`dirname $lang`
stem=`basename $lang .po`
msgfmt -o ${dir}/${stem}.mo $lang
done
+rm $LOCKFILE
View
29 bin/update_site.py 100644 → 100755
@@ -16,6 +16,7 @@
import sys
from textwrap import dedent
from optparse import OptionParser
+from hashlib import md5
# Constants
PROJECT = 0
@@ -28,10 +29,17 @@
'prod': ['prod', 'master'],
}
+# The URL of the SVN repository with the localization files (*.po). If you set
+# it to a non-empty value, remember to `git rm --cached -r locale` in the root
+# of the project. Example:
+# LOCALE_REPO_URL = 'https://svn.mozilla.org/projects/l10n-misc/trunk/playdoh/locale'
+LOCALE_REPO_URL = ''
+
GIT_PULL = "git pull -q origin %(branch)s"
GIT_SUBMODULE = "git submodule update --init"
+SVN_CO = "svn checkout --force %(url)s locale"
SVN_UP = "svn update"
-COMPILE_PO = "./compile.sh"
+COMPILE_MO = "./bin/compile-mo.sh %(localedir)s %(unique)s"
EXEC = 'exec'
CHDIR = 'chdir'
@@ -41,6 +49,8 @@ def update_site(env, debug):
"""Run through commands to update this site."""
error_updating = False
here = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
+ locale = os.path.join(here, 'locale')
+ unique = md5(locale).hexdigest()
project_branch = {'branch': ENV_BRANCH[env][PROJECT]}
vendor_branch = {'branch': ENV_BRANCH[env][VENDOR]}
@@ -50,17 +60,24 @@ def update_site(env, debug):
(EXEC, GIT_SUBMODULE),
]
+ # Checkout the locale repo into locale/ if the URL is known
+ if LOCALE_REPO_URL and not os.path.exists(os.path.join(locale, '.svn')):
+ commands += [
+ (EXEC, SVN_CO % {'url': LOCALE_REPO_URL}),
+ (EXEC, COMPILE_MO % {'localedir': locale, 'unique': unique}),
+ ]
+
# Update locale dir if applicable
- if os.path.exists(os.path.join(here, 'locale', '.svn')):
+ if os.path.exists(os.path.join(locale, '.svn')):
commands += [
- (CHDIR, os.path.join(here, 'locale')),
+ (CHDIR, locale),
(EXEC, SVN_UP),
- (EXEC, COMPILE_PO),
(CHDIR, here),
+ (EXEC, COMPILE_MO % {'localedir': locale, 'unique': unique}),
]
- elif os.path.exists(os.path.join(here, 'locale', '.git')):
+ elif os.path.exists(os.path.join(locale, '.git')):
commands += [
- (CHDIR, os.path.join(here, 'locale')),
+ (CHDIR, locale),
(EXEC, GIT_PULL % 'master'),
(CHDIR, here),
]
View
31 locale/en_US/LC_MESSAGES/messages.po
@@ -0,0 +1,31 @@
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2011-05-26 18:11-0700\n"
+"PO-Revision-Date: 2011-05-26 18:11-0700\n"
+"Last-Translator: Automatically generated\n"
+"Language-Team: none\n"
+"Language: en_US\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"X-Generator: Translate Toolkit 1.8.0\n"
+"Plural-Forms: nplurals=2; plural=(n != 1);\n"
+
+#: apps/examples/templates/examples/home.html:5
+msgid "Hello world"
+msgstr "Hello world"
+
+#. This is a localizer comment
+#: apps/examples/templates/examples/home.html:9
+msgid "This is a <em>test view</em>."
+msgstr "This is a <em>test view</em>."
+
+#: apps/examples/templates/examples/home.html:11
+msgid "<a href=\"%(docs_url)s\">Learn you some Playdoh</a> and then go build something <strong>awesome</strong>."
+msgstr "<a href=\"%(docs_url)s\">Learn you some Playdoh</a> and then go build something <strong>awesome</strong>."
+
+#: apps/examples/templates/examples/home.html:17
+msgid "Current locale: %(LANG)s.<br> Available locales: %(langs)s."
+msgstr "Current locale: %(LANG)s.<br> Available locales: %(langs)s."
View
29 locale/fr/LC_MESSAGES/messages.po
@@ -0,0 +1,29 @@
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2011-06-03 19:07-0700\n"
+"Last-Translator: Automatically generated\n"
+"Language-Team: none\n"
+"Language: fr\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n > 1);\n"
+
+#: apps/examples/templates/examples/home.html:5
+msgid "Hello world"
+msgstr "Bonjour le monde"
+
+#. This is a localizer comment
+#: apps/examples/templates/examples/home.html:9
+msgid "This is a <em>test view</em>."
+msgstr "Ceci est une <em>vue de test</em>."
+
+#: apps/examples/templates/examples/home.html:11
+msgid "<a href=\"%(docs_url)s\">Learn you some Playdoh</a> and then go build something <strong>awesome</strong>."
+msgstr "<a href=\"%(docs_url)s\">Apprends à jouer avec Playdoh</a> et construis quelque chose de <strong>génial</strong>."
+
+#: apps/examples/templates/examples/home.html:17
+msgid "Current locale: %(LANG)s.<br> Available locales: %(langs)s."
+msgstr "Langue active&nbsp;: %(LANG)s.<br> Langues disponibles&nbsp;: %(langs)s."
View
32 locale/templates/LC_MESSAGES/messages.pot
@@ -0,0 +1,32 @@
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2011-06-03 19:07-0700\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=utf-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"X-Generator: Translate Toolkit 1.8.0\n"
+
+#: apps/examples/templates/examples/home.html:5
+msgid "Hello world"
+msgstr ""
+
+#. This is a localizer comment
+#: apps/examples/templates/examples/home.html:9
+msgid "This is a <em>test view</em>."
+msgstr ""
+
+#: apps/examples/templates/examples/home.html:11
+msgid ""
+"<a href=\"%(docs_url)s\">Learn you some Playdoh</a> and then go build "
+"something <strong>awesome</strong>."
+msgstr ""
+
+#: apps/examples/templates/examples/home.html:17
+msgid "Current locale: %(LANG)s.<br> Available locales: %(langs)s."
+msgstr ""
View
1 settings.py
@@ -48,6 +48,7 @@
TEXT_DOMAIN = 'messages'
STANDALONE_DOMAINS = [TEXT_DOMAIN, 'javascript']
TOWER_KEYWORDS = {'_lazy': None}
+TOWER_ADD_HEADERS = True
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html

0 comments on commit 87abd66

Please sign in to comment.
Something went wrong with that request. Please try again.