-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[Fixes #13646] : Use multilang entries in ISO19115 #14421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
361a406
4bb2667
f51f9c5
83d9f1b
f04f183
c6d205f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| ######################################################################### | ||
| # | ||
| # Copyright (C) 2026 OSGeo | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # This program is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| # | ||
| ######################################################################### | ||
|
|
||
| import logging | ||
|
|
||
| from django import template | ||
| from django.conf import settings | ||
|
|
||
| from geonode.metadata.multilang.utils import ( | ||
| get_default_language, | ||
| get_3_from_2, | ||
| get_2_from_3, | ||
| get_multilang_field_names, | ||
| get_all_multilang_fields, | ||
| ) | ||
|
|
||
| register = template.Library() | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _language_label(language_code_2, fallback): | ||
| """ | ||
| Return the display label for a configured language. | ||
| """ | ||
| language_labels = {code.split("-")[0]: label for code, label in settings.LANGUAGES} | ||
|
|
||
| return language_labels.get(language_code_2, fallback) | ||
|
|
||
|
|
||
| def _language_descriptor(lang_2, lang_3): | ||
| """Build the language descriptor used by the ISO template.""" | ||
| return { | ||
| "id": f"locale-{lang_2}", | ||
| "iso639_2": lang_3, | ||
| "label": _language_label(lang_2, lang_3), | ||
| "encoding": "utf8", | ||
| } | ||
|
|
||
|
|
||
| @register.filter(name="is_multilang") | ||
| def is_multilang(field_name): | ||
| """Return whether the field is configured as multilingual.""" | ||
| return field_name in getattr(settings, "MULTILANG_FIELDS", ()) | ||
|
|
||
|
|
||
| @register.simple_tag(name="multilang_values") | ||
| def multilang_values(field_name, metadata): | ||
| """ | ||
| Return all translations for a multilingual field except the default | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "except the default" should now be removed |
||
| language. | ||
|
nrjadkry marked this conversation as resolved.
|
||
| """ | ||
| if not isinstance(metadata, dict) or not is_multilang(field_name): | ||
| return [] | ||
|
|
||
| return [ | ||
| { | ||
| "locale": language_code, | ||
| "text": metadata[translated_field], | ||
| } | ||
| for language_code, translated_field in get_multilang_field_names(field_name) | ||
| if metadata.get(translated_field) | ||
| ] | ||
|
|
||
|
|
||
| @register.simple_tag(name="language_info") | ||
| def language_info(lang_3): | ||
| """ | ||
| Return information about a single ISO639-2 language. | ||
| """ | ||
| if not lang_3: | ||
| lang_2 = get_default_language() | ||
| lang_3 = get_3_from_2(lang_2) | ||
| if lang_3 is None: | ||
| logger.warning( | ||
| "No ISO639-2 language found for '%s'; using 'eng' as the default.", | ||
| lang_2, | ||
| ) | ||
| lang_3 = "und" | ||
| elif len(lang_3) == 2: | ||
| lang_2 = lang_3 | ||
| lang_3 = get_3_from_2(lang_2) or "und" | ||
| else: | ||
| lang_2 = get_2_from_3(lang_3) | ||
|
|
||
| if lang_2 is None: | ||
| logger.warning( | ||
| "No ISO639-1 language found for '%s'; using the ISO639-2 code as the label.", | ||
| lang_3, | ||
| ) | ||
|
|
||
| return _language_descriptor( | ||
| lang_2, | ||
| lang_3, | ||
| ) | ||
|
etj marked this conversation as resolved.
|
||
|
|
||
|
|
||
| @register.simple_tag(name="languages_info") | ||
| def languages_info(metadata, fields=None): | ||
| """ | ||
| Return descriptors for all translated languages present in the metadata. | ||
| """ | ||
| if not isinstance(metadata, dict): | ||
| return [] | ||
|
|
||
| multilingual_fields = set(fields or settings.MULTILANG_FIELDS) | ||
|
|
||
| languages = set() | ||
|
|
||
| for (field_name, language_code), multilang_field_name in get_all_multilang_fields().items(): | ||
| if field_name not in multilingual_fields or not metadata.get(multilang_field_name): | ||
| continue | ||
|
|
||
| languages.add(language_code) | ||
|
|
||
| descriptors = [] | ||
| for language_code in sorted(languages): | ||
| lang_3 = get_3_from_2(language_code) | ||
|
|
||
| if lang_3 is None: | ||
| logger.warning( | ||
| "No entry in settings.LANGUAGE_MAPPINGS for language '%s'; skipping this entry.", | ||
| language_code, | ||
| ) | ||
| continue | ||
|
|
||
| descriptors.append(_language_descriptor(language_code, lang_3)) | ||
|
|
||
| return descriptors | ||
|
nrjadkry marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,3 +59,32 @@ def get_all_multilang_fields(): | |
| for field in settings.MULTILANG_FIELDS | ||
| for lang in get_2letters_languages() | ||
| } | ||
|
|
||
|
|
||
| def get_3_from_2(lang_2): | ||
| """ | ||
| Return the preferred ISO 639-2 (3-letter) code for an ISO 639-1 code. | ||
| """ | ||
| mappings = getattr(settings, "LANGUAGE_MAPPINGS", ()) | ||
| if not isinstance(mappings, dict): | ||
| mappings = dict(mappings) | ||
|
|
||
| code = mappings.get(lang_2) | ||
| if isinstance(code, (list, tuple)): | ||
| return code[0] if code else None | ||
| return code | ||
|
|
||
|
|
||
| def get_2_from_3(lang_3): | ||
| """ | ||
| Return the ISO 639-1 (2-letter) code for a given ISO 639-2 (3-letter) code. | ||
| """ | ||
| mappings = getattr(settings, "LANGUAGE_MAPPINGS", ()) | ||
| if isinstance(mappings, dict): | ||
| mappings = mappings.items() | ||
|
|
||
| for lang_2, lang_3_codes in mappings: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop will be executed over and over.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated to use only a few set of mappings for Languages |
||
| codes = lang_3_codes if isinstance(lang_3_codes, (list, tuple)) else (lang_3_codes,) | ||
| if lang_3 in codes: | ||
| return lang_2 | ||
| return None | ||
|
nrjadkry marked this conversation as resolved.
|
||
Uh oh!
There was an error while loading. Please reload this page.