def best_lang_old(self, locale_codes):
# Construct an expression that will find the best locale according to list.
scores = {}
current_score = 1
locale_collection = Locale.locale_collection
locale_collection_subsets = Locale.locale_collection_subsets
for locale_code in locale_codes:
# is the locale there?
locale_id = locale_collection.get(locale_code, None)
if locale_id:
scores[locale_id] = current_score
current_score += 1
# is the base locale there?
root_locale = Locale.extract_root_locale(locale_code)
if root_locale not in locale_codes:
locale_id = locale_collection.get(root_locale, None)
if locale_id:
scores[locale_id] = current_score
current_score += 1
# is another variant there?
mt_variants = list()
found = False
for sublocale in locale_collection_subsets[root_locale]:
if sublocale in locale_codes:
continue
if sublocale == root_locale:
continue
if Locale.locale_is_machine_translated(sublocale):
mt_variants.append(sublocale)
continue
locale_id = locale_collection.get(sublocale, None)
if locale_id:
scores[locale_id] = current_score
found = True
if found:
current_score += 1
# Put MT variants as last resort.
for sublocale in mt_variants:
locale_id = locale_collection.get(sublocale, None)
if locale_id:
scores[locale_id] = current_score
# Assume each mt variant to have a lower score.
current_score += 1
c = case(scores, value=LangStringEntry.locale_id,
else_=current_score)
q = Query(LangStringEntry).order_by(c).limit(1).subquery()
return aliased(LangStringEntry, q)
评论列表
文章目录