Package horizons :: Package gui :: Module i18n
[hide private]
[frames] | no frames]

Source Code for Module horizons.gui.i18n

 1  # ################################################### 
 2  # Copyright (C) 2013-2017 The Unknown Horizons Team 
 3  # team@unknown-horizons.org 
 4  # This file is part of Unknown Horizons. 
 5  # 
 6  # Unknown Horizons is free software; you can redistribute it and/or modify 
 7  # it under the terms of the GNU General Public License as published by 
 8  # the Free Software Foundation; either version 2 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # This program is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  # GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with this program; if not, write to the 
18  # Free Software Foundation, Inc., 
19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
20  # ################################################### 
21   
22  import logging 
23  import weakref 
24   
25  from horizons.gui import translations 
26  from horizons.messaging import LanguageChanged 
27   
28  log = logging.getLogger("i18n") 
29   
30  # save translated widgets 
31  translated_widgets = {} 
32   
33   
34 -def translate_widget(untranslated, filename):
35 """ 36 Load widget translations from guitranslations.py file. 37 Its entries look like {element_name: (attribute, translation)}. 38 The translation is not applied to inactive widgets. 39 Check update_all_translations for the application. 40 """ 41 global translated_widgets 42 if filename in translations.text_translations: 43 for (element_name, attribute), translation in translations.text_translations[filename].items(): 44 widget = untranslated.findChild(name=element_name) 45 if widget is not None: 46 replace_attribute(widget, attribute, translation) 47 widget.adaptLayout() 48 else: 49 log.debug('No translation key in i18n.guitranslations for file %s', filename) 50 51 # save as weakref for updates to translations 52 translated_widgets[filename] = weakref.ref(untranslated) 53 54 return untranslated
55 56
57 -def replace_attribute(widget, attribute, text):
58 if hasattr(widget, attribute): 59 setattr(widget, attribute, text) 60 else: 61 log.warning("Could not replace attribute %s in widget %s", attribute, widget)
62 63
64 -def update_translations(message):
65 global translated_widgets 66 translations.set_translations() 67 68 for (filename, widget) in translated_widgets.items(): 69 widget = widget() # resolve weakref 70 if not widget: 71 continue 72 all_widgets = translations.text_translations.get(filename, {}) 73 for (element_name, attribute), translation in all_widgets.items(): 74 element = widget.findChild(name=element_name) 75 if element is None: 76 # something hidden by pychan currently, we cannot find it 77 log.debug('Could not find element `%s` in widget `%s` - ' 78 'assuming it is hidden', element_name, widget) 79 continue 80 replace_attribute(element, attribute, translation) 81 #NOTE pychan + reloading font = ??? 82 element.font = element.font 83 widget.adaptLayout()
84 85 86 LanguageChanged.subscribe(update_translations) 87