Merge lp:~zyga/command-not-found/rework-locale-support into lp:~command-not-found-developers/command-not-found/trunk

Proposed by Zygmunt Krynicki
Status: Merged
Merged at revision: 140
Proposed branch: lp:~zyga/command-not-found/rework-locale-support
Merge into: lp:~command-not-found-developers/command-not-found/trunk
Diff against target: 126 lines (+36/-38)
3 files modified
CommandNotFound/CommandNotFound.py (+2/-1)
CommandNotFound/util.py (+3/-33)
command-not-found (+31/-4)
To merge this branch: bzr merge lp:~zyga/command-not-found/rework-locale-support
Reviewer Review Type Date Requested Status
Command Not Found Developers Pending
Review via email: mp+72848@code.launchpad.net

Description of the change

See the bzr commit message for details

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CommandNotFound/CommandNotFound.py'
2--- CommandNotFound/CommandNotFound.py 2011-08-10 13:03:51 +0000
3+++ CommandNotFound/CommandNotFound.py 2011-08-25 10:00:25 +0000
4@@ -2,6 +2,7 @@
5 # Licensed under GPL, see COPYING for the whole text
6
7 import gdbm
8+import gettext
9 import grp
10 import os
11 import os.path
12@@ -9,7 +10,7 @@
13 import string
14 import sys
15
16-from CommandNotFound.util import gettext_wrapper as _
17+_ = gettext.translation("command-not-found", fallback=True).ugettext
18
19
20 class BinaryDatabase(object):
21
22=== modified file 'CommandNotFound/util.py'
23--- CommandNotFound/util.py 2011-08-10 12:50:58 +0000
24+++ CommandNotFound/util.py 2011-08-25 10:00:25 +0000
25@@ -1,42 +1,12 @@
26 # (c) Zygmunt Krynicki 2008
27 # Licensed under GPL, see COPYING for the whole text
28
29+import gettext
30+import locale
31 import sys
32 import gettext
33
34-
35-def no_gettext_for_you(message):
36- """This function is used instead of gettext when there are some locale problems"""
37- return message
38-
39-
40-def gettext_not_crashy(s):
41- """ The getext handling is confusing:
42- - gettext.gettext caused LP: #161159
43- - getext.lgettext causes LP: #282446
44- Here we just try both :(
45- """
46- try:
47- return gettext.lgettext(s)
48- except UnicodeEncodeError:
49- return gettext.gettext(s)
50-
51-
52-def setup_locale():
53- import locale
54- try:
55- locale.getpreferredencoding()
56- gettext.bindtextdomain("command-not-found", "/usr/share/locale")
57- gettext.textdomain("command-not-found")
58- gettext.install("command-not-found", unicode=True)
59- return gettext_not_crashy
60- except locale.Error:
61- #print "Warning: python was unable to setup locale!"
62- #print "Internationalizatio features will not be enabled."
63- return no_gettext_for_you
64-
65-
66-_ = gettext_wrapper = setup_locale()
67+_ = gettext.translation("command-not-found", fallback=True).ugettext
68
69
70 def crash_guard(callback, bug_report_url, version):
71
72=== modified file 'command-not-found'
73--- command-not-found 2011-08-10 13:07:05 +0000
74+++ command-not-found 2011-08-25 10:00:25 +0000
75@@ -2,20 +2,47 @@
76 # (c) Zygmunt Krynicki 2005, 2006, 2007, 2008
77 # Licensed under GPL, see COPYING for the whole text
78
79+from __future__ import absolute_import
80+
81+
82 __version__ = "0.2.21"
83 BUG_REPORT_URL = "https://bugs.launchpad.net/command-not-found/+filebug"
84
85 try:
86 import sys
87- import CommandNotFound
88- from CommandNotFound.util import crash_guard, gettext_wrapper as _
89+ import gettext
90+ from optparse import OptionParser
91+
92+ from CommandNotFound.util import crash_guard
93 from CommandNotFound import CommandNotFound
94- from optparse import OptionParser
95 except KeyboardInterrupt:
96 sys.exit(127)
97
98+
99+def enable_i18n():
100+ cnf = gettext.translation("command-not-found", fallback=True)
101+ cnf.install(unicode=True)
102+
103+
104+class LocaleOptionParser(OptionParser):
105+ """
106+ OptionParser is broken as its implementation of _get_encoding() uses
107+ sys.getdefaultencoding() which is ascii, what it should be using is
108+ locale.getpreferredencoding() which returns value based on LC_CTYPE (most
109+ likely) and allows for UTF-8 encoding to be used.
110+ """
111+ def _get_encoding(self, file):
112+ encoding = getattr(file, "encoding", None)
113+ if not encoding:
114+ encoding = locale.getpreferredencoding()
115+ return encoding
116+
117+
118 def main():
119- parser = OptionParser(version = __version__, usage=_("%prog [options] <command-name>"))
120+ enable_i18n()
121+ parser = LocaleOptionParser(
122+ version=__version__,
123+ usage=_("%prog [options] <command-name>"))
124 parser.add_option('-d', '--data-dir', action='store',
125 default="/usr/share/command-not-found",
126 help=_("use this path to locate data fields"))

Subscribers

People subscribed via source and target branches