From 4c8e44724d9aa2b38b516a54033ee718f998424f Mon Sep 17 00:00:00 2001 From: Michael Polidori Date: Fri, 20 Mar 2026 11:42:27 -0400 Subject: [PATCH 1/2] Fix URLs with dash newline insertion --- ckanext/translate/logic/action.py | 44 +++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/ckanext/translate/logic/action.py b/ckanext/translate/logic/action.py index b4183da..2fc5ca2 100644 --- a/ckanext/translate/logic/action.py +++ b/ckanext/translate/logic/action.py @@ -52,23 +52,42 @@ def _ignore_terms(translate_values): with io.open(ignore_list_path, "r", encoding="utf-8") as terms_file: terms = terms_file.read().splitlines() + url_pattern = re.compile(r"https?://[a-zA-Z0-9\.\-\/\?\=\&\%\#\_]+") hash_to_original = {} translate_values = list(translate_values) for index, translate_value in enumerate(translate_values): + # This was initially implemented as an attempt to fix the + # `-` character being replaced with `-\n` in translated URLs. + # It turns out, it's not required to solve that, but I'm + # leaving it because it will prevent Google from accidentally + # altering URLs. Even though the odds of that happening are low, + # it's better to be safe than sorry). + def _shield_url(url_match): + original_url = url_match.group() + url_hash = hashlib.sha256(original_url.encode("utf-8")).hexdigest()[:32] + + hash_to_original[url_hash] = original_url + + return url_hash + + translate_value = url_pattern.sub(_shield_url, translate_value) + for term in terms: pattern = re.compile(re.escape(term), re.IGNORECASE | re.UNICODE) - def replace_with_hash(match): + def _replace_with_hash(match): original_text = match.group() - hash_value = hashlib.sha256(original_text.encode("utf-8")).hexdigest() + hash_value = hashlib.sha256(original_text.encode("utf-8")).hexdigest()[ + :32 + ] if hash_value not in hash_to_original: hash_to_original[hash_value] = original_text return hash_value - translate_value = pattern.sub(replace_with_hash, translate_value) + translate_value = pattern.sub(_replace_with_hash, translate_value) translate_values[index] = translate_value @@ -108,11 +127,27 @@ def translate(context, data_dict): for index, translated_item in enumerate(response.translations): for hash_value, original_text in list(hash_to_original.items()): pattern = re.compile(re.escape(hash_value)) + # After text leaves this extension, a `\n` is inserted + # after the `-` in some cases. This happens before it + # gets to ckanext-scheming or ckanext-fluent, so it's + # likely some sort of validation/sanitization step in + # CKAN core. Replacing `-` with `%2d` prevents this + # from happening, and it's functionally equivalent in + # URLs, so it shouldn't cause any issues. + original_text = ( + original_text.replace("-", "%2d") + if original_text.startswith("http") + else original_text + ) translated_item.translated_text = pattern.sub( lambda _: original_text, translated_item.translated_text ) translated_dict.update( - {list(translate_keys)[index]: html.unescape(translated_item.translated_text)} + { + list(translate_keys)[index]: html.unescape( + translated_item.translated_text + ) + } ) return {"output": translated_dict} @@ -122,4 +157,3 @@ def get_actions(): return { "translate": translate, } - From cb1dddb5dbc53d0d0103e794364eb22a2c07a361 Mon Sep 17 00:00:00 2001 From: Michael Polidori Date: Thu, 9 Apr 2026 14:39:22 -0400 Subject: [PATCH 2/2] Fix env variable declarations --- ckanext/translate/plugin.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/ckanext/translate/plugin.py b/ckanext/translate/plugin.py index de59646..5f95501 100644 --- a/ckanext/translate/plugin.py +++ b/ckanext/translate/plugin.py @@ -2,15 +2,15 @@ import ckan.plugins.toolkit as toolkit import logging -from ckanext.translate.logic import ( - action, auth -) +from ckan.config.declaration import Declaration, Key +from ckanext.translate.logic import action, auth log = logging.getLogger(__name__) class TranslatePlugin(plugins.SingletonPlugin): plugins.implements(plugins.IConfigurer) + plugins.implements(plugins.IConfigDeclaration) plugins.implements(plugins.IAuthFunctions) plugins.implements(plugins.IActions) @@ -20,17 +20,21 @@ def update_config(self, config_): toolkit.add_public_directory(config_, "public") toolkit.add_resource("assets", "translate") - # IConfigurer - def update_config_schema(self, schema): - not_empty = toolkit.get_validator("not_empty") - ignore_missing = toolkit.get_validator("ignore_missing") - schema.update({ - 'ckanext.translate.google_service_account_file': [not_empty, str], - 'ckanext.translate.google_project_id': [not_empty, str], - 'ckanext.translate.google_location': [not_empty, str], - 'ckanext.translate.google_ignore_list_path': [ignore_missing, str], - }) - return schema + # IConfigDeclaration + def declare_config_options(self, declaration: Declaration, key: Key): + declaration.annotate("ckanext-translate settings") + declaration.declare(key.ckanext.translate.google_service_account_file).set_validators( + "not_empty unicode_safe" + ) + declaration.declare(key.ckanext.translate.google_project_id).set_validators( + "not_empty unicode_safe" + ) + declaration.declare(key.ckanext.translate.google_location).set_validators( + "not_empty unicode_safe" + ) + declaration.declare(key.ckanext.translate.ignore_list_path).set_validators( + "ignore_missing unicode_safe" + ) # IAuthFunctions def get_auth_functions(self):