Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #1382: extended_locales.patch

File extended_locales.patch, 8.6 KB (added by chrisz, 5 years ago)

Extended patch that also solves the problem of finding the locales directory

  • turbogears/i18n/tg_gettext.py

     
    22import turbogears 
    33from turbogears.i18n.utils import get_locale 
    44from turbojson.jsonify import jsonify 
    5 from turbogears.util import request_available 
     5from turbogears.util import get_package_name, request_available 
    66import os 
     7import sys 
    78import types 
    89 
    910_catalogs = {} 
    1011 
     12def get_locale_dir(): 
     13    localedir = turbogears.config.get("i18n.locale_dir") 
     14    if not localedir: 
     15        localedir = os.path.join(os.path.dirname( 
     16            sys.modules[get_package_name()].__path__[0]), "locales") 
     17    return localedir 
     18 
    1119def is_locale_supported(locale, domain=None): 
    1220    """Checks if [domain].mo file exists for this language 
    1321    """ 
     22    if not domain: domain = turbogears.config.get("i18n.domain", "messages") 
    1423 
    15     if not domain:domain = turbogears.config.get("i18n.domain", "messages") 
    16     localedir = turbogears.config.get("i18n.locale_dir", "locales") 
     24    localedir = get_locale_dir() 
     25    return os.path.exists(os.path.join(localedir, locale, "LC_MESSAGES", "%s.mo" % domain)) 
    1726 
    18     return os.path.exists(os.path.join(localedir, locale, "LC_MESSAGES", "%s.mo" %domain)) 
    19  
    2027def get_catalog(locale, domain = None): 
    21     """Return translations for given locale.  
     28    """Return translations for given locale. 
    2229    """ 
    23     if domain is None:domain = turbogears.config.get("i18n.domain", "messages") 
     30    if not domain: domain = turbogears.config.get("i18n.domain", "messages") 
    2431 
    2532    catalog = _catalogs.get(domain) 
    2633 
    2734    if not catalog: 
    28  
    2935        catalog = _catalogs[domain] = {} 
    3036 
    3137    messages = catalog.get(locale) 
    3238    if not messages: 
    33  
    34         localedir = turbogears.config.get("i18n.locale_dir", "locales") 
    35         messages = catalog[locale] = translation(domain=domain,  
     39        localedir = get_locale_dir() 
     40        messages = catalog[locale] = translation(domain=domain, 
    3641            localedir=localedir, languages=[locale]) 
    3742 
    3843    return messages 
     
    4045def plain_gettext(key, locale=None, domain=None): 
    4146    """Gets the gettext value for key.Added to builtins as '_'. Returns Unicode string. 
    4247    @param key: text to be translated 
    43     @param locale: locale code to be used.If locale is None, gets the value provided  
     48    @param locale: locale code to be used.If locale is None, gets the value provided 
    4449    by get_locale. 
    4550    """ 
    4651    gettext_func = turbogears.config.get("i18n.gettext", tg_gettext) 
     
    4954def tg_gettext(key, locale=None, domain=None): 
    5055    """Gets the gettext value for key.Added to builtins as '_'. Returns Unicode string. 
    5156    @param key: text to be translated 
    52     @param locale: locale code to be used.If locale is None, gets the value provided by  
     57    @param locale: locale code to be used.If locale is None, gets the value provided by 
    5358    get_locale. 
    5459    """ 
    5560    if locale is None:locale = get_locale() 
     
    7479 
    7580    if num==1: 
    7681 
    77         return plain_gettext(key1, locale)  
     82        return plain_gettext(key1, locale) 
    7883 
    7984    else: 
    8085 
    81         return plain_gettext(key2, locale)  
     86        return plain_gettext(key2, locale) 
    8287 
    8388class lazystring(object): 
    8489    """Has a number of lazily evaluated functions replicating a string. Just override the eval() method to produce the actual value. 
  • turbogears/qstemplates/quickstart/setup.py_tmpl

     
    44import os 
    55execfile(os.path.join("${package}", "release.py")) 
    66 
     7packages=find_packages() 
     8package_data = find_package_data(where='${package}', 
     9    package='${package}') 
     10if os.path.isdir('locales'): 
     11    packages.append('locales') 
     12    package_data.update(find_package_data(where='locales', 
     13        exclude=('*.po',), only_in_packages=False)) 
     14 
    715setup( 
    816    name="${project}", 
    917    version=version, 
    10      
     18 
    1119    # uncomment the following lines if you fill them out in release.py 
    1220    #description=description, 
    1321    #author=author, 
     
    1523    #url=url, 
    1624    #download_url=download_url, 
    1725    #license=license, 
    18      
    19     install_requires = [ 
     26 
     27    install_requires=[ 
    2028        "TurboGears >= ${turbogearsversion}", 
    21 #if $identity == "sqlalchemy"  
    22         "SQLAlchemy",  
    23 #end if  
     29#if $identity == "sqlalchemy" 
     30        "SQLAlchemy", 
     31#end if 
    2432    ], 
    25     scripts = ["start-${package}.py"], 
     33    scripts=["start-${package}.py"], 
    2634    zip_safe=False, 
    27     packages=find_packages(), 
    28     package_data = find_package_data(where='${package}', 
    29                                      package='${package}'), 
    30     keywords = [ 
     35    packages=packages, 
     36    package_data=package_data, 
     37    keywords=[ 
    3138        # Use keywords if you'll be adding your package to the 
    3239        # Python Cheeseshop 
    33          
     40 
    3441        # if this has widgets, uncomment the next line 
    3542        # 'turbogears.widgets', 
    36          
     43 
    3744        # if this has a tg-admin command, uncomment the next line 
    3845        # 'turbogears.command', 
    39          
     46 
    4047        # if this has identity providers, uncomment the next line 
    4148        # 'turbogears.identity.provider', 
    42      
     49 
    4350        # If this is a template plugin, uncomment the next line 
    4451        # 'python.templating.engines', 
    45          
     52 
    4653        # If this is a full application, uncomment the next line 
    4754        # 'turbogears.app', 
    4855    ], 
    49     classifiers = [ 
     56    classifiers=[ 
    5057        'Development Status :: 3 - Alpha', 
    5158        'Operating System :: OS Independent', 
    5259        'Programming Language :: Python', 
     
    5562        # if this is an application that you'll distribute through 
    5663        # the Cheeseshop, uncomment the next line 
    5764        # 'Framework :: TurboGears :: Applications', 
    58          
     65 
    5966        # if this is a package that includes widgets that you'll distribute 
    6067        # through the Cheeseshop, uncomment the next line 
    6168        # 'Framework :: TurboGears :: Widgets', 
    6269    ], 
    63     test_suite = 'nose.collector', 
     70    test_suite='nose.collector', 
    6471    ) 
    65      
     72 
  • turbogears/qstemplates/quickstart/start-+package+.py_tmpl

     
    22import pkg_resources 
    33pkg_resources.require("TurboGears") 
    44 
    5 from turbogears import update_config, start_server 
     5from turbogears import config, update_config, start_server 
    66import cherrypy 
    77cherrypy.lowercase_api = True 
    88from os.path import * 
     
    1313# look for setup.py in this directory. If it's not there, this script is 
    1414# probably installed 
    1515if len(sys.argv) > 1: 
    16     update_config(configfile=sys.argv[1],  
     16    update_config(configfile=sys.argv[1], 
    1717        modulename="${package}.config") 
    1818elif exists(join(dirname(__file__), "setup.py")): 
    1919    update_config(configfile="dev.cfg",modulename="${package}.config") 
    2020else: 
    2121    update_config(configfile="prod.cfg",modulename="${package}.config") 
     22config.update(dict(package="${package}")) 
    2223 
    2324from ${package}.controllers import Root 
    2425 
  • turbogears/util.py

     
    9898 
    9999def get_package_name(): 
    100100    """Try to find out the package name of the current directory.""" 
     101    package = config.get("package") 
     102    if package: 
     103        return package 
    101104    if "--egg" in sys.argv: 
    102105        projectname = sys.argv[sys.argv.index("--egg")+1] 
    103106        egg = pkg_resources.get_distribution(projectname) 
    104         package = list(egg._get_metadata("top_level.txt"))[0] 
    105         return package 
    106     fname = get_project_meta('top_level.txt') 
    107     if fname: 
    108         return open(fname).readline()[:-1] 
     107        top_level = egg._get_metadata("top_level.txt") 
     108    else: 
     109        fname = get_project_meta('top_level.txt') 
     110        top_level = fname and open(fname) or [] 
     111    for package in top_level: 
     112        package = package.rstrip() 
     113        if package and package != 'locales': 
     114            return package 
    109115 
    110116def get_project_name(): 
    111117    pkg_info = get_project_meta('PKG-INFO') 
     
    426432           "arg_index", "inject_arg", "inject_args", "bind_args", 
    427433           "recursive_update", "combine_contexts", "request_available", 
    428434           "flatten_sequence", "load_class", "Bunch", 
    429            "parse_http_accept_header",  
     435           "parse_http_accept_header", 
    430436           "to_unicode", "to_utf8", "get_template_encoding_default", 
    431437           "find_precision", "copy_if_mutable"]