aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Jaggars <jhjaggars@gmail.com>2012-01-31 09:22:56 -0600
committerJesse Jaggars <jhjaggars@gmail.com>2012-01-31 09:22:56 -0600
commita698f42b93f7469cdf2b0f69b92a725e9a70f804 (patch)
treee7e200bc9df522f0697217bd022585168dfe605a
parentd3af119863bd525e085495d01d329dd4384275cd (diff)
downloadsos-a698f42b93f7469cdf2b0f69b92a725e9a70f804.tar.gz
Fixing i18n in jython/java use case.
ResourceBundle was unable to find the translation text resources. This change creates a new classloader loaded with the jarfile that contains the translations and passes it in directly to ResourceBundle.getBundle. Additionally the eap6 plugin has been renamed to as7.
-rw-r--r--Makefile4
-rw-r--r--sos/__init__.py61
2 files changed, 48 insertions, 17 deletions
diff --git a/Makefile b/Makefile
index a4b8513f..5d6cbf29 100644
--- a/Makefile
+++ b/Makefile
@@ -84,9 +84,9 @@ po: clean
cp $(PO_DIR)/en.properties $(PO_DIR)/en_US.properties
-eap6: po
+as7: po
cp -r sos/* $(SRC_BUILD)/sos/
- find $(SRC_BUILD)/sos/plugins/ -not -name "*eap6.py" -not -name "*__init__.py" -type f -delete
+ find $(SRC_BUILD)/sos/plugins/ -not -name "*as7.py" -not -name "*__init__.py" -type f -delete
zip: po
zip -r $(ZIP_DEST) sos
diff --git a/sos/__init__.py b/sos/__init__.py
index 8d45216a..1829e7ee 100644
--- a/sos/__init__.py
+++ b/sos/__init__.py
@@ -15,24 +15,55 @@
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+"""
+This module houses the i18n setup and message function. The default is to use
+gettext to internationalize messages. If the client calls set_i18n and passes a
+path to a resource bundle the _ method will be changed to use java
+ResourceBundle code to present messages.
+"""
+
__version__ = "@SOSVERSION@"
-try:
- from java.util import ResourceBundle
+import gettext
+gettext_dir = "/usr/share/locale"
+gettext_app = "sos"
+
+gettext.bindtextdomain(gettext_app, gettext_dir)
+
+def _default(msg):
+ return gettext.dgettext(gettext_app, msg)
+
+_sos = _default
+
+def _get_classloader(jarfile):
+ """Makes a new classloader loaded with the jarfile. This is useful since it
+ seems very difficult to get jars added to the correct classpath for
+ ResourceBundle.getBundle to find."""
+ from java.net import URLClassLoader, URL
+ from java.io import File
+ import jarray
- rb = ResourceBundle.getBundle("sos.po.sos")
+ file_ = File(jarfile)
+ ary = jarray.array([file_.toURL()], URL)
+ classloader = URLClassLoader.newInstance(ary)
+ return classloader
- def _sos(msg):
- try:
- return rb.getString(msg).encode('utf-8')
- except:
- return msg
-except:
- import gettext
- gettext_dir = "/usr/share/locale"
- gettext_app = "sos"
+def set_i18n(path=None, basename="sos.po.sos"):
+ """Use this method to change the default i18n behavior from gettext to java
+ ResourceBundle.getString. This is really only useful when using jython.
+ Path is expected to be the path to a jarfile that contains the translation
+ files (.properties)"""
+ try:
+ from java.util import ResourceBundle, Locale
- gettext.bindtextdomain(gettext_app, gettext_dir)
+ rb = ResourceBundle.getBundle(basename,
+ Locale.getDefault(), _get_classloader(path))
- def _sos(msg):
- return gettext.dgettext(gettext_app, msg)
+ def _java(msg):
+ try:
+ return rb.getString(msg).encode('utf-8')
+ except:
+ return msg
+ _sos = _java
+ except:
+ pass