1 import os
2 import codecs
3 from cherrypy import request
4 from turbogears.i18n import get_locale
5 from turbogears import startup, config
6 from turbogears.util import parse_http_accept_header
7 from turbogears.i18n.utils import lang_in_gettext_format
8 from turbogears.widgets.base import JSLink, CoreWD, RenderOnlyWD, static
9
10 __all__ = ["LocalizableJSLink", "CalendarLangFileLink"]
11
12
14 """Provides a simple way to include language-specific data in your JS code.
15
16 Language file to use is determined from the user's locale or from the 'language'
17 parameter. If there is no language file for the language (determined via
18 'supported_languages' parameter) than 'default_language' is used.
19
20 """
21 default_language = 'en'
22 supported_languages = ['en']
23 params = ['default_language', 'language', 'supported_languages']
24 params_doc = {
25 'language' : 'language code to use ' \
26 '(overrides user locale setting which is the default)',
27 'default_language' : 'language code to use ' \
28 'if specified language is not supported',
29 'supported_languages' : 'list of supported language codes ' \
30 ' (which means corresponding language files exist)',
31 }
32
39
40
44
45
46 _norm_tag = ''.join([(c.islower() or c.isdigit() or c == '-')
47 and c or c.isupper() and c.lower() or ' '
48 for c in map(chr, xrange(256))])
49
51 """Normalize string to alphanumeric ascii chars, hyphens and underscores.
52
53 The length is limited to 16 characters and other characters are
54 collapsed to single underscore, or ignored at the start or end.
55
56 """
57 if tag is not None:
58 return '_'.join(str(tag)[:16].translate(_norm_tag).split())
59
60
62 """Normalize the name of a character set."""
63 if charset is not None:
64 charset = norm_tag(charset)
65 try:
66 charset = norm_tag(codecs.lookup(charset).name)
67 except (LookupError, AttributeError):
68
69 pass
70 return charset
71
72
74 """Links to proper calendar.js language file depending on HTTP info."""
75
76 default_charset = 'utf-8'
77
79 languages = parse_http_accept_header(
80 request.headers.get("Accept-Language")) or []
81 language = d.get('language')
82 if language and language != '*':
83 languages.insert(0, language)
84 languages = map(norm_tag, languages)
85 default_language = norm_tag(self.default_language)
86 languages.append(default_language)
87 charsets = parse_http_accept_header(
88 request.headers.get("Accept-Charset")) or []
89 charsets = map(norm_charset, charsets)
90 default_charset = norm_charset(self.default_charset)
91 charset = norm_charset(config.get("kid.encoding"))
92 if charset and charset != default_charset:
93 charsets.append(charset)
94 charsets.append(default_charset)
95 path = "/tg_widgets/%s" % self.mod
96 base_dir = config.get("static_filter.dir", path=path)
97 base_name = "calendar/lang/calendar-"
98 def find_link():
99 params = dict()
100 for lang in languages:
101 if not lang or lang == '*':
102 lang = default_language
103 base_lang = base_name + lang.replace('-', '_')
104 for charset in charsets:
105 if not charset or charset == '*':
106 charset = default_charset
107 file_name = base_lang
108 if charset != default_charset:
109 file_name += '-' + charset.replace('-', '_')
110 file_name += '.js'
111 if os.path.exists(os.path.join(base_dir, file_name)):
112 path = "/%stg_widgets/%s" % (startup.webpath, self.mod)
113 link = "%s/%s" % (path, file_name)
114 return (link, language, charset)
115 if charset == default_charset:
116 break
117 d["link"], d['language'], d['charset'] = find_link()
118