Skip to content

Commit

Permalink
Add KEYSTONE_LOCALEDIR env variable
Browse files Browse the repository at this point in the history
Part of fixing bug #995287

Syncs these two commits from oslo-incubator:

  Support overriding oslo localedir too
  Add a gettextutils.install() helper function

to get a new gettextutils.install() function which allows the default
localedir to be overwritten via an environment variable.

A few things to note:

 - the gettext.install() call is moved from common.config to the
   toplevel scripts to fix cases (e.g. the legacy auth_token middleware)
   where keystone code might be imported by a program who's default
   translation domain is not 'keystone'.

 - we add a gettext.install() call in keystone.test so that tests have
   the _() builtin installed.

Change-Id: I86562b3a65d371673bb21f7179eecc7602bc0775
  • Loading branch information
markmc committed May 29, 2013
1 parent 2879d42 commit 7ce56d5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
3 changes: 3 additions & 0 deletions bin/keystone-all
Expand Up @@ -24,6 +24,7 @@ import pbr.version
from keystone.common import utils
from keystone.common import wsgi_server
from keystone import config
from keystone.openstack.common import gettextutils
from keystone.openstack.common import importutils


Expand Down Expand Up @@ -70,6 +71,8 @@ def serve(*servers):


if __name__ == '__main__':
gettextutils.install('keystone')

dev_conf = os.path.join(possible_topdir,
'etc',
'keystone.conf')
Expand Down
4 changes: 3 additions & 1 deletion bin/keystone-manage
Expand Up @@ -13,11 +13,13 @@ if os.path.exists(os.path.join(possible_topdir,
'__init__.py')):
sys.path.insert(0, possible_topdir)


from keystone import cli
from keystone.openstack.common import gettextutils


if __name__ == '__main__':
gettextutils.install('keystone')

dev_conf = os.path.join(possible_topdir,
'etc',
'keystone.conf')
Expand Down
3 changes: 0 additions & 3 deletions keystone/common/config.py
Expand Up @@ -14,7 +14,6 @@
# License for the specific language governing permissions and limitations
# under the License.

import gettext
import os
import sys

Expand All @@ -23,8 +22,6 @@
from keystone.common import logging


gettext.install('keystone', unicode=1)

_DEFAULT_LOG_FORMAT = "%(asctime)s %(levelname)8s [%(name)s] %(message)s"
_DEFAULT_LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
_DEFAULT_AUTH_METHODS = ['password', 'token']
Expand Down
25 changes: 21 additions & 4 deletions keystone/openstack/common/gettextutils.py
Expand Up @@ -20,14 +20,31 @@
Usual usage in an openstack.common module:
from nova.openstack.common.gettextutils import _
from keystone.openstack.common.gettextutils import _
"""

import gettext
import os


t = gettext.translation('openstack-common', 'locale', fallback=True)
_localedir = os.environ.get('keystone'.upper() + '_LOCALEDIR')
_t = gettext.translation('keystone', localedir=_localedir, fallback=True)


def _(msg):
return t.ugettext(msg)
return _t.ugettext(msg)


def install(domain):
"""Install a _() function using the given translation domain.
Given a translation domain, install a _() function using gettext's
install() function.
The main difference from gettext.install() is that we allow
overriding the default localedir (e.g. /usr/share/locale) using
a translation-domain-specific environment variable (e.g.
NOVA_LOCALEDIR).
"""
gettext.install(domain,
localedir=os.environ.get(domain.upper() + '_LOCALEDIR'),
unicode=True)
3 changes: 3 additions & 0 deletions keystone/test.py
Expand Up @@ -22,6 +22,7 @@
import sys
import time

import gettext
import mox
import nose.exc
from paste import deploy
Expand All @@ -47,6 +48,8 @@
wsgi_server.monkey_patch_eventlet()


gettext.install('keystone', unicode=1)

LOG = logging.getLogger(__name__)
ROOTDIR = os.path.dirname(os.path.abspath(os.curdir))
VENDOR = os.path.join(ROOTDIR, 'vendor')
Expand Down

0 comments on commit 7ce56d5

Please sign in to comment.