-
Notifications
You must be signed in to change notification settings - Fork 17
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
feat: allow to translate dataset text on chart #648
Changes from 7 commits
d6369e1
02eae45
5081220
42c2df3
21de07d
6f71c60
859c901
6eb71cc
4c8c1a7
6cc6c2e
9541153
55b96b0
2ad6530
f567c9f
b8d39af
86f8e76
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 |
---|---|---|
|
@@ -2,7 +2,6 @@ name: Tutor Integration Test | |
|
||
on: | ||
pull_request: | ||
branches: [ main ] | ||
paths-ignore: | ||
- .github/** | ||
- .ci/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
This file is used to load translations from the locale.yaml file and provide a function to get translations | ||
""" | ||
import yaml | ||
|
||
TRANSLATIONS_FILE_PATH = "/app/localization/locale.yaml" | ||
DATASET_STRINGS_FILE_PATH = "/app/localization/datasets_strings.yaml" | ||
|
||
merged_data = {} | ||
with open(TRANSLATIONS_FILE_PATH, "r") as file: | ||
yaml_content = file.read() | ||
yaml_documents = yaml_content.split("\n---\n") | ||
|
||
for doc in yaml_documents: | ||
data = yaml.safe_load(doc) | ||
if data is not None: | ||
for lang, translations in data.items(): | ||
if lang not in merged_data: | ||
merged_data[lang] = {} | ||
merged_data[lang].update(translations) | ||
|
||
|
||
ASSETS_TRANSLATIONS = merged_data | ||
|
||
DATASET_STRINGS = yaml.safe_load(open(DATASET_STRINGS_FILE_PATH, "r")) | ||
|
||
|
||
def get_translation(text, language): | ||
"""Get a translation for a text in a language""" | ||
default_text = f"{text}" | ||
LANGUAGE = ASSETS_TRANSLATIONS.get(language, {}) | ||
if not LANGUAGE: | ||
return default_text | ||
return ASSETS_TRANSLATIONS.get(language, {}).get(text) or default_text |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,11 @@ | |
cf https://superset.apache.org/docs/installation/sql-templating/ | ||
""" | ||
from superset.extensions import security_manager | ||
from pythonpath.localization import get_translation, DATASET_STRINGS | ||
from superset import security_manager | ||
import logging | ||
|
||
log = logging.getLogger(__name__) | ||
ALL_COURSES = "1 = 1" | ||
NO_COURSES = "1 = 0" | ||
|
||
|
@@ -44,4 +48,31 @@ def can_view_courses(username, field_name="course_id", **kwargs): | |
return NO_COURSES | ||
|
||
|
||
def translate_column(column_name): | ||
""" | ||
Translate a string to the given language. | ||
""" | ||
roles = security_manager.get_user_roles() | ||
log.info(f"Roles: {roles}") | ||
lang = "en" | ||
if roles: | ||
for role in roles: | ||
role_str = role.name.split(" - ") | ||
Ian2012 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len(role_str) > 1: | ||
lang = role_str[1] | ||
break | ||
|
||
strings = DATASET_STRINGS.get(column_name, []) | ||
case_format = """CASE \n {cases} \n ELSE {column_name} \n END""" | ||
single_case_format = "WHEN {column_name} = '{string}' THEN '{translation}'" | ||
cases = "\n".join( | ||
single_case_format.format(column_name=column_name, string=string, translation=get_translation(string, lang)) | ||
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. When 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. With this change, it's using: |
||
for string in strings | ||
) | ||
|
||
log.info(f"Translating {column_name} to {lang}") | ||
Ian2012 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log.info(case_format.format(column_name=column_name, cases=cases)) | ||
|
||
return case_format.format(column_name=column_name, cases=cases) | ||
|
||
{{patch("superset-jinja-filters")}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
enrollment_mode: | ||
- audit | ||
- verified | ||
- honor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the
--debug
flag meant to stay in?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this only will affect dev environment, local en k8s works the same as before.