@@ -38,32 +38,24 @@ def deep_merge(d1, d2):
3838class QGSReader :
3939 """ Read QGIS projects and extract data for QWC config """
4040
41- def __init__ (self , config , logger , assets_dir , translations_dir , use_cached_project_metadata , global_print_layouts ):
41+ def __init__ (self , config , logger , assets_dir , use_cached_project_metadata , global_print_layouts ):
4242 """Constructor
4343
4444 :param obj config: Config generator config
4545 :param Logger logger: Application logger
4646 :param string assets_dir: Assets directory
47- :param str translations_dir: Viewer translations directory
4847 :param bool use_cached_project_metadata: Whether to use cached project metadata
4948 :param list global_print_layouts: Global print layouts
5049 """
5150 self .config = config
5251 self .logger = logger
5352 self .assets_dir = assets_dir
54- self .translations_dir = translations_dir
5553 self .use_cached_project_metadata = use_cached_project_metadata
5654 self .global_print_layouts = global_print_layouts
5755
5856 self .qgs_resources_path = config .get ('qgis_projects_base_dir' , '/tmp/' )
5957 self .qgs_ext = config .get ('qgis_project_extension' , '.qgs' )
6058 self .nested_nrels = config .get ('generate_nested_nrel_forms' , False )
61- try :
62- with open (os .path .join (self .translations_dir , 'tsconfig.json' )) as fh :
63- self .viewer_languages = json .load (fh )['languages' ]
64- except :
65- self .logger .warning ("Failed to detect viewer languages from tsconfig.json" )
66- self .viewer_languages = ["en-US" ]
6759
6860 self .db_engine = DatabaseEngine ()
6961
@@ -147,20 +139,15 @@ def read(self, map_prefix, theme_item, edit_datasets):
147139
148140 # Build layername -> shortname lookup
149141 shortname_map = {}
150- id_name_map = {}
151142 for maplayer in root .findall ('.//maplayer' ):
152143 layernameEl = maplayer .find ('layername' )
153144 if layernameEl is not None :
154145 shortnameEl = maplayer .find ('shortname' )
155- idEl = maplayer .find ('id' )
156146 shortname = shortnameEl .text if shortnameEl is not None else layernameEl .text
157147 shortname_map [layernameEl .text ] = shortname
158- if idEl is not None :
159- id_name_map [idEl .text ] = shortname
160148
161149 return {
162150 "project_crs" : self .__project_crs (root ),
163- "translations" : self .__theme_translations (qgs_dir , projectname , id_name_map ),
164151 "print_templates" : self .__print_templates (root , shortname_map ),
165152 "visibility_presets" : self .__visibility_presets (root ),
166153 "layer_metadata" : self .__layer_metadata (root , shortname_map , map_prefix , edit_datasets , theme_item , qgs_dir ),
@@ -172,76 +159,6 @@ def __project_crs(self, root):
172159 authid = root .find ('./projectCrs/spatialrefsys/authid' )
173160 return authid .text if authid is not None else None
174161
175- def __theme_translations (self , qgs_dir , projectname , id_name_map ):
176- """ Read theme portion of translations from <projectname>_<lang>.json. """
177- all_translations = {}
178-
179- for language in self .viewer_languages :
180- translations = {}
181-
182- ts_file = os .path .join (qgs_dir , f"{ projectname } _{ language } .ts" )
183- if not os .path .exists (ts_file ):
184- ts_file = os .path .join (qgs_dir , f"{ projectname } _{ language [0 :2 ]} .ts" )
185- if os .path .exists (ts_file ):
186- self .logger .info ('Reading project translations %s' % ts_file )
187- try :
188- ts_document = ElementTree .parse (ts_file )
189-
190- # Build translation string lookup
191- for context in ts_document .findall ("./context" ):
192- context_name = context .find ('./name' )
193- if context_name is None :
194- continue
195-
196- context_name_parts = context_name .text .split (":" )
197- key = None
198- if len (context_name_parts ) >= 3 and context_name_parts [0 ] == "project" and context_name_parts [1 ] == "layers" :
199- # replace layer id with layer name
200- layername = id_name_map [context_name_parts [2 ]]
201-
202- if len (context_name_parts ) == 3 :
203- ts_path = f"layertree"
204- key = layername
205- elif len (context_name_parts ) == 4 and context_name_parts [3 ] == "fieldaliases" :
206- ts_path = f"layers.{ layername } .fields"
207- elif len (context_name_parts ) == 4 and context_name_parts [3 ] == "formcontainers" :
208- ts_path = f"layers.{ layername } .form"
209- elif len (context_name_parts ) == 2 and context_name_parts [0 ] == "project" and context_name_parts [1 ] == "layergroups" :
210- ts_path = f"layertree"
211- else :
212- # Unknown ts context
213- continue
214-
215- context_ts = translations
216- for entry in ts_path .split ("." ):
217- context_ts [entry ] = context_ts .get (entry , {})
218- context_ts = context_ts [entry ]
219-
220- for message in context .findall ("./message" ):
221- source = message .find ('./source' )
222- translation = message .find ('./translation' )
223- if source is not None and translation is not None and translation .get ('type' , '' ) != "unfinished" :
224- context_ts [key or source .text ] = translation .text
225-
226- except Exception as e :
227- self .logger .info ('Failed to auxiliary project translations %s: %s' % (ts_file , str (e )))
228-
229- json_file = os .path .join (qgs_dir , f"{ projectname } _{ language } .json" )
230- if not os .path .exists (json_file ):
231- json_file = os .path .join (qgs_dir , f"{ projectname } _{ language [0 :2 ]} .json" )
232- if os .path .exists (json_file ):
233- self .logger .info ('Reading auxiliary project translations %s' % json_file )
234- try :
235- with open (json_file ) as fh :
236- translations = deep_merge (translations , json .load (fh ))
237- except Exception as e :
238- self .logger .info ('Failed to read auxiliary project translations %s: %s' % (json_file , str (e )))
239-
240- if translations :
241- all_translations [language ] = translations
242-
243- return all_translations
244-
245162 def __print_templates (self , root , shortname_map ):
246163 """ Collect print templates from QGS and merge with global print layouts. """
247164
0 commit comments