Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions ckanext/translate/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}
Expand All @@ -122,4 +157,3 @@ def get_actions():
return {
"translate": translate,
}

32 changes: 18 additions & 14 deletions ckanext/translate/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):
Expand Down
Loading