Package orm2 :: Package ui :: Package xist :: Module i18n
[hide private]
[frames] | no frames]

Source Code for Module orm2.ui.xist.i18n

  1  #!/usr/bin/env python 
  2  # -*- coding: iso-8859-1 -*- 
  3   
  4  ##  This file is part of orm, The Object Relational Membrane Version 2. 
  5  ## 
  6  ##  Copyright 2002-2006 by Diedrich Vorberg <diedrich@tux4web.de> 
  7  ## 
  8  ##  All Rights Reserved 
  9  ## 
 10  ##  For more Information on orm see the README file. 
 11  ## 
 12  ##  This program is free software; you can redistribute it and/or modify 
 13  ##  it under the terms of the GNU General Public License as published by 
 14  ##  the Free Software Foundation; either version 2 of the License, or 
 15  ##  (at your option) any later version. 
 16  ## 
 17  ##  This program is distributed in the hope that it will be useful, 
 18  ##  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  ##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  ##  GNU General Public License for more details. 
 21  ## 
 22  ##  You should have received a copy of the GNU General Public License 
 23  ##  along with this program; if not, write to the Free Software 
 24  ##  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 25  ## 
 26  ##  I have added a copy of the GPL in the file gpl.txt. 
 27   
 28  """ 
 29    
 30  """ 
 31   
 32  from types import * 
 33  from ll.xist import xsc 
 34   
35 -def translate(txt, context):
36 """ 37 Someday soon, this function will return a translation for unicode_string 38 suitable for context. 39 40 - If txt is a unicode string it will translate it and return a 41 xsc.Text instance to be used with xist 42 - If a xist DOM tree is supplied, it will recursively traverse it 43 and translate B{all} CharachterData instance's content that are not 44 marked with a __translated__ Flag. 45 46 """ 47 if type(txt) == UnicodeType: 48 # insert actual translation here!! 49 50 ret = xsc.Text(txt) 51 ret.__translated__ = True 52 return ret 53 54 elif type(txt) == StringType: 55 return translate(unicode(txt), context) 56 57 elif isinstance(txt, xsc.Text): 58 if getattr(txt, "__translated__", False): 59 return txt 60 else: 61 return translate(txt.content, context) 62 63 elif isinstance(txt, xsc.Frag): 64 for index, a in enumerate(txt): 65 txt[index] = translate(txt[index], context) 66 67 elif isinstance(txt, xsc.Element): 68 txt.content = translate(txt.content, context) 69 70 else: 71 tpl = ( repr(type(txt)), repr(txt), ) 72 raise TypeError("translate can only operate on unicode strings, "+ \ 73 "xsc.Element, or xsc.Frag, not %s %s" % tpl)
74
75 -def translate_template(context, template, *args, **kw):
76 """ 77 The i18n version of the % opperator: template must be a unicode string, 78 which will be translated (including the placeholders) and then applied to 79 the data passed as either positional arguments or key word arguments. 80 81 Example:: 82 83 >>> translate_template(context, '%s %s', one, two) 84 '<content of one> <content of two>' 85 >>> translate_template(context, '%(one)s %(two)s', one='1', two='2') 86 '1 2' 87 88 89 """ 90 if type(template) == UnicodeType: template = unicode(template) 91 92 if type(template) != UnicodeType: 93 raise TypeError("tramslate_template can only operate on unicode " + \ 94 "strings, not %s %s" % ( repr(type(template)), 95 repr(template), )) 96 char_data = translate(template) 97 98 if args: 99 char_data.content = char_data.content % args 100 elif kw: 101 char_data.content = char_data.content % kw 102 else: 103 raise ValueError("You must either supply positional or key words " + \ 104 "arguments for the % opperator to work on!") 105 106 return char_data
107 108 _ = translate 109 110 # Local variables: 111 # mode: python 112 # ispell-local-dictionary: "english" 113 # End: 114