Merge remote-tracking branch 'build_test/vuplus-3.0-next' into vuplus-3.0
[vuplus_openvuplus_3.0] / meta-openvuplus / recipes-devtools / python / python / pgettext.patch
1 Index: Python-2.7.9/Lib/gettext.py
2 ===================================================================
3 --- Python-2.7.9.orig/Lib/gettext.py
4 +++ Python-2.7.9/Lib/gettext.py
5 @@ -53,6 +53,7 @@ from errno import ENOENT
6  __all__ = ['NullTranslations', 'GNUTranslations', 'Catalog',
7             'find', 'translation', 'install', 'textdomain', 'bindtextdomain',
8             'dgettext', 'dngettext', 'gettext', 'ngettext',
9 +           'pgettext', 'lpgettext', 'npgettext', 'lnpgettext', 'ldnpgettext',
10             ]
11  
12  _default_localedir = os.path.join(sys.prefix, 'share', 'locale')
13 @@ -193,11 +194,21 @@ class NullTranslations:
14              return self._fallback.gettext(message)
15          return message
16  
17 +    def pgettext(self, context, message):
18 +        if self._fallback:
19 +            return self._fallback.pgettext(context, message)
20 +        return message
21 +
22      def lgettext(self, message):
23          if self._fallback:
24              return self._fallback.lgettext(message)
25          return message
26  
27 +    def lpgettext(self, context, message):
28 +        if self._fallback:
29 +            return self._fallback.lpgettext(context, message)
30 +        return message
31 +
32      def ngettext(self, msgid1, msgid2, n):
33          if self._fallback:
34              return self._fallback.ngettext(msgid1, msgid2, n)
35 @@ -206,6 +217,14 @@ class NullTranslations:
36          else:
37              return msgid2
38  
39 +    def npgettext(self, context, msgid1, msgid2, n):
40 +        if self._fallback:
41 +            return self._fallback.npgettext(context, msgid1, msgid2, n)
42 +        if n == 1:
43 +            return msgid1
44 +        else:
45 +            return msgid2
46 +
47      def lngettext(self, msgid1, msgid2, n):
48          if self._fallback:
49              return self._fallback.lngettext(msgid1, msgid2, n)
50 @@ -227,6 +246,14 @@ class NullTranslations:
51          else:
52              return unicode(msgid2)
53  
54 +    def lnpgettext(self, context, msgid1, msgid2, n):
55 +        if self._fallback:
56 +            return self._fallback.lnpgettext(context, msgid1, msgid2, n)
57 +        if n == 1:
58 +            return msgid1
59 +        else:
60 +            return msgid2
61 +
62      def info(self):
63          return self._info
64  
65 @@ -252,13 +279,22 @@ class NullTranslations:
66                  __builtin__.__dict__['lgettext'] = self.lgettext
67              if "lngettext" in names:
68                  __builtin__.__dict__['lngettext'] = self.lngettext
69 -
70 +            if "pgettext" in names:
71 +                __builtin__.__dict__['pgettext'] = self.pgettext
72 +            if "npgettext" in names:
73 +                __builtin__.__dict__['npgettext'] = self.npgettext
74 +            if "lnpgettext" in names:
75 +                __builtin__.__dict__['lnpgettext'] = self.lnpgettext
76  
77  class GNUTranslations(NullTranslations):
78      # Magic number of .mo files
79      LE_MAGIC = 0x950412deL
80      BE_MAGIC = 0xde120495L
81  
82 +    # The encoding of a msgctxt and a msgid in a .mo file is
83 +    # msgctxt + "\x04" + msgid (gettext version >= 0.15)
84 +    CONTEXT = "%s\x04%s"
85 +
86      def _parse(self, fp):
87          """Override this method to support alternative .mo formats."""
88          unpack = struct.unpack
89 @@ -354,6 +390,21 @@ class GNUTranslations(NullTranslations):
90              return tmsg.encode(self._charset)
91          return tmsg
92  
93 +    def pgettext(self, context, message):
94 +        ctxt_msg_id = self.CONTEXT % (context, message)
95 +        missing = object()
96 +        tmsg = self._catalog.get(ctxt_msg_id, missing)
97 +        if tmsg is missing:
98 +            if self._fallback:
99 +                return self._fallback.pgettext(context, message)
100 +            return message
101 +        # Encode the Unicode tmsg back to an 8-bit string, if possible
102 +        if self._output_charset:
103 +            return tmsg.encode(self._output_charset)
104 +        elif self._charset:
105 +            return tmsg.encode(self._charset)
106 +        return tmsg
107 +
108      def lgettext(self, message):
109          missing = object()
110          tmsg = self._catalog.get(message, missing)
111 @@ -365,6 +416,18 @@ class GNUTranslations(NullTranslations):
112              return tmsg.encode(self._output_charset)
113          return tmsg.encode(locale.getpreferredencoding())
114  
115 +    def lpgettext(self, context, message):
116 +        ctxt_msg_id = self.CONTEXT % (context, message)
117 +        missing = object()
118 +        tmsg = self._catalog.get(ctxt_msg_id, missing)
119 +        if tmsg is missing:
120 +            if self._fallback:
121 +                return self._fallback.lpgettext(context, message)
122 +            return message
123 +        if self._output_charset:
124 +            return tmsg.encode(self._output_charset)
125 +        return tmsg.encode(locale.getpreferredencoding())
126 +
127      def ngettext(self, msgid1, msgid2, n):
128          try:
129              tmsg = self._catalog[(msgid1, self.plural(n))]
130 @@ -381,6 +444,23 @@ class GNUTranslations(NullTranslations):
131              else:
132                  return msgid2
133  
134 +    def npgettext(self, context, msgid1, msgid2, n):
135 +        ctxt_msg_id = self.CONTEXT % (context, msgid1)
136 +        try:
137 +            tmsg = self._catalog[(ctxt_msg_id, self.plural(n))]
138 +            if self._output_charset:
139 +                return tmsg.encode(self._output_charset)
140 +            elif self._charset:
141 +                return tmsg.encode(self._charset)
142 +            return tmsg
143 +        except KeyError:
144 +            if self._fallback:
145 +                return self._fallback.npgettext(context, msgid1, msgid2, n)
146 +            if n == 1:
147 +                return msgid1
148 +            else:
149 +                return msgid2
150 +
151      def lngettext(self, msgid1, msgid2, n):
152          try:
153              tmsg = self._catalog[(msgid1, self.plural(n))]
154 @@ -416,6 +496,21 @@ class GNUTranslations(NullTranslations):
155                  tmsg = unicode(msgid2)
156          return tmsg
157  
158 +    def lnpgettext(self, context, msgid1, msgid2, n):
159 +        ctxt_msg_id = self.CONTEXT % (context, msgid1)
160 +        try:
161 +            tmsg = self._catalog[(ctxt_msg_id, self.plural(n))]
162 +            if self._output_charset:
163 +                return tmsg.encode(self._output_charset)
164 +            return tmsg.encode(locale.getpreferredencoding())
165 +        except KeyError:
166 +            if self._fallback:
167 +                return self._fallback.lnpgettext(context, msgid1, msgid2, n)
168 +            if n == 1:
169 +                return msgid1
170 +            else:
171 +                return msgid2
172 +
173  
174  # Locate a .mo file using the gettext strategy
175  def find(domain, localedir=None, languages=None, all=0):
176 @@ -532,6 +627,14 @@ def dgettext(domain, message):
177          return message
178      return t.gettext(message)
179  
180 +def dpgettext(domain, context, message):
181 +    try:
182 +        t = translation(domain, _localedirs.get(domain, None),
183 +                        codeset=_localecodesets.get(domain))
184 +    except IOError:
185 +        return message
186 +    return t.pgettext(context, message)
187 +
188  def ldgettext(domain, message):
189      try:
190          t = translation(domain, _localedirs.get(domain, None),
191 @@ -540,6 +643,14 @@ def ldgettext(domain, message):
192          return message
193      return t.lgettext(message)
194  
195 +def ldpgettext(domain, context, message):
196 +    try:
197 +        t = translation(domain, _localedirs.get(domain, None),
198 +                        codeset=_localecodesets.get(domain))
199 +    except IOError:
200 +        return message
201 +    return t.lpgettext(context, message)
202 +
203  def dngettext(domain, msgid1, msgid2, n):
204      try:
205          t = translation(domain, _localedirs.get(domain, None),
206 @@ -551,6 +662,17 @@ def dngettext(domain, msgid1, msgid2, n)
207              return msgid2
208      return t.ngettext(msgid1, msgid2, n)
209  
210 +def dnpgettext(domain, context, msgid1, msgid2, n):
211 +    try:
212 +        t = translation(domain, _localedirs.get(domain, None),
213 +                        codeset=_localecodesets.get(domain))
214 +    except IOError:
215 +        if n == 1:
216 +            return msgid1
217 +        else:
218 +            return msgid2
219 +    return t.npgettext(context, msgid1, msgid2, n)
220 +
221  def ldngettext(domain, msgid1, msgid2, n):
222      try:
223          t = translation(domain, _localedirs.get(domain, None),
224 @@ -562,18 +684,41 @@ def ldngettext(domain, msgid1, msgid2, n
225              return msgid2
226      return t.lngettext(msgid1, msgid2, n)
227  
228 +def ldnpgettext(domain, context, msgid1, msgid2, n):
229 +    try:
230 +        t = translation(domain, _localedirs.get(domain, None),
231 +                        codeset=_localecodesets.get(domain))
232 +    except IOError:
233 +        if n == 1:
234 +            return msgid1
235 +        else:
236 +            return msgid2
237 +    return t.lnpgettext(context, msgid1, msgid2, n)
238 +
239  def gettext(message):
240      return dgettext(_current_domain, message)
241  
242 +def pgettext(context, message):
243 +    return dpgettext(_current_domain, context, message)
244 +
245  def lgettext(message):
246      return ldgettext(_current_domain, message)
247  
248 +def lpgettext(context, message):
249 +    return ldpgettext(_current_domain, context, message)
250 +
251  def ngettext(msgid1, msgid2, n):
252      return dngettext(_current_domain, msgid1, msgid2, n)
253  
254 +def npgettext(context, msgid1, msgid2, n):
255 +    return dnpgettext(_current_domain, context, msgid1, msgid2, n)
256 +
257  def lngettext(msgid1, msgid2, n):
258      return ldngettext(_current_domain, msgid1, msgid2, n)
259  
260 +def lnpgettext(context, msgid1, msgid2, n):
261 +    return ldnpgettext(_current_domain, context, msgid1, msgid2, n)
262 +
263  # dcgettext() has been deemed unnecessary and is not implemented.
264  
265  # James Henstridge's Catalog constructor from GNOME gettext.  Documented usage
266 Index: Python-2.7.9/Tools/i18n/msgfmt.py
267 ===================================================================
268 --- Python-2.7.9.orig/Tools/i18n/msgfmt.py
269 +++ Python-2.7.9/Tools/i18n/msgfmt.py
270 @@ -6,7 +6,8 @@
271  
272  This program converts a textual Uniforum-style message catalog (.po file) into
273  a binary GNU catalog (.mo file).  This is essentially the same function as the
274 -GNU msgfmt program, however, it is a simpler implementation.
275 +GNU msgfmt program, however, it is a simpler implementation.  Currently it
276 +does not handle plural forms but it does handle message contexts.
277  
278  Usage: msgfmt.py [OPTIONS] filename.po
279  
280 @@ -32,7 +33,7 @@ import getopt
281  import struct
282  import array
283  
284 -__version__ = "1.1"
285 +__version__ = "1.2"
286  
287  MESSAGES = {}
288  
289 @@ -46,11 +47,14 @@ def usage(code, msg=''):
290  
291  
292  \f
293 -def add(id, str, fuzzy):
294 +def add(ctxt, id, str, fuzzy):
295      "Add a non-fuzzy translation to the dictionary."
296      global MESSAGES
297      if not fuzzy and str:
298 -        MESSAGES[id] = str
299 +        if ctxt is None:
300 +            MESSAGES[id] = str
301 +        else:
302 +            MESSAGES["%s\x04%s" % (ctxt, id)] = str
303  
304  
305  \f
306 @@ -100,6 +104,7 @@ def generate():
307  def make(filename, outfile):
308      ID = 1
309      STR = 2
310 +    CTXT = 3
311  
312      # Compute .mo name from .po name and arguments
313      if filename.endswith('.po'):
314 @@ -115,7 +120,7 @@ def make(filename, outfile):
315          print >> sys.stderr, msg
316          sys.exit(1)
317  
318 -    section = None
319 +    section = msgctxt = None
320      fuzzy = 0
321  
322      # Parse the catalog
323 @@ -124,8 +129,8 @@ def make(filename, outfile):
324          lno += 1
325          # If we get a comment line after a msgstr, this is a new entry
326          if l[0] == '#' and section == STR:
327 -            add(msgid, msgstr, fuzzy)
328 -            section = None
329 +            add(msgctxt, msgid, msgstr, fuzzy)
330 +            section = msgctxt = None
331              fuzzy = 0
332          # Record a fuzzy mark
333          if l[:2] == '#,' and 'fuzzy' in l:
334 @@ -133,10 +138,16 @@ def make(filename, outfile):
335          # Skip comments
336          if l[0] == '#':
337              continue
338 -        # Now we are in a msgid section, output previous section
339 -        if l.startswith('msgid') and not l.startswith('msgid_plural'):
340 +        # Now we are in a msgid or msgctxt section, output previous section
341 +        if l.startswith("msgctxt"):
342 +            if section == STR:
343 +                add(msgctxt, msgid, msgstr, fuzzy)
344 +            section = CTXT
345 +            l = l[7:]
346 +            msgctxt = ''
347 +        elif l.startswith('msgid') and not l.startswith('msgid_plural'):
348              if section == STR:
349 -                add(msgid, msgstr, fuzzy)
350 +                add(msgctxt, msgid, msgstr, fuzzy)
351              section = ID
352              l = l[5:]
353              msgid = msgstr = ''
354 @@ -174,6 +185,8 @@ def make(filename, outfile):
355          l = ast.literal_eval(l)
356          if section == ID:
357              msgid += l
358 +        elif section == CTXT:
359 +            msgctxt += l
360          elif section == STR:
361              msgstr += l
362          else:
363 @@ -183,7 +196,7 @@ def make(filename, outfile):
364              sys.exit(1)
365      # Add last entry
366      if section == STR:
367 -        add(msgid, msgstr, fuzzy)
368 +        add(msgctxt, msgid, msgstr, fuzzy)
369  
370      # Compute output
371      output = generate()