@@ -40,6 +40,8 @@ def translate_semantic_model(
4040 from pyspark .sql import SparkSession
4141 from sempy_labs .tom import connect_semantic_model
4242
43+ icons .sll_tags .append ("TranslateSemanticModel" )
44+
4345 def _clean_text (text , exclude_chars ):
4446 if exclude_chars :
4547 for char in exclude_chars :
@@ -55,7 +57,7 @@ def _clean_text(text, exclude_chars):
5557 columns = ["Object Type" , "Name" , "Description" , "Display Folder" ]
5658 )
5759
58- icons . sll_tags . append ( "TranslateSemanticModel" )
60+ final_df = pd . DataFrame ( columns = [ 'Value' , 'Translation' ] )
5961
6062 with connect_semantic_model (
6163 dataset = dataset , readonly = False , workspace = workspace
@@ -65,9 +67,9 @@ def _clean_text(text, exclude_chars):
6567 oName = _clean_text (o .Name , exclude_characters )
6668 oDescription = _clean_text (o .Description , exclude_characters )
6769 new_data = {
68- "Object Type" : "Table" ,
6970 "Name" : o .Name ,
70- "TName" : oName ,
71+ "TName" : oName ,
72+ "Object Type" : "Table" ,
7173 "Description" : o .Description ,
7274 "TDescription" : oDescription ,
7375 "Display Folder" : None ,
@@ -81,9 +83,9 @@ def _clean_text(text, exclude_chars):
8183 oDescription = _clean_text (o .Description , exclude_characters )
8284 oDisplayFolder = _clean_text (o .DisplayFolder , exclude_characters )
8385 new_data = {
84- "Object Type" : "Column" ,
8586 "Name" : o .Name ,
8687 "TName" : oName ,
88+ "Object Type" : "Column" ,
8789 "Description" : o .Description ,
8890 "TDescription" : oDescription ,
8991 "Display Folder" : o .DisplayFolder ,
@@ -97,9 +99,9 @@ def _clean_text(text, exclude_chars):
9799 oDescription = _clean_text (o .Description , exclude_characters )
98100 oDisplayFolder = _clean_text (o .DisplayFolder , exclude_characters )
99101 new_data = {
100- "Object Type" : "Measure" ,
101102 "Name" : o .Name ,
102103 "TName" : oName ,
104+ "Object Type" : "Measure" ,
103105 "Description" : o .Description ,
104106 "TDescription" : oDescription ,
105107 "Display Folder" : o .DisplayFolder ,
@@ -113,9 +115,9 @@ def _clean_text(text, exclude_chars):
113115 oDescription = _clean_text (o .Description , exclude_characters )
114116 oDisplayFolder = _clean_text (o .DisplayFolder , exclude_characters )
115117 new_data = {
116- "Object Type" : "Hierarchy" ,
117118 "Name" : o .Name ,
118119 "TName" : oName ,
120+ "Object Type" : "Hierarchy" ,
119121 "Description" : o .Description ,
120122 "TDescription" : oDescription ,
121123 "Display Folder" : o .DisplayFolder ,
@@ -128,9 +130,9 @@ def _clean_text(text, exclude_chars):
128130 oName = _clean_text (o .Name , exclude_characters )
129131 oDescription = _clean_text (o .Description , exclude_characters )
130132 new_data = {
131- "Object Type" : "Level" ,
132133 "Name" : o .Name ,
133134 "TName" : oName ,
135+ "Object Type" : "Level" ,
134136 "Description" : o .Description ,
135137 "TDescription" : oDescription ,
136138 "Display Folder" : None ,
@@ -163,152 +165,51 @@ def _clean_text(text, exclude_chars):
163165 )
164166
165167 df_panda = transDF .toPandas ()
168+ df_panda = df_panda [~ df_panda [clm ].isin ([None , '' ])][[clm , 'translation' ]]
169+
170+ df_panda = df_panda .rename (columns = {clm : 'value' })
171+ final_df = pd .concat ([final_df , df_panda ], ignore_index = True )
166172
167- def set_translation_if_exists (
168- obj , obj_type , property_name , property_value , df , lang , index
169- ):
170- if property_name in df .columns and len (property_value ) > 0 :
171- df_filt = df [
172- (df ["Object Type" ] == obj_type )
173- & (df [property_name ] == property_value )
174- ]
175- if len (df_filt ) == 1 :
176- translation = df_filt ["translation" ].str [index ].iloc [0 ]
177- tom .set_translation (
178- object = obj ,
179- language = lang ,
180- property = property_name ,
181- value = translation ,
182- )
173+ def set_translation_if_exists (object , language , property , index ):
174+
175+ if property == 'Name' :
176+ trans = object .Name
177+ elif property == 'Description' :
178+ trans = object .Description
179+ elif property == 'Display Folder' :
180+ trans = object .DisplayFolder
181+
182+ df_filt = final_df [final_df ['value' ] == trans ]
183+ if not df_filt .empty :
184+ translation_value = df_filt ['translation' ].str [index ].iloc [0 ]
185+ tom .set_translation (object = object , language = language , property = property , value = translation_value )
186+
187+ for language in languages :
188+ index = languages .index (language )
189+ tom .add_translation (language = language )
190+ print (
191+ f"{ icons .in_progress } Translating { clm .lower ()} s into the '{ language } ' language..."
192+ )
183193
184- for lang in languages :
185- i = languages .index (lang )
186- tom .add_translation (language = lang )
187- print (
188- f"{ icons .in_progress } Translating { clm .lower ()} s into the '{ lang } ' language..."
189- )
194+ for t in tom .model .Tables :
195+ set_translation_if_exists (object = t , language = language , property = 'Name' , index = index )
196+ set_translation_if_exists (object = t , language = language , property = 'Description' , index = index )
197+ for c in tom .all_columns ():
198+ set_translation_if_exists (object = c , language = language , property = 'Name' , index = index )
199+ set_translation_if_exists (object = c , language = language , property = 'Description' , index = index )
200+ set_translation_if_exists (object = c , language = language , property = 'Display Folder' , index = index )
201+ for c in tom .all_measures ():
202+ set_translation_if_exists (object = c , language = language , property = 'Name' , index = index )
203+ set_translation_if_exists (object = c , language = language , property = 'Description' , index = index )
204+ set_translation_if_exists (object = c , language = language , property = 'Display Folder' , index = index )
205+ for c in tom .all_hierarchies ():
206+ set_translation_if_exists (object = c , language = language , property = 'Name' , index = index )
207+ set_translation_if_exists (object = c , language = language , property = 'Description' , index = index )
208+ set_translation_if_exists (object = c , language = language , property = 'Display Folder' , index = index )
209+ for c in tom .all_levels ():
210+ set_translation_if_exists (object = c , language = language , property = 'Name' , index = index )
211+ set_translation_if_exists (object = c , language = language , property = 'Description' , index = index )
190212
191- for t in tom .model .Tables :
192- if t .IsHidden is False :
193- if clm == "Name" :
194- set_translation_if_exists (
195- t , "Table" , "Name" , t .Name , df_panda , lang , i
196- )
197- elif clm == "Description" :
198- set_translation_if_exists (
199- t ,
200- "Table" ,
201- "Description" ,
202- t .Description ,
203- df_panda ,
204- lang ,
205- i ,
206- )
207- for c in t .Columns :
208- if c .IsHidden is False :
209- if clm == "Name" :
210- set_translation_if_exists (
211- c , "Column" , "Name" , c .Name , df_panda , lang , i
212- )
213- elif clm == "Description" :
214- set_translation_if_exists (
215- c ,
216- "Column" ,
217- "Description" ,
218- c .Description ,
219- df_panda ,
220- lang ,
221- i ,
222- )
223- elif clm == "Display Folder" :
224- set_translation_if_exists (
225- c ,
226- "Column" ,
227- "Display Folder" ,
228- c .DisplayFolder ,
229- df_panda ,
230- lang ,
231- i ,
232- )
233- for h in t .Hierarchies :
234- if h .IsHidden is False :
235- if clm == "Name" :
236- set_translation_if_exists (
237- h ,
238- "Hierarchy" ,
239- "Name" ,
240- h .Name ,
241- df_panda ,
242- lang ,
243- i ,
244- )
245- elif clm == "Description" :
246- set_translation_if_exists (
247- h ,
248- "Hierarchy" ,
249- "Description" ,
250- h .Description ,
251- df_panda ,
252- lang ,
253- i ,
254- )
255- elif clm == "Display Folder" :
256- set_translation_if_exists (
257- h ,
258- "Hierarchy" ,
259- "Display Folder" ,
260- h .DisplayFolder ,
261- df_panda ,
262- lang ,
263- i ,
264- )
265- for lev in h .Levels :
266- if clm == "Name" :
267- set_translation_if_exists (
268- lev ,
269- "Level" ,
270- "Name" ,
271- lev .Name ,
272- df_panda ,
273- lang ,
274- i ,
275- )
276- elif clm == "Description" :
277- set_translation_if_exists (
278- lev ,
279- "Level" ,
280- "Description" ,
281- lev .Description ,
282- df_panda ,
283- lang ,
284- i ,
285- )
286- for ms in t .Measures :
287- if ms .IsHidden is False :
288- if clm == "Name" :
289- set_translation_if_exists (
290- ms , "Measure" , "Name" , ms .Name , df_panda , lang , i
291- )
292- elif clm == "Description" :
293- set_translation_if_exists (
294- ms ,
295- "Measure" ,
296- "Description" ,
297- ms .Description ,
298- df_panda ,
299- lang ,
300- i ,
301- )
302- elif clm == "Display Folder" :
303- set_translation_if_exists (
304- ms ,
305- "Measure" ,
306- "Display Folder" ,
307- ms .DisplayFolder ,
308- df_panda ,
309- lang ,
310- i ,
311- )
312213 result = pd .DataFrame (
313214 columns = [
314215 "Language" ,
0 commit comments