1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
# Copyright 2009-2011 Canonical Ltd. This software is licensed under
# the GNU Affero General Public License version 3 (see the file
# LICENSE).
# pylint: disable-msg=E0611,W0212
__metaclass__ = type
__all__ = [
'Language',
'LanguageSet',
]
from sqlobject import (
BoolCol,
IntCol,
SQLObjectNotFound,
SQLRelatedJoin,
StringCol,
)
from storm.expr import (
And,
Count,
Desc,
Join,
LeftJoin,
Or,
)
from zope.interface import implements
from canonical.database.enumcol import EnumCol
from canonical.database.sqlbase import SQLBase
from canonical.launchpad.helpers import ensure_unicode
from lp.services.database.lpstorm import (
ISlaveStore,
IStore,
)
from lp.app.errors import NotFoundError
from lp.registry.model.karma import (
KarmaCache,
KarmaCategory,
)
from lp.services.database.decoratedresultset import DecoratedResultSet
from lp.services.propertycache import (
cachedproperty,
get_property_cache,
)
from lp.services.worlddata.interfaces.language import (
ILanguage,
ILanguageSet,
TextDirection,
)
# XXX: JonathanLange 2010-11-10 bug=673796: It turns out this module is
# unusable without spokenin being imported first. So, import spokenin.
from lp.services.worlddata.model.spokenin import SpokenIn
SpokenIn
class Language(SQLBase):
implements(ILanguage)
_table = 'Language'
code = StringCol(
dbName='code', notNull=True, unique=True, alternateID=True)
uuid = StringCol(dbName='uuid', notNull=False, default=None)
nativename = StringCol(dbName='nativename')
englishname = StringCol(dbName='englishname')
pluralforms = IntCol(dbName='pluralforms')
pluralexpression = StringCol(dbName='pluralexpression')
visible = BoolCol(dbName='visible', notNull=True)
direction = EnumCol(
dbName='direction', notNull=True, schema=TextDirection,
default=TextDirection.LTR)
translation_teams = SQLRelatedJoin(
'Person', joinColumn="language",
intermediateTable='Translator', otherColumn='translator')
_countries = SQLRelatedJoin(
'Country', joinColumn='language', otherColumn='country',
intermediateTable='SpokenIn')
# Define a read/write property `countries` so it can be passed
# to language administration `LaunchpadFormView`.
def _getCountries(self):
return self._countries
def _setCountries(self, countries):
for country in self._countries:
if country not in countries:
self.removeCountry(country)
for country in countries:
if country not in self._countries:
self.addCountry(country)
countries = property(_getCountries, _setCountries)
@property
def displayname(self):
"""See `ILanguage`."""
return '%s (%s)' % (self.englishname, self.code)
def __repr__(self):
return "<%s '%s' (%s)>" % (
self.__class__.__name__, self.englishname, self.code)
@property
def guessed_pluralforms(self):
"""See `ILanguage`."""
forms = self.pluralforms
if forms is None:
# Just take a plausible guess. The caller needs a number.
return 2
else:
return forms
@property
def alt_suggestion_language(self):
"""See `ILanguage`.
Non-visible languages and English are not translatable, so they
are excluded. Brazilian Portuguese has diverged from Portuguese
to such a degree that it should be treated as a parent language.
Norwegian languages Nynorsk (nn) and Bokmaal (nb) are similar
and may provide suggestions for each other.
"""
if self.code == 'pt_BR':
return None
elif self.code == 'nn':
return Language.byCode('nb')
elif self.code == 'nb':
return Language.byCode('nn')
codes = self.code.split('_')
if len(codes) == 2 and codes[0] != 'en':
language = Language.byCode(codes[0])
if language.visible == True:
return language
else:
return None
return None
@property
def dashedcode(self):
"""See `ILanguage`."""
return self.code.replace('_', '-')
@property
def abbreviated_text_dir(self):
"""See `ILanguage`."""
if self.direction == TextDirection.LTR:
return 'ltr'
elif self.direction == TextDirection.RTL:
return 'rtl'
else:
assert False, "unknown text direction"
@property
def translators(self):
"""See `ILanguage`."""
from lp.registry.model.person import (
Person,
PersonLanguage,
)
return IStore(Language).using(
Join(
Person,
LanguageSet._getTranslatorJoins(),
Person.id == PersonLanguage.personID),
).find(
Person,
PersonLanguage.language == self,
).order_by(Desc(KarmaCache.karmavalue))
@cachedproperty
def translators_count(self):
"""See `ILanguage`."""
return self.translators.count()
class LanguageSet:
implements(ILanguageSet)
@staticmethod
def _getTranslatorJoins():
# XXX CarlosPerelloMarin 2007-03-31 bug=102257:
# The KarmaCache table doesn't have a field to store karma per
# language, so we are actually returning the people with the most
# translation karma that have this language selected in their
# preferences.
from lp.registry.model.person import PersonLanguage
return Join(
PersonLanguage,
Join(
KarmaCache,
KarmaCategory,
And(
KarmaCategory.name == 'translations',
KarmaCache.categoryID == KarmaCategory.id,
KarmaCache.productID == None,
KarmaCache.projectID == None,
KarmaCache.sourcepackagenameID == None,
KarmaCache.distributionID == None)),
PersonLanguage.personID ==
KarmaCache.personID)
@property
def _visible_languages(self):
return Language.select(
'visible IS TRUE',
orderBy='englishname')
@property
def common_languages(self):
"""See `ILanguageSet`."""
return iter(self._visible_languages)
def getDefaultLanguages(self, want_translators_count=False):
"""See `ILanguageSet`."""
return self.getAllLanguages(
want_translators_count=want_translators_count,
only_visible=True)
def getAllLanguages(self, want_translators_count=False,
only_visible=False):
"""See `ILanguageSet`."""
result = IStore(Language).find(
Language,
Language.visible == True if only_visible else True,
).order_by(
Language.englishname)
if want_translators_count:
def preload_translators_count(languages):
from lp.registry.model.person import PersonLanguage
ids = set(language.id for language in languages).difference(
set([None]))
counts = IStore(Language).using(
LeftJoin(
Language,
self._getTranslatorJoins(),
PersonLanguage.languageID == Language.id),
).find(
(Language, Count(PersonLanguage)),
Language.id.is_in(ids),
).group_by(Language)
for language, count in counts:
get_property_cache(language).translators_count = count
return DecoratedResultSet(
result, pre_iter_hook=preload_translators_count)
return result
def __iter__(self):
"""See `ILanguageSet`."""
return iter(Language.select(orderBy='englishname'))
def __getitem__(self, code):
"""See `ILanguageSet`."""
language = self.getLanguageByCode(code)
if language is None:
raise NotFoundError(code)
return language
def get(self, language_id):
"""See `ILanguageSet`."""
try:
return Language.get(language_id)
except SQLObjectNotFound:
return None
def getLanguageByCode(self, code):
"""See `ILanguageSet`."""
assert isinstance(code, basestring), (
"%s is not a valid type for 'code'" % type(code))
try:
return Language.byCode(code)
except SQLObjectNotFound:
return None
def keys(self):
"""See `ILanguageSet`."""
return [language.code for language in Language.select()]
def canonicalise_language_code(self, code):
"""See `ILanguageSet`."""
if '-' in code:
language, country = code.split('-', 1)
return "%s_%s" % (language, country.upper())
else:
return code
def codes_to_languages(self, codes):
"""See `ILanguageSet`."""
languages = []
for code in [self.canonicalise_language_code(code) for code in codes]:
try:
languages.append(self[code])
except KeyError:
pass
return languages
def createLanguage(self, code, englishname, nativename=None,
pluralforms=None, pluralexpression=None, visible=True,
direction=TextDirection.LTR):
"""See `ILanguageSet`."""
return Language(
code=code, englishname=englishname, nativename=nativename,
pluralforms=pluralforms, pluralexpression=pluralexpression,
visible=visible, direction=direction)
def search(self, text):
"""See `ILanguageSet`."""
if text:
text = ensure_unicode(text).lower()
results = ISlaveStore(Language).find(
Language, Or(
Language.code.lower().contains_string(text),
Language.englishname.lower().contains_string(
text))).order_by(Language.englishname)
else:
results = None
return results
|