Skip to content

Commit

Permalink
chore: refactor translation extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Mar 7, 2024
1 parent a67bd14 commit 1440802
Showing 1 changed file with 87 additions and 7 deletions.
94 changes: 87 additions & 7 deletions scripts/translate_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,89 @@
import shutil
import yaml

class TranslatableAsset:
translatable_attributes = []

def __init__(self, asset: dict):
self.asset = asset
for key in ASSET_FOLDER_MAPPING:
if key in asset:
self.asset_type = ASSET_FOLDER_MAPPING[key]
break

def extract_text(self):
"""
Extract text from an asset.
"""

def extract_text(self):
"""
Remove any variables from the content which should be omitted.
"""
strings = []
for var_path in self.translatable_attributes:
strings.extend(self.translate_var(self.asset, var_path.split(".")))

return strings

def translate_var(self, content, var_path: list):
"""
Helper method to remove content from the content dict.
"""
print("Translating var_path: ", var_path)
if not content:
return []
if len(var_path) == 1:
# If the content is a list, we need to iterate over each item in the list
if isinstance(content, list):
strings = []
for item in content:
strings.append(item.get(var_path[0], ""))
return strings
# print("Translating asset: ", self.asset_type, "var_path: ", var_path)
string = [content.get(var_path[0], "")]
return string or []
else:
# If the content is a list, we need to iterate over each item in the list
if isinstance(content, list):
strings = []
for item in content:
strings.extend(self.translate_var(item, var_path))
return strings
if isinstance(content, dict):
return self.translate_var(content.get(var_path[0], {}), var_path[1:])
else:
print("Could not translate var_path: ", var_path)
return []


class DashboardAsset(TranslatableAsset):
translatable_attributes = [
"dashboard_title",
"metadata.native_filter_configuration.name",
"position.meta.text",
"position.meta.code",
]

class ChartAsset(TranslatableAsset):
translatable_attributes = [
"slice_name",
"description",
"params.x_axis_label",
"params.y_axis_label",
]

class DatasetAsset(TranslatableAsset):
translatable_attributes = [
"metrics.verbose_name",
"columns.verbose_name",
]


ASSET_FOLDER_MAPPING = {
"dashboard_title": "dashboards",
"slice_name": "charts",
"table_name": "datasets",
"dashboard_title": ("dashboards", DashboardAsset),
"slice_name": ("charts", ChartAsset),
"table_name": ("datasets", DatasetAsset),
}


Expand Down Expand Up @@ -88,11 +167,11 @@ def extract_text(asset, type):

return strings

for key, value in ASSET_FOLDER_MAPPING.items():
for key, (asset_type, Asset) in ASSET_FOLDER_MAPPING.items():
if key in asset:
strings = extract_text(asset, value)
strings = Asset(asset).extract_text()
print(
f"Extracted {len(strings)} strings from {value} {asset.get('uuid')}"
f"Extracted {strings} strings from {asset_type} {asset.get('uuid')}"
)
return strings

Expand Down Expand Up @@ -145,7 +224,7 @@ def compile_translations(root_path):
outfile.write("---\n")
# If we don't use an extremely large width, the jinja in our translations
# can be broken by newlines. So we use the largest number there is.
yaml.dump(all_translations, outfile, width=math.inf)
yaml.dump(all_translations, outfile, width=math.inf, sort_keys=True)
outfile.write("\n{{ patch('superset-extra-asset-translations')}}\n")

# We remove these files to avoid confusion about where translations are coming
Expand Down Expand Up @@ -178,4 +257,5 @@ def extract_translations(root_path):
with open(translation_file, "w") as file:
file.write(yaml.dump(translations))


print("Done compiling translations.")

0 comments on commit 1440802

Please sign in to comment.