PHP Classes

File: test/py/test.py

Recommend this page to a friend!
  Classes of Nikos M.   Localizer   test/py/test.py   Download  
File: test/py/test.py
Role: Auxiliary data
Content typex: text/plain
Description: Auxiliary data
Class: Localizer
Get localized texts in PHP, JavaScript and Python
Author: By
Last change:
Date: 3 months ago
Size: 2,125 bytes
 

Contents

Class file image Download
# -*- coding: utf-8 -*- import os, sys DIR = os.path.dirname(os.path.abspath(__file__)) def import_module(name, path): #import imp #try: # mod_fp, mod_path, mod_desc = imp.find_module(name, [path]) # mod = getattr( imp.load_module(name, mod_fp, mod_path, mod_desc), name ) #except ImportError as exc: # mod = None # sys.stderr.write("Error: failed to import module ({})".format(exc)) #finally: # if mod_fp: mod_fp.close() #return mod import importlib.util, sys spec = importlib.util.spec_from_file_location(name, path+name+'.py') mod = importlib.util.module_from_spec(spec) sys.modules[name] = mod spec.loader.exec_module(mod) return getattr(mod, name) # import the Localizer.py (as a) module, probably you will want to place this in another dir/package Localizer = import_module('Localizer', os.path.join(DIR, '../../src/py/')) if not Localizer: print ('Could not load the Localizer Module') sys.exit(1) l10n = Localizer() # setup supported locales l10n.locale('en', {}) l10n.locale('el', { 'I want to say {0}' : '???? ?? ?? {0}', 'hello to you' : '???? ?? ????', 'hello to all' : '???? ?? ?????', '@' : { # specific context 'ctx1' : { 'hello to you' : '???? ?? ???? ????' } } }) # set current locale l10n.locale('el', True) # UTF8 BOM UTF8_BOM = b"\xEF\xBB\xBF" sys.stdout.reconfigure(encoding='utf-8') sys.stdout.buffer.write(UTF8_BOM) sys.stdout.flush() print('Localizer.VERSION = ' + Localizer.VERSION) print(l10n.locale()) print(l10n.l('hello to you')) print(l10n.l('hello to all')) print(l10n.l('hello to you', '???? ?? ????')) print(l10n.l('hello to all', '???? ?? ?????')) print(l10n.l('I want to say {0}', (l10n.l('hello to you'),))) print(l10n.l('I want to say {0}', (l10n.l('hello to all'),))) print(l10n.ln(1, 'hello to you', 'hello to all')) print(l10n.ln(2, 'hello to you', 'hello to all')) print(l10n.l('hello to you', '???? ?? ???? ????')) print(l10n.l(('hello to you','ctx1')))