Skip to content

Commit

Permalink
[#959] add ITranslations interface
Browse files Browse the repository at this point in the history
This allows extensions to merge in their own translations with the main
ckan mo file. Each extension should have an 'i18n' directory in the root
directory of their repo containing its translations. The gettext domain
(and hence filename) of the .mo files should be ckanext-{extname}
  • Loading branch information
joetsoi committed Jun 16, 2015
1 parent bb9bc11 commit 7d22c0c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
16 changes: 14 additions & 2 deletions ckan/lib/i18n.py
Expand Up @@ -2,10 +2,14 @@

from babel import Locale, localedata
from babel.core import LOCALE_ALIASES
from babel.support import Translations
from pylons import config
from pylons import i18n
import pylons

import ckan.i18n
from ckan.plugins import PluginImplementations
from ckan.plugins.interfaces import ITranslations

LOCALE_ALIASES['pt'] = 'pt_BR' # Default Portuguese language to
# Brazilian territory, since
Expand Down Expand Up @@ -121,9 +125,17 @@ def _set_lang(lang):
if config.get('ckan.i18n_directory'):
fake_config = {'pylons.paths': {'root': config['ckan.i18n_directory']},
'pylons.package': config['pylons.package']}
i18n.set_lang(lang, pylons_config=fake_config)
i18n.set_lang(lang, pylons_config=fake_config, class_=Translations)
else:
i18n.set_lang(lang)
i18n.set_lang(lang, class_=Translations)

for plugin in PluginImplementations(ITranslations):
pylons.translator.merge(Translations.load(
dirname=plugin.directory(),
locales=plugin.locales(),
domain=plugin.domain()
))


def handle_request(request, tmpl_context):
''' Set the language for the request '''
Expand Down
28 changes: 28 additions & 0 deletions ckan/lib/plugins.py
Expand Up @@ -524,3 +524,31 @@ def activity_template(self):
return 'organization/activity_stream.html'

_default_organization_plugin = DefaultOrganizationForm()


class DefaultTranslation(object):
def directory(self):
'''Change the directory of the *.mo translation files
The default implementation assumes the plugin is
ckanext/myplugin/plugin.py and the translations are stored in
i18n/
'''
import os
return os.path.abspath(os.path.join(os.path.dirname(__file__),
'../../', 'i18n'))
def locales(self):
'''Change the list of locales that this plugin handles
By default the will assume any directory in subdirectory returned
by self.directory() is a locale handled by this plugin
'''
import os
directory = self.directory()
return [ d for
d in os.listdir(directory)
if os.path.isdir(os.path.join(directory, d))
]

def domain(self):
return 'ckanext-{name}'.format(name=self.name)
20 changes: 20 additions & 0 deletions ckan/plugins/interfaces.py
Expand Up @@ -22,6 +22,7 @@
'ITemplateHelpers',
'IFacets',
'IAuthenticator',
'ITranslations',
]

from inspect import isclass
Expand Down Expand Up @@ -1432,3 +1433,22 @@ def abort(self, status_code, detail, headers, comment):
'''called on abort. This allows aborts due to authorization issues
to be overriden'''
return (status_code, detail, headers, comment)


class ITranslations(Interface):
def directory(self):
'''Change the directory of the *.mo translation files
The default implementation assumes the plugin is
ckanext/myplugin/plugin.py and the translations are stored in
i18n/
'''
def locales(self):
'''Change the list of locales that this plugin handles
By default the will assume any directory in subdirectory returned
by self.directory() is a locale handled by this plugin
'''

def domain(self):
'''Change the gettext domain handled by this plugin'''

0 comments on commit 7d22c0c

Please sign in to comment.