diff options
author | eric <eric> | 2004-06-14 18:31:31 +0000 |
---|---|---|
committer | eric <eric> | 2004-06-14 18:31:31 +0000 |
commit | 5268aa5802366910b7900e83c9de7ffff2ff96d6 (patch) | |
tree | 4a28991fc8fe76ddab6b2b023b9f8c4990775f64 | |
parent | 64520381cd4fb7c3a0684549292132911f9cf6f4 (diff) | |
download | aurweb-5268aa5802366910b7900e83c9de7ffff2ff96d6.tar.xz |
Started adding i18n support
-rw-r--r-- | web/html/.htaccess | 1 | ||||
-rw-r--r-- | web/html/index.php | 3 | ||||
-rw-r--r-- | web/lib/translator.inc | 55 | ||||
-rwxr-xr-x | web/utils/genpopo | 135 |
4 files changed, 194 insertions, 0 deletions
diff --git a/web/html/.htaccess b/web/html/.htaccess new file mode 100644 index 0000000..1bf1488 --- /dev/null +++ b/web/html/.htaccess @@ -0,0 +1 @@ +php_value include_path ".:../lib:../lang" diff --git a/web/html/index.php b/web/html/index.php new file mode 100644 index 0000000..ec2808a --- /dev/null +++ b/web/html/index.php @@ -0,0 +1,3 @@ +<? +print "Hello, world!<br/>\n"; +?> diff --git a/web/lib/translator.inc b/web/lib/translator.inc new file mode 100644 index 0000000..2d45f2a --- /dev/null +++ b/web/lib/translator.inc @@ -0,0 +1,55 @@ +<? +# this include file provides support for i18n +# + +# usage: +# use the _() function for returning translated strings of +# text. The string can contain escape codes %h for HTML +# and %s for regular text. +# +# supporting scripts: +# there is a supporting script, web/utils/genpopo, that will +# parse the PHP files and create PHP include files that contain +# a mapping for each translated language. The include files +# have the form, +# +# $_t["en"]["My cat is large."] = "My cat is large."; +# $_t["es"]["My cat is large."] = "Mi gato esta grande."; +# +# examples: +# print _("%s has %s apples.", array("Bill", "5")); +# print _("This is a %h%s%h problem!", array("<b>","major","</b>")); + +include_once("common_po.inc"); + +function _($tag, $args=array()) { + global $_t; + global $LANG; + + # default to English if the lang hasn't been provided + # + if (!$LANG) { + $lang = "en"; + } else { + $lang = $LANG; + } + + # create the translation, if it doesn't exist, highlight it + # + $translated = $_t[$lang][$tag]; + if (!$translated) { + $translated = "<blink><b>_" . $tag . "_</b></blink>"; + } + + # replace escape substitutions + # + if (!empty($args)) { + while (list($k, $v) = each($args)) { + $translated = preg_replace("/\%[sh]/", $v, $translated, 1); + } + } + return $translated; +} + +# vim: ts=2 sw=2 noet ft=php +?> diff --git a/web/utils/genpopo b/web/utils/genpopo new file mode 100755 index 0000000..97cb154 --- /dev/null +++ b/web/utils/genpopo @@ -0,0 +1,135 @@ +#! /usr/bin/python -O +# -*- coding: iso-8859-1 -*- + +# this script iterates through the 'html' and 'lib' directories +# looking for php scripts that contain a include_once("xxx_po.inc") +# line and _() functions. It creates/appends to the corresponding +# "xxx_po.inc" file in the 'lang' subdirectory and places the +# i18n strings into the file in the proper format. +# + +import sys +print_dupes = '-v' in sys.argv + +import re, os +up = re.compile('_\(\s*"(([^"]|(?<=\\\\)["])+)"') + +lang = { 'common_po.po': {} } + +current_dir = os.getcwd() + +# Find the common_po.po file. +# +common = {} +for dir in ['../lang', 'lang']: + if os.path.exists(dir): + os.chdir(dir) + if os.path.exists('common_po.list'): + f = open('common_po.list','r') + lines = f.readlines() + f.close() + for line in lines: + common[line[:-1]] = 0 + lang['common_po.po'][line[:-1]] = 1 + os.chdir(current_dir) + break + os.chdir(current_dir) +else: + print "Can't find common_po.list file." + raise SystemExit + +# Find the lang directory. +# +for dir in ['../lang', 'lang']: + if os.path.exists(dir): + lang_dir = dir + break +else: + print "Can't find the lang directory." + raise SystemExit + +# Iterate through various places where the php files might be. +# +for dir in ['../html', '../lib', 'html', 'lib']: + + if os.path.exists(dir): + # Find all the PHP files in the current directory. + # + files = [x for x in os.listdir(dir) + if (x[-4:] == '.inc' and x[-7:] != '_po.inc') + or x[-6:] == '.class' + or x[-4:] == '.php' + or x[-6:] == '.phtml' + ] + os.chdir(dir) + + for file in files: + f = open(file,'r') + lines = f.readlines() + f.close() + + # Is this file one we need to parse for internationalized strings? + # + parse_file = 0 + for line in lines: + match = re.search("include(_once|)\s*\(\s*[\"']([A-Za-z_]+_po.inc)[\"']\s*\);",line) + if match and match.group(2) != "common_po.inc": + po = match.group(2).replace(".inc",".po") + if not lang.has_key(po): + lang[po] = {} + parse_file = 1 + break + + # If we need to parse the file, do so. + # + if parse_file: + print "Parsing %s..." % file + for line in lines: + match = up.search(line) + while match: + term = match.group(1).replace('\\"','"') + if common.has_key(term): + common[term] += 1 + else: + if print_dupes: + for key in lang.keys(): + if key != po and lang[key].has_key(term): + print "...Duplicate term: \"%s\" is also in %s." % (term,key) + lang[po][term] = 1 + line = line[match.end(1):] + match = up.search(line) + + os.chdir(current_dir) + +# TODO Now generate all the .inc files if they don't already exist. +# if they do exist, only append new stuff to the end. If the 'force' +# option is passed, just overwrite the entire thing. +# +os.chdir(lang_dir) +for po in lang.keys(): + print "Generating %s..." % po + f = open(po,'w') + f.write("""# INSTRUCTIONS TO TRANSLATORS: +# blah blah blah.... +""") + + for term in lang[po].keys(): + f.write("\n"); + f.write('_$t["en"]["%s"] = "%s";\n' % (term, term)) + f.write('# _$t["es"]["%s"] = "--> Spanish translation here. <--";\n' % term) + f.write('# _$t["fr"]["%s"] = "--> French translation here. <--";\n' % term) + f.write('# _$t["de"]["%s"] = "--> German translation here. <--";\n' % term) + + f.write("\n"); + f.close() + +# Print out warnings for unused and little-used common entries. +# +for key in common.keys(): + if common[key] == 1: + print "Warning: common entry '%s' is only used once." % key +for key in common.keys(): + if common[key] == 0: + print "Warning: unused common entry '%s'." % key + +# vim: ts=2 sw=2 noet ft=python |