| 
									
										
										
										
											2022-10-17 21:15:32 +03:00
										 |  |  | import json | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-31 19:56:37 +03:00
										 |  |  | from modules import errors | 
					
						
							| 
									
										
										
										
											2022-11-06 09:02:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-17 21:15:32 +03:00
										 |  |  | localizations = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def list_localizations(dirname): | 
					
						
							|  |  |  |     localizations.clear() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for file in os.listdir(dirname): | 
					
						
							|  |  |  |         fn, ext = os.path.splitext(file) | 
					
						
							|  |  |  |         if ext.lower() != ".json": | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         localizations[fn] = os.path.join(dirname, file) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-06 09:02:25 +03:00
										 |  |  |     from modules import scripts | 
					
						
							|  |  |  |     for file in scripts.list_scripts("localizations", ".json"): | 
					
						
							|  |  |  |         fn, ext = os.path.splitext(file.filename) | 
					
						
							|  |  |  |         localizations[fn] = file.path | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-17 21:15:32 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-13 19:44:55 +03:00
										 |  |  | def localization_js(current_localization_name: str) -> str: | 
					
						
							| 
									
										
										
										
											2022-10-17 21:15:32 +03:00
										 |  |  |     fn = localizations.get(current_localization_name, None) | 
					
						
							|  |  |  |     data = {} | 
					
						
							|  |  |  |     if fn is not None: | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             with open(fn, "r", encoding="utf8") as file: | 
					
						
							|  |  |  |                 data = json.load(file) | 
					
						
							|  |  |  |         except Exception: | 
					
						
							| 
									
										
										
										
											2023-05-31 19:56:37 +03:00
										 |  |  |             errors.report(f"Error loading localization from {fn}", exc_info=True) | 
					
						
							| 
									
										
										
										
											2022-10-17 21:15:32 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-13 19:44:55 +03:00
										 |  |  |     return f"window.localization = {json.dumps(data)}" |