~ubuntu-branches/debian/wheezy/idjc/wheezy

« back to all changes in this revision

Viewing changes to idjcpython/ln_text.py

  • Committer: Package Import Robot
  • Author(s): Alessio Treglia
  • Date: 2011-12-03 16:33:59 UTC
  • mfrom: (0.2.6)
  • Revision ID: package-import@ubuntu.com-20111203163359-dq5fy9i756jpoy29
Tags: 0.8.6-1
* New upstream release.
* debian/control:
  - Wrap and sort.
  - Build-depend on autopoint.
  - Drop autotools-dev, unnecessary.
* Drop the whole patch set, none of them is still needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#   ln_text.py: localisation support for IDJC
2
 
#   Copyright (C) 2010 Stephen Fairchild (s-fairchild@users.sourceforge.net)
3
 
#
4
 
#   This program is free software: you can redistribute it and/or modify
5
 
#   it under the terms of the GNU General Public License as published by
6
 
#   the Free Software Foundation, either version 2 of the License, or
7
 
#   (at your option) any later version.
8
 
#
9
 
#   This program is distributed in the hope that it will be useful,
10
 
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
#   GNU General Public License for more details.
13
 
#
14
 
#   You should have received a copy of the GNU General Public License
15
 
#   along with this program in the file entitled COPYING.
16
 
#   If not, see <http://www.gnu.org/licenses/>.
17
 
 
18
 
__all__ = ['ln']
19
 
 
20
 
import os, sys
21
 
 
22
 
def _get_modulename(cls):
23
 
   return cls.__name__ + "_text"
24
 
 
25
 
class xx_XX(object):
26
 
   def __getattr__(self, name):
27
 
      """Setting the LANG environment variable to xx_XX will result in all the item labels handled by this module being set by this method. This feature provides the language translator with a context for the item lables themselves."""
28
 
      if name[0] != "_":
29
 
         print "*** ln_text.py: warning, missing attribute '%s'" % name
30
 
         if self._warning == False:
31
 
            print "this and other missing attribute warnings will only appear once"
32
 
            self._warning = True
33
 
         setattr(self, name, "((%s))" % name)   # prevent duplicate warnings
34
 
         return getattr(self, name)
35
 
   def _load_text(self, cls):
36
 
      """The regular __import__ function will not dump multiple references from a module into a class necessitating the following code."""
37
 
      modulename = _get_modulename(cls)
38
 
      #print "loading attributes from %s for %s" % (modulename, cls.__name__)
39
 
      mod = __import__(modulename)
40
 
      for item in dir(mod):
41
 
         if item[0] != "_":
42
 
            setattr(cls, item, getattr(mod, item))
43
 
   def _detect_mismatched_references(self, cls):
44
 
      def no_underscore(s):
45
 
         return s[0] != "_"
46
 
      cls_vars = vars(cls)
47
 
      for reference in filter(no_underscore, dir(self)):
48
 
         if not reference in cls_vars:
49
 
            print "### ln_text.py: warning, attribute %s does not appear in the canonical list %s" % (reference, cls.__name__)
50
 
   def __str__(self):
51
 
      return "Language translation: " + self.__class__.__name__
52
 
   def __init__(self):
53
 
      self._warning = False
54
 
      for cls in self.__class__.mro():
55
 
         if cls is not xx_XX:
56
 
            if cls._load == True:
57
 
               self._load_text(cls)
58
 
            if cls.__base__ is xx_XX:
59
 
               self._detect_mismatched_references(cls)
60
 
         else:
61
 
            break
62
 
      if os.getenv("LANG") == "xx_XX":       # convert to a valid locale
63
 
         os.environ["LANG"] = "en_GB"
64
 
 
65
 
class en_GB(xx_XX):
66
 
   _load = True
67
 
 
68
 
class en_US(en_GB):
69
 
   _load = True
70
 
 
71
 
class de_DE(en_GB):
72
 
   _load = True
73
 
   
74
 
def _set():               # choose which translation to use
75
 
   lev = os.getenv("LANG")
76
 
   try:
77
 
      language = lev[0:2].lower()
78
 
      country  = lev[3:5].upper()
79
 
      if lev[2] != "_":
80
 
         raise IndexError
81
 
   except (IndexError,TypeError):
82
 
      print "Selecting language en_US. Change environment variable LANG to override."
83
 
      lev = "en_US"
84
 
   else:
85
 
      lev = "_".join((language, country))
86
 
   
87
 
   gns = sys.modules[__name__]      # the global name space
88
 
   # make sure lev matches one of the available languages
89
 
   if not lev in dir(gns):
90
 
      for each in dir(gns):
91
 
         if each[:2] == language:
92
 
            print "Compromise language match found", each
93
 
            lev = each
94
 
            break
95
 
      else:
96
 
         print "Could not find a suitable language match - will use en_US"
97
 
         lev = "en_US"
98
 
 
99
 
   # find the class whose name matches the lev variable and instantiate it
100
 
   return getattr(gns, lev)()
101
 
 
102
 
# set the ln (localisation) variable which is what the calling module should import 
103
 
ln = _set()
104
 
 
105
 
if __name__ == "__main__":
106
 
   def _cleanlist(module):
107
 
      return [ l for l in dir(module) if l[0] != "_" ]
108
 
 
109
 
   def sort(cls, basecls = en_GB):
110
 
      header = (
111
 
"""# -*- encoding: utf-8 -*-
112
 
#   Set your text editor to UTF-8 before modifying this file
113
 
 
114
 
#   %s: IDJC language localisation file for %s
115
 
%s
116
 
#
117
 
#   This program is free software: you can redistribute it and/or modify
118
 
#   it under the terms of the GNU General Public License as published by
119
 
#   the Free Software Foundation, either version 2 of the License, or
120
 
#   (at your option) any later version.
121
 
#
122
 
#   This program is distributed in the hope that it will be useful,
123
 
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
124
 
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
125
 
#   GNU General Public License for more details.
126
 
#
127
 
#   You should have received a copy of the GNU General Public License
128
 
#   along with this program in the file entitled COPYING.
129
 
#   If not, see <http://www.gnu.org/licenses/>\n\n""")
130
 
 
131
 
      try:
132
 
         trans = __import__(_get_modulename(cls))
133
 
      except:
134
 
         print "Import failed. Try creating the empty file %s and try again." % (_get_modulename(cls) + ".py")
135
 
         sys.exit(5)
136
 
      base  = __import__(_get_modulename(basecls))
137
 
      translist = _cleanlist(trans)
138
 
      
139
 
      def matching(item):
140
 
         return getattr(trans, item) == getattr(base, item)
141
 
      
142
 
      def write_items(file, module, attrlist, prepend = ""):
143
 
         for attr in attrlist:
144
 
            file.write("".join((prepend, attr, " = ")))
145
 
            data = getattr(module, attr)
146
 
            if type(data) == tuple:
147
 
               file.write(str(data))
148
 
            if type(data) == str:
149
 
               escdata = data.replace('"', '\"')
150
 
               if '\n' in data:
151
 
                  if prepend:
152
 
                     escdata = escdata.replace('\n', '\n#')
153
 
                  file.write(escdata.join(('"""', '"""')))
154
 
               else:
155
 
                  file.write(escdata.join(('"', '"')))
156
 
            file.write("\n\n")
157
 
 
158
 
      def stretch(text):
159
 
         return " ".join(text)
160
 
 
161
 
      baselist = _cleanlist(base)
162
 
      unmatched = [ r for r in translist if not r in baselist ]
163
 
      regular = []
164
 
      untranslated = []
165
 
      for baseitem in baselist:
166
 
         if trans != base and (not baseitem in translist or matching(baseitem)):
167
 
            untranslated.append(baseitem)
168
 
         else:
169
 
            regular.append(baseitem)
170
 
      if "translationcopyright" in translist:
171
 
         transcopyright = getattr(trans, "translationcopyright")
172
 
         if type(transcopyright) == str:
173
 
            transcopyright = (transcopyright, )
174
 
      else:
175
 
         transcopyright = ("Copyright (C) 2008 Stephen Fairchild (s-fairchild@users.sourceforge.net)", )
176
 
      copyrights = "#   " + "\n#   ".join(transcopyright)
177
 
      filename = _get_modulename(cls) + ".py"
178
 
      file = open(filename, "w")
179
 
      file.write(header % (filename, cls.__name__, copyrights))
180
 
      if unmatched:
181
 
         file.write(stretch("# Items with no match in %s\n\n" % (_get_modulename(basecls) + ".py")))
182
 
         write_items(file, trans, unmatched)
183
 
      if regular:
184
 
         if trans != base:
185
 
            file.write(stretch("# The following are regular translations\n\n"))
186
 
         write_items(file, trans, regular)
187
 
      if untranslated:
188
 
         file.write(stretch("# Untranslated items -- same as %s\n\n" % basecls.__name__))
189
 
         write_items(file, base, untranslated, "#")
190
 
      print "Sort of %s complete." % filename
191
 
      sys.exit(0)
192
 
   print "Use the sort() function to neatly arrange the translatable items.\n"
193
 
   print "Example usage:\n"
194
 
   print ">>> sort(fr_FR)"
195
 
   print "Untranslated items will be in British English for translation into French"
196
 
   print "The file fr_FR_text.py file needs to exist beforehand even if empty.\n"
197
 
   print ">>> sort(de_DE, fr_FR)"
198
 
   print "Untranslated items will be in French for translation into German."
199
 
   print "This assumes there is a French translation in the first place.\n"
200
 
   print "Choose one or two of:", [ s[1].__name__ for s in locals().items() if type(s[1]) == type and s[1] is not xx_XX and issubclass(s[1], xx_XX) ]
201
 
   print "To add a language/country code you'll need to edit ln_text.py"
202
 
   print "\nIt is quite safe to run this over files that have already been translated.\nCtrl+D quits."