3
3
import shutil
4
4
import yaml
5
5
6
+ class TranslatableAsset :
7
+ translatable_attributes = []
8
+
9
+ def __init__ (self , asset : dict ):
10
+ self .asset = asset
11
+ for key in ASSET_FOLDER_MAPPING :
12
+ if key in asset :
13
+ self .asset_type = ASSET_FOLDER_MAPPING [key ]
14
+ break
15
+
16
+ def extract_text (self ):
17
+ """
18
+ Extract text from an asset.
19
+ """
20
+ strings = []
21
+ for var_path in self .translatable_attributes :
22
+ strings .extend (self .translate_var (self .asset , var_path .split ("." )))
23
+
24
+ return strings
25
+
26
+ def translate_var (self , content , var_path ):
27
+ """
28
+ Helper method to remove content from the content dict.
29
+ """
30
+ if not content :
31
+ return []
32
+ if len (var_path ) == 1 :
33
+ if isinstance (content , list ):
34
+ strings = []
35
+ for item in content :
36
+ strings .append (item .get (var_path [0 ], "" ))
37
+ return strings
38
+ string = [content .get (var_path [0 ], "" )]
39
+ return string or []
40
+ else :
41
+ if isinstance (content , list ):
42
+ strings = []
43
+ for item in content :
44
+ strings .extend (self .translate_var (item , var_path [1 :]))
45
+ return strings
46
+ if isinstance (content , dict ):
47
+ if var_path [0 ] == "*" :
48
+ strings = []
49
+ for key , value in content .items ():
50
+ strings .extend (self .translate_var (value , var_path [1 :]))
51
+ return strings
52
+ return self .translate_var (content .get (var_path [0 ]), var_path [1 :])
53
+ else :
54
+ print ("Could not translate var_path: " , var_path , content )
55
+ return []
56
+
57
+
58
+ class DashboardAsset (TranslatableAsset ):
59
+ translatable_attributes = [
60
+ "dashboard_title" ,
61
+ "description" ,
62
+ "metadata.native_filter_configuration.name" ,
63
+ "metadata.native_filter_configuration.description" ,
64
+ "position.*.meta.text" ,
65
+ "position.*.meta.code" ,
66
+ ]
67
+
68
+ class ChartAsset (TranslatableAsset ):
69
+ translatable_attributes = [
70
+ "slice_name" ,
71
+ "description" ,
72
+ "params.x_axis_label" ,
73
+ "params.y_axis_label" ,
74
+ ]
75
+
76
+ class DatasetAsset (TranslatableAsset ):
77
+ translatable_attributes = [
78
+ "metrics.verbose_name" ,
79
+ "columns.verbose_name" ,
80
+ ]
81
+
82
+
6
83
ASSET_FOLDER_MAPPING = {
7
- "dashboard_title" : "dashboards" ,
8
- "slice_name" : "charts" ,
84
+ "dashboard_title" : ("dashboards" , DashboardAsset ),
85
+ "slice_name" : ("charts" , ChartAsset ),
86
+ "table_name" : ("datasets" , DatasetAsset ),
9
87
}
10
88
11
89
@@ -42,50 +120,11 @@ def mark_text_for_translation(asset):
42
120
For every asset extract the text and mark it for translation
43
121
"""
44
122
45
- def extract_text (asset , type ):
46
- """
47
- Extract text from an asset
48
- """
49
- strings = []
50
- if type == "dashboards" :
51
- strings .append (asset ["dashboard_title" ])
52
-
53
- # Gets translatable fields from filters
54
- for filter in asset ["metadata" ]["native_filter_configuration" ]:
55
- strings .append (filter ["name" ])
56
-
57
- # Gets translatable fields from charts
58
- for element in asset .get ("position" , {}).values ():
59
- if not isinstance (element , dict ):
60
- continue
61
-
62
- meta = element .get ("meta" , {})
63
-
64
- if meta .get ("text" ):
65
- strings .append (meta ["text" ])
66
-
67
- if meta .get ("code" ):
68
- strings .append (meta ["code" ])
69
-
70
- elif type == "charts" :
71
- strings .append (asset ["slice_name" ])
72
-
73
- if asset .get ("description" ):
74
- strings .append (asset ["description" ])
75
-
76
- if asset ["params" ].get ("x_axis_label" ):
77
- strings .append (asset ["params" ]["x_axis_label" ])
78
-
79
- if asset ["params" ].get ("y_axis_label" ):
80
- strings .append (asset ["params" ]["y_axis_label" ])
81
-
82
- return strings
83
-
84
- for key , value in ASSET_FOLDER_MAPPING .items ():
123
+ for key , (asset_type , Asset ) in ASSET_FOLDER_MAPPING .items ():
85
124
if key in asset :
86
- strings = extract_text (asset , value )
125
+ strings = Asset (asset ). extract_text ( )
87
126
print (
88
- f"Extracted { len (strings )} strings from { value } { asset .get ('uuid' )} "
127
+ f"Extracted { len (strings )} strings from { asset_type } { asset .get ('uuid' )} "
89
128
)
90
129
return strings
91
130
@@ -138,7 +177,7 @@ def compile_translations(root_path):
138
177
outfile .write ("---\n " )
139
178
# If we don't use an extremely large width, the jinja in our translations
140
179
# can be broken by newlines. So we use the largest number there is.
141
- yaml .dump (all_translations , outfile , width = math .inf )
180
+ yaml .dump (all_translations , outfile , width = math .inf , sort_keys = True )
142
181
outfile .write ("\n {{ patch('superset-extra-asset-translations')}}\n " )
143
182
144
183
# We remove these files to avoid confusion about where translations are coming
@@ -162,6 +201,7 @@ def extract_translations(root_path):
162
201
163
202
print ("Gathering text for translations..." )
164
203
STRINGS = set (get_text_for_translations (root_path ))
204
+ print (f"Extracted { len (STRINGS )} strings for translation." )
165
205
translations = {'en' : {}}
166
206
167
207
for string in STRINGS :
0 commit comments