Repository: ckan/ckan

Issue:
Tests fail when LANG environment variable is set to German
### CKAN Version if known (or site URL)

2.6.0a
### Please describe the expected behaviour

All the tests on master pass 
### Please describe the actual behaviour

Two tests in `/ckan/tests/legacy/lib/test_navl.py` are failing. Output:

```
..........FF
======================================================================
FAIL: ckan.tests.legacy.lib.test_navl.test_formencode_compat
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/knut/.virtualenvs/ckan/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/Users/knut/viderum/ckan/ckan/tests/legacy/lib/test_navl.py", line 334, in test_formencode_compat
    assert errors == {'email': [u'An email address must contain a single @']}, errors
AssertionError: {'email': [u'Eine E-Mail-Adresse muss genau ein @-Zeichen enthalten']}

======================================================================
FAIL: ckan.tests.legacy.lib.test_navl.test_range_validator
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/knut/.virtualenvs/ckan/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/Users/knut/viderum/ckan/ckan/tests/legacy/lib/test_navl.py", line 350, in test_range_validator
    assert errors == {'name': [u'Missing value'], 'email': [u'Please enter a number that is 10 or smaller']}, errors
AssertionError: {'email': [u'Bitte geben Sie eine Zahl ein, die kleiner oder gleich 10 ist'], 'name': [u'Missing value']}
```

When run with `LANG` set to `en_US.UTF-8` the tests pass:

```
user@host:$ LANG="en_US.UTF-8" nosetests --ckan --with-pylons=test-core.ini ckan/tests/legacy/lib/test_navl.py
```

```
............
----------------------------------------------------------------------
Ran 12 tests in 0.005s

OK
```
### What steps can be taken to reproduce the issue?

Run the tests with a non English `LANG`:

```
LANG="de_DE.UTF-8" nosetests --ckan --with-pylons=test-core.ini ckan/tests/legacy/lib/test_navl.py
```



Discussion:
Point 3 from [this part](http://www.formencode.org/en/latest/Validator.html#localization-of-error-messages-i18n) of the documentation of formencode seems to be relevant here.


Resolving patch:
diff --git a/ckan/config/environment.py b/ckan/config/environment.py
--- a/ckan/config/environment.py
+++ b/ckan/config/environment.py
@@ -8,6 +8,7 @@
 
 import sqlalchemy
 from pylons import config
+import formencode
 
 import ckan.config.routing as routing
 import ckan.model as model
@@ -199,6 +200,12 @@ def update_config():
         template_paths = extra_template_paths.split(',') + template_paths
     config['pylons.app_globals'].template_paths = template_paths
 
+    # Set the default language for validation messages from formencode
+    # to what is set as the default locale in the config
+    default_lang = config.get('ckan.locale_default', 'en')
+    formencode.api.set_stdtranslation(domain="FormEncode",
+                                      languages=[default_lang])
+
     # Markdown ignores the logger config, so to get rid of excessive
     # markdown debug messages in the log, set it to the level of the
     # root logger.
diff --git a/ckan/lib/navl/dictization_functions.py b/ckan/lib/navl/dictization_functions.py
--- a/ckan/lib/navl/dictization_functions.py
+++ b/ckan/lib/navl/dictization_functions.py
@@ -6,6 +6,7 @@
 
 from ckan.common import _
 
+
 class Missing(object):
     def __unicode__(self):
         raise Invalid(_('Missing value'))


Test patch:
diff --git a/ckan/tests/lib/test_navl.py b/ckan/tests/lib/test_navl.py
new file mode 100644
--- /dev/null
+++ b/ckan/tests/lib/test_navl.py
@@ -0,0 +1,30 @@
+import nose
+import pylons
+
+from ckan.tests import helpers
+from ckan.config import environment
+
+eq_ = nose.tools.eq_
+
+
+class TestFormencdoeLanguage(object):
+    @helpers.change_config('ckan.locale_default', 'de')
+    def test_formencode_uses_locale_default(self):
+        environment.update_config()
+        from ckan.lib.navl.dictization_functions import validate
+        from ckan.lib.navl.validators import not_empty
+        from formencode import validators
+        schema = {
+            "name": [not_empty, unicode],
+            "email": [validators.Email],
+            "email2": [validators.Email],
+        }
+
+        data = {
+            "name": "fred",
+            "email": "32",
+            "email2": "david@david.com",
+        }
+
+        converted_data, errors = validate(data, schema)
+        eq_({'email': [u'Eine E-Mail-Adresse muss genau ein @-Zeichen enthalten']}, errors)