-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
409 lines (337 loc) · 15.8 KB
/
plugin.py
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# encoding: utf-8
import six
from six import string_types
import ckan
from ckan.plugins import SingletonPlugin, implements, IPackageController
from ckan.plugins import IGroupController, IOrganizationController, ITagController, IResourceController
from ckan.common import request, config, c
from ckan.logic import get_action
try:
long # Python 2
except NameError:
long = int # Python 3
def translate_data_dict(data_dict):
'''Return the given dict (e.g. a dataset dict) with as many of its fields
as possible translated into the desired or the fallback language.
'''
desired_lang_code = request.environ['CKAN_LANG']
fallback_lang_code = config.get('ckan.locale_default', 'en')
# Get a flattened copy of data_dict to do the translation on.
flattened = ckan.lib.navl.dictization_functions.flatten_dict(
data_dict)
# Get a simple flat list of all the terms to be translated, from the
# flattened data dict.
terms = set()
for (key, value) in flattened.items():
if value in (None, True, False):
continue
elif isinstance(value, string_types):
terms.add(value)
elif isinstance(value, (int, long)):
continue
else:
for item in value:
if isinstance(value, dict):
if key == (u'organization',) and item == 'description':
terms.add(value[item])
else:
terms.add(item)
else:
terms.add(item)
# Get the translations of all the terms (as a list of dictionaries).
translations = get_action('term_translation_show')(
{'model': ckan.model},
{'terms': terms,
'lang_codes': (desired_lang_code, fallback_lang_code)})
# Transform the translations into a more convenient structure.
desired_translations = {}
fallback_translations = {}
for translation in translations:
if translation['lang_code'] == desired_lang_code:
desired_translations[translation['term']] = (
translation['term_translation'])
else:
assert translation['lang_code'] == fallback_lang_code
fallback_translations[translation['term']] = (
translation['term_translation'])
# Make a copy of the flattened data dict with all the terms replaced by
# their translations, where available.
translated_flattened = {}
for (key, value) in flattened.items():
# Don't translate names that are used for form URLs.
if key == ('name',):
translated_flattened[key] = value
elif (key[0] in ('tags', 'groups') and len(key) == 3
and key[2] == 'name'):
translated_flattened[key] = value
elif value in (None, True, False):
# Don't try to translate values that aren't strings.
translated_flattened[key] = value
elif isinstance(value, string_types):
if value in desired_translations:
translated_flattened[key] = desired_translations[value]
else:
translated_flattened[key] = fallback_translations.get(
value, value)
elif isinstance(value, (int, long, dict)):
if key == (u'organization',):
translated_flattened[key] = translate_data_dict(value);
else:
translated_flattened[key] = value
else:
translated_value = []
for item in value:
if item in desired_translations:
translated_value.append(desired_translations[item])
else:
translated_value.append(
fallback_translations.get(item, item)
)
translated_flattened[key] = translated_value
# Finally unflatten and return the translated data dict.
translated_data_dict = (ckan.lib.navl.dictization_functions
.unflatten(translated_flattened))
return translated_data_dict
def translate_resource_data_dict(data_dict):
'''Return the given dict with as many of its fields
as possible translated into the desired or the fallback language.
'''
desired_lang_code = request.environ['CKAN_LANG']
fallback_lang_code = config.get('ckan.locale_default', 'en')
# Get a flattened copy of data_dict to do the translation on.
flattened = ckan.lib.navl.dictization_functions.flatten_dict(
data_dict)
# Get a simple flat list of all the terms to be translated, from the
# flattened data dict.
terms = set()
for (key, value) in flattened.items():
if value in (None, True, False):
continue
elif isinstance(value, string_types):
terms.add(value)
elif isinstance(value, (int, long)):
continue
else:
for item in value:
terms.add(item)
# Get the translations of all the terms (as a list of dictionaries).
translations = ckan.logic.action.get.term_translation_show(
{'model': ckan.model},
{'terms': terms,
'lang_codes': (desired_lang_code, fallback_lang_code)})
# Transform the translations into a more convenient structure.
desired_translations = {}
fallback_translations = {}
for translation in translations:
if translation['lang_code'] == desired_lang_code:
desired_translations[translation['term']] = (
translation['term_translation'])
else:
assert translation['lang_code'] == fallback_lang_code
fallback_translations[translation['term']] = (
translation['term_translation'])
# Make a copy of the flattened data dict with all the terms replaced by
# their translations, where available.
translated_flattened = {}
for (key, value) in flattened.items():
# Don't translate names that are used for form URLs.
if key == ('name',):
if value in desired_translations:
translated_flattened[key] = desired_translations[value]
elif value in fallback_translations:
translated_flattened[key] = fallback_translations.get(value, value)
else:
translated_flattened[key] = value
elif value in (None, True, False):
# Don't try to translate values that aren't strings.
translated_flattened[key] = value
elif isinstance(value, string_types):
if value in desired_translations:
translated_flattened[key] = desired_translations[value]
else:
translated_flattened[key] = fallback_translations.get(
value, value)
elif isinstance(value, (int, long, dict)):
translated_flattened[key] = value
else:
translated_value = []
for item in value:
if item in desired_translations:
translated_value.append(desired_translations[item])
else:
translated_value.append(
fallback_translations.get(item, item)
)
translated_flattened[key] = translated_value
# Finally unflatten and return the translated data dict.
translated_data_dict = (ckan.lib.navl.dictization_functions
.unflatten(translated_flattened))
return translated_data_dict
KEYS_TO_IGNORE = ['state', 'revision_id', 'id', #title done seperately
'metadata_created', 'metadata_modified', 'site_id']
class MultilingualDataset(SingletonPlugin):
implements(IPackageController, inherit=True)
LANGS = config.get('ckan.locale_order', 'en').split(" ")
def before_index(self, search_data):
default_lang = search_data.get(
'lang_code',
config.get('ckan.locale_default', 'en')
)
## translate title
title = search_data.get('title')
search_data['title_' + default_lang] = title
title_translations = get_action('term_translation_show')(
{'model': ckan.model},
{'terms': [title],
'lang_codes': self.LANGS})
for translation in title_translations:
title_field = 'title_' + translation['lang_code']
search_data[title_field] = translation['term_translation']
## translate rest
all_terms = []
for key, value in sorted(six.iteritems(search_data)):
if key in KEYS_TO_IGNORE or key.startswith('title'):
continue
if not isinstance(value, list):
value = [value]
for item in value:
if isinstance(item, string_types):
all_terms.append(item)
field_translations = get_action('term_translation_show')(
{'model': ckan.model},
{'terms': all_terms,
'lang_codes': self.LANGS})
text_field_items = dict(('text_' + lang, []) for lang in self.LANGS)
text_field_items['text_' + default_lang].extend(all_terms)
for translation in sorted(field_translations, key=lambda tr: all_terms.index(tr['term'])):
lang_field = 'text_' + translation['lang_code']
text_field_items[lang_field].append(translation['term_translation'])
for key, value in six.iteritems(text_field_items):
search_data[key] = ' '.join(value)
return search_data
def before_search(self, search_params):
lang_set = set(self.LANGS)
try:
current_lang = request.environ['CKAN_LANG']
except TypeError as err:
if err.message == ('No object (name: request) has been registered '
'for this thread'):
# This happens when this code gets called as part of a paster
# command rather then as part of an HTTP request.
current_lang = config.get('ckan.locale_default')
else:
raise
except KeyError:
current_lang = config.get('ckan.locale_default')
# fallback to default locale if locale not in suported langs
if not current_lang in lang_set:
current_lang = config.get('ckan.locale_default')
# fallback to english if default locale is not supported
if not current_lang in lang_set:
current_lang = 'en'
# treat current lang differenly so remove from set
lang_set.remove(current_lang)
# weight current lang more highly
query_fields = 'title_%s^8 text_%s^4' % (current_lang, current_lang)
for lang in lang_set:
query_fields += ' title_%s^2 text_%s' % (lang, lang)
search_params['qf'] = query_fields
return search_params
def after_search(self, search_results, search_params):
# Translate the unselected search facets.
facets = search_results.get('search_facets')
if not facets:
return search_results
desired_lang_code = request.environ['CKAN_LANG']
fallback_lang_code = config.get('ckan.locale_default', 'en')
# Look up translations for all of the facets in one db query.
terms = set()
for facet in facets.values():
for item in facet['items']:
terms.add(item['display_name'])
translations = get_action('term_translation_show')(
{'model': ckan.model},
{'terms': terms,
'lang_codes': (desired_lang_code, fallback_lang_code)})
# Replace facet display names with translated ones.
for facet in facets.values():
for item in facet['items']:
matching_translations = [translation for
translation in translations
if translation['term'] == item['display_name']
and translation['lang_code'] == desired_lang_code]
if not matching_translations:
matching_translations = [translation for
translation in translations
if translation['term'] == item['display_name']
and translation['lang_code'] == fallback_lang_code]
if matching_translations:
assert len(matching_translations) == 1
item['display_name'] = (
matching_translations[0]['term_translation'])
return search_results
def before_view(self, dataset_dict):
# Translate any selected search facets (e.g. if we are rendering a
# group read page or the dataset index page): lookup translations of
# all the terms in c.fields (c.fields contains the selected facets)
# and save them in c.translated_fields where the templates can
# retrieve them later.
desired_lang_code = request.environ['CKAN_LANG']
fallback_lang_code = config.get('ckan.locale_default', 'en')
try:
fields = c.fields
except AttributeError:
return translate_data_dict(dataset_dict)
terms = [value for param, value in fields]
translations = get_action('term_translation_show')(
{'model': ckan.model},
{'terms': terms,
'lang_codes': (desired_lang_code, fallback_lang_code)})
c.translated_fields = {}
for param, value in fields:
matching_translations = [translation for translation in
translations if translation['term'] == value and
translation['lang_code'] == desired_lang_code]
if not matching_translations:
matching_translations = [translation for translation in
translations if translation['term'] == value and
translation['lang_code'] == fallback_lang_code]
if matching_translations:
assert len(matching_translations) == 1
translation = matching_translations[0]['term_translation']
c.translated_fields[(param, value)] = translation
# Now translate the fields of the dataset itself.
return translate_data_dict(dataset_dict)
class MultilingualGroup(SingletonPlugin):
'''The MultilingualGroup plugin translates group names and other group
fields on group read pages and on the group index page.
For example on the page /de/group/david the title "Dave's Books" at the
top of the page might be translated to "Dave's Bucher".
Datasets are also shown on group pages, but these are translated by the
MultilingualDataset plugin.
'''
implements(IGroupController, inherit=True)
implements(IOrganizationController, inherit=True)
def before_view(self, data_dict):
translated_data_dict = translate_data_dict(data_dict)
return translated_data_dict
class MultilingualTag(SingletonPlugin):
'''The MultilingualTag plugin translates tag names on tag read pages and
on the tag index page.
For example on the page /de/tag/tolstoy the title "Tag: tolstoy" at the
top of the page might be translated to "Tag: Tolstoi".
Datasets are also shown on tag pages, but these are translated by the
MultilingualDataset plugin.
'''
implements(ITagController, inherit=True)
def before_view(self, data_dict):
translated_data_dict = translate_data_dict(data_dict)
return translated_data_dict
class MultilingualResource(SingletonPlugin):
'''The MultilinguaResource plugin translate the selected resource name and description on resource
preview page.
'''
implements(IResourceController, inherit=True)
def before_show(self, data_dict):
translated_data_dict = translate_resource_data_dict(data_dict)
return translated_data_dict