I am trying to make a simple localization module that takes a key name and returns the localized string based on the language given. The language is one of the constants and maps to a python file that contains the string table. I want to do this dynamically at runtime. Below is my approach, but GAE does not support the imp module. Is there an alternative way to do this?
import logging import imp import localizable LANGUAGE_EN = "en" LANGUAGE_JP = "ja" class Localizer(object): """ Returns a localized string corresponding to unique keys """ @classmethod def localize(cls, language = LANGUAGE_EN, key = None): user_language = imp.load_source("localizable.%s" % language, "/") if (user_language): return user_language.Locale.localize(key) else: logging.error("Localizable file was not found") return ""
I put the language files in localizable/en.py, etc.