source: trunk/web/addons/job_monarch/lib/extjs/examples/locale/create_languages_js.py @ 619

Last change on this file since 619 was 619, checked in by ramonb, 15 years ago

lib/:

  • added new AJAX dependancies: ExtJS, pChart, Lightbox2
File size: 1.9 KB
Line 
1#!/usr/bin/env python
2
3import sys, os, re
4
5try:
6    import chardet
7except ImportError:
8    print "You need universal encoding detector for this script"
9    print " http://chardet.feedparser.org or apt-get install python-chardet"
10    sys.exit()
11
12regexp_language = re.compile("\* +(.+) +translation", re.IGNORECASE)
13js_template = """/* This file is automaticly generated by create_language_js.py */
14
15// some data used in the examples
16Ext.namespace('Ext.exampledata');
17
18// TODO: complete and sort the list
19Ext.exampledata.languages = [
20%s
21];
22"""
23
24
25lang_dubs = {}
26def lang_name(file):
27    language = os.path.basename(file)
28    m = regexp_language.search(open(file).read(512))
29    if m:
30        language = m.groups()[0]
31        if not lang_dubs.has_key(language):
32            lang_dubs[language] = file
33        else:
34            raise Exception('Duplicate language '+language+' for file '+file)
35    return language
36
37def print_locale(lang_code):
38    print lang_code,
39    sys.stdout.flush()
40    return True
41
42def main():
43    base_dir = "../../src/locale"
44    base_file = lambda f: os.path.join(base_dir, f)
45    try:
46        locales = os.listdir(base_dir)
47    except IOError:
48        print "Cannot find source locale directory: %s ... exiting" % base_dir
49        sys.exit()
50   
51    valid_file = lambda e: e.endswith(".js") and e.startswith("ext-lang-")
52    char_set = lambda f: chardet.detect(open(f).read())['encoding']
53    lang_code = lambda f: f[9:f.rfind(".js")]
54    info_set = lambda f: (lang_name(base_file(f)), (lang_code(f), char_set(base_file(f))))
55    locales = dict(info_set(file) for file in locales if valid_file(file) and print_locale(lang_code(file)))
56    print "... done"
57    locale_strarray = ',\n'.join(["\t[%r, %r, %r]" % (code, name, charset) \
58                                     for name, (code, charset) in sorted(locales.items())])
59    # create languages.js
60    open("languages.js", "w").write(js_template % locale_strarray)
61    print "saved %d languages to languages.js" % len(locales)
62       
63if __name__=="__main__":
64    main()
Note: See TracBrowser for help on using the repository browser.