我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用locale.bindtextdomain()。
def main(): mark_time("in main()") root_logger = logging.getLogger() root_logger.addHandler(logging.StreamHandler()) if '--debug' in sys.argv: root_logger.setLevel(logging.DEBUG) else: root_logger.setLevel(logging.INFO) # Tell GTK+ to use out translations locale.bindtextdomain('gtimelog', LOCALE_DIR) locale.textdomain('gtimelog') # Tell Python's gettext.gettext() to use our translations gettext.bindtextdomain('gtimelog', LOCALE_DIR) gettext.textdomain('gtimelog') # Make ^C terminate the process signal.signal(signal.SIGINT, signal.SIG_DFL) # Run the app app = Application() mark_time("app created") sys.exit(app.run(sys.argv))
def _win_bindtextdomain(self, localedomain, localedir): """ Help routine for loading and setting up libintl attributes Returns libintl """ from ctypes import cdll try: libintl = cdll.LoadLibrary('libintl-8') libintl.bindtextdomain(localedomain, localedir) libintl.textdomain(localedomain) libintl.bind_textdomain_codeset(localedomain, "UTF-8") except WindowsError: LOG.warning("Localization library libintl not on %PATH%, localization will be incomplete")
def _setup_locale(self): relpath = Environment.get_locale() locale.bindtextdomain(Application.DOMAIN, relpath)
def init(): localedir = os.getenv("IBUS_LOCALEDIR") # Python's locale module doesn't provide all methods on some # operating systems like FreeBSD try: # for non-standard localedir locale.bindtextdomain(DOMAINNAME, localedir) locale.bind_textdomain_codeset(DOMAINNAME, "UTF-8") except AttributeError: pass gettext.bindtextdomain(DOMAINNAME, localedir) gettext.bind_textdomain_codeset(DOMAINNAME, "UTF-8")
def __init__(self, **kwargs): self.ui = None GObject.threads_init() listen() Gtk.Application.__init__(self, application_id='com.github.geigi.cozy') GLib.setenv("PULSE_PROP_media.role", "music", True) import gettext locale.bindtextdomain('cozy', localedir) locale.textdomain('cozy') gettext.install('cozy', localedir)
def init_i18n(languages=None): # Ensure any resources we extract get cleaned up interpreter shutdown atexit.register(pkg_resources.cleanup_resources) # Figure out where the language catalogs are; this will extract them # if the package is frozen localedir = pkg_resources.resource_filename(__name__, 'locale') try: # Use the user's default locale instead of C locale.setlocale(locale.LC_ALL, '') except locale.Error as e: # If locale is not supported, use C which should at least provide # consistency. In this case, don't set a gettext domain to prevent # translation of strings locale.setlocale(locale.LC_ALL, 'C') else: # Set translation domain for GNU's gettext (needed by GTK's Builder) try: locale.bindtextdomain(__project__, localedir) locale.textdomain(__project__) except AttributeError: if sys.platform.startswith('win'): try: # We're on Windows; try and use intl.dll instead import ctypes libintl = ctypes.cdll.LoadLibrary('intl.dll') except OSError: # intl.dll isn't available; give up return else: libintl.bindtextdomain(__project__, localedir) libintl.textdomain(__project__) libintl.bind_textdomain_codeset(__project, 'UTF-8') else: # We're on something else (Mac OS X most likely); no idea what # to do here yet return # Finally, set translation domain for Python's built-in gettext _gettext.bindtextdomain(__project__, localedir) _gettext.textdomain(__project__)
def __init__(self): #get current directory self.install_dir = os.getcwd() #set up logging FORMAT = "%(asctime)s %(funcName)-14s %(lineno)d| %(levelname)-8s | %(message)s" logging.basicConfig(filename='gpt.log',level=logging.DEBUG,filemode='w',format=FORMAT,datefmt="%H:%M:%S") self.log = logging.getLogger(__name__) #Glade files/window configuration gladefile_list = [ "tlcalculator.glade", "gopro.glade", "appwindow.glade", "playerwindow.glade"] self.gladefile = [] for f in gladefile_list: self.gladefile.append(os.path.join(self.install_dir,"herostuff",f)) #css stylesheet self.stylesheet = os.path.join(self.install_dir,"herostuff","gtk.css") self.locales_dir = os.path.join(self.install_dir,'herostuff','po','locale') self.appname = 'GPT' #setting up localization locale.bindtextdomain(self.appname,self.locales_dir) locale.textdomain(self.locales_dir) gettext.bindtextdomain(self.appname,self.locales_dir) gettext.textdomain(self.appname) #check for config file to set up working directory #create file in case it does not exist self.config = os.path.join(os.path.expanduser('~'),".config","gpt.conf") self.defaultwdir = os.path.join(os.path.expanduser('~'),"GP") if os.path.isfile(self.config) is False: self.stdir = self.defaultwdir self.chkdir(self.stdir) self.createconfig(self.stdir) self.kd_supp = True else: self.readconfig() self.show_message(_("Working directory: %s") % self.stdir)
def main(): """ Start the Safe Eyes. """ system_locale = gettext.translation('safeeyes', localedir=Utility.LOCALE_PATH, languages=[Utility.system_locale(), 'en_US'], fallback=True) system_locale.install() # locale.bindtextdomain is required for Glade files # gettext.bindtextdomain(gettext.textdomain(), Utility.LOCALE_PATH) locale.bindtextdomain('safeeyes', Utility.LOCALE_PATH) parser = argparse.ArgumentParser(prog='safeeyes', description=_('description')) group = parser.add_mutually_exclusive_group() group.add_argument('-a', '--about', help=_('show the about dialog'), action='store_true') group.add_argument('-d', '--disable', help=_('disable the currently running safeeyes instance'), action='store_true') group.add_argument('-e', '--enable', help=_('enable the currently running safeeyes instance'), action='store_true') group.add_argument('-q', '--quit', help=_('quit the running safeeyes instance and exit'), action='store_true') group.add_argument('-s', '--settings', help=_('show the settings dialog'), action='store_true') group.add_argument('-t', '--take-break', help=_('Take a break now').lower(), action='store_true') parser.add_argument('--debug', help=_('start safeeyes in debug mode'), action='store_true') parser.add_argument('--version', action='version', version='%(prog)s ' + SAFE_EYES_VERSION) args = parser.parse_args() # Initialize the logging Utility.intialize_logging(args.debug) config = Config() if __running(): logging.info("Safe Eyes is already running") rpc_client = RPCClient(config.get('rpc_port')) if args.about: rpc_client.show_about() elif args.disable: rpc_client.disable_safeeyes() elif args.enable: rpc_client.enable_safeeyes() elif args.settings: rpc_client.show_settings() elif args.take_break: rpc_client.take_break() elif args.quit: rpc_client.quit() else: # Default behavior is opening settings rpc_client.show_settings() sys.exit(0) elif not args.quit: logging.info("Starting Safe Eyes") safeeyes = SafeEyes(system_locale, config) safeeyes.start() Timer(1.0, lambda: __evaluate_arguments(args, safeeyes)).start() Gtk.main()
def _install(domain, localedir, asglobal=False): ''' :param domain: translation domain :param localedir: locale directory :param asglobal: if True, installs the function _() in Python’s builtin namespace. Default is False Private function doing all the work for the :func:`elib.intl.install` and :func:`elib.intl.install_module` functions. ''' # prep locale system if asglobal: locale.setlocale(locale.LC_ALL, '') # on windows systems, set the LANGUAGE environment variable if sys.platform == 'win32' or sys.platform == 'nt': _putenv('LANGUAGE', _getscreenlanguage()) # The locale module on Max OS X lacks bindtextdomain so we specifically # test on linux2 here. See commit 4ae8b26fd569382ab66a9e844daa0e01de409ceb if sys.platform == 'linux2': locale.bindtextdomain(domain, localedir) locale.bind_textdomain_codeset(domain, 'UTF-8') locale.textdomain(domain) # initialize Python's gettext interface gettext.bindtextdomain(domain, localedir) gettext.bind_textdomain_codeset(domain, 'UTF-8') if asglobal: gettext.textdomain(domain) # on windows systems, initialize libintl if sys.platform == 'win32' or sys.platform == 'nt': from ctypes import cdll libintl = cdll.intl libintl.bindtextdomain(domain, localedir) libintl.bind_textdomain_codeset(domain, 'UTF-8') if asglobal: libintl.textdomain(domain) del libintl