Package Gnumed :: Package wxpython :: Module gmI18nWidgets
[frames] | no frames]

Source Code for Module Gnumed.wxpython.gmI18nWidgets

  1  """GNUmed I18n/L10n related widgets. 
  2  """ 
  3  #================================================================ 
  4  __author__ = 'karsten.hilbert@gmx.net' 
  5  __license__ = 'GPL v2 or later (details at http://www.gnu.org)' 
  6   
  7  # stdlib 
  8  import logging 
  9  import sys 
 10   
 11   
 12  # 3rd party 
 13  import wx 
 14   
 15   
 16  # GNUmed 
 17  if __name__ == '__main__': 
 18          sys.path.insert(0, '../../') 
 19   
 20  from Gnumed.pycommon import gmTools 
 21  from Gnumed.pycommon import gmNetworkTools 
 22  from Gnumed.pycommon import gmPG2 
 23  from Gnumed.pycommon import gmI18N 
 24  from Gnumed.pycommon import gmDispatcher 
 25   
 26   
 27  from Gnumed.wxpython import gmListWidgets 
 28  from Gnumed.wxpython import gmEditArea 
 29  from Gnumed.wxpython import gmPhraseWheel 
 30  from Gnumed.wxpython import gmGuiHelpers 
 31  from Gnumed.wxpython import gmAuthWidgets 
 32   
 33   
 34  _log = logging.getLogger('gm.ui') 
 35   
 36  #============================================================================== 
 37  from Gnumed.wxGladeWidgets import wxgDatabaseTranslationEAPnl 
 38   
39 -class cDatabaseTranslationEAPnl(wxgDatabaseTranslationEAPnl.wxgDatabaseTranslationEAPnl, gmEditArea.cGenericEditAreaMixin):
40
41 - def __init__(self, *args, **kwargs):
42 43 try: 44 data = kwargs['translation'] 45 del kwargs['translation'] 46 except KeyError: 47 data = None 48 49 wxgDatabaseTranslationEAPnl.wxgDatabaseTranslationEAPnl.__init__(self, *args, **kwargs) 50 gmEditArea.cGenericEditAreaMixin.__init__(self) 51 52 # Code using this mixin should set mode and data 53 # after instantiating the class: 54 self.mode = 'new' 55 self.data = data 56 if data is not None: 57 self.mode = 'edit'
58 59 #self.__init_ui() 60 #---------------------------------------------------------------- 61 # def __init_ui(self): 62 # # adjust phrasewheels etc 63 #---------------------------------------------------------------- 64 # generic Edit Area mixin API 65 #----------------------------------------------------------------
66 - def _valid_for_save(self):
67 68 fields = [self._TCTRL_original, self._TCTRL_translation, self._TCTRL_language] 69 70 has_errors = False 71 for field in fields: 72 if field.GetValue().strip() == '': 73 has_errors = True 74 field.SetBackgroundColour(gmPhraseWheel.color_prw_invalid) 75 field.SetFocus() 76 else: 77 field.SetBackgroundColour(gmPhraseWheel.color_prw_valid) 78 79 return (has_errors is False)
80 #----------------------------------------------------------------
81 - def _save_as_new(self):
82 self.data = gmPG2.update_translation_in_database ( 83 language = self._TCTRL_language.GetValue().strip(), 84 original = self._TCTRL_original.GetValue().strip(), 85 translation = self._TCTRL_translation.GetValue().strip() 86 ) 87 return True
88 #----------------------------------------------------------------
89 - def _save_as_update(self):
90 return self._save_as_new()
91 #----------------------------------------------------------------
92 - def _refresh_as_new(self):
93 self._TCTRL_original.SetValue('') 94 self._TCTRL_original.SetEditable(True) 95 self._TCTRL_translation.SetValue('') 96 self._TCTRL_language.SetValue('') 97 self._TCTRL_original.SetFocus()
98 #----------------------------------------------------------------
99 - def _refresh_from_existing(self):
100 self._TCTRL_original.SetValue(self.data['orig']) 101 self._TCTRL_original.SetEditable(False) 102 self._TCTRL_translation.SetValue(gmTools.coalesce(self.data['trans'])) 103 self._TCTRL_language.SetValue(gmTools.coalesce(self.data['lang'])) 104 self._TCTRL_translation.SetFocus()
105 #----------------------------------------------------------------
107 self._TCTRL_original.SetValue(self.data['orig']) 108 self._TCTRL_original.SetEditable(False) 109 self._TCTRL_translation.SetValue('') 110 self._TCTRL_language.SetValue('') 111 self._TCTRL_translation.SetFocus()
112 #---------------------------------------------------------------- 113 114 #------------------------------------------------------------------------------
115 -def edit_translation(parent=None, translation=None, single_entry=False):
116 ea = cDatabaseTranslationEAPnl(parent, -1) 117 ea.data = translation 118 ea.mode = gmTools.coalesce(translation, 'new', 'edit') 119 dlg = gmEditArea.cGenericEditAreaDlg2(parent, -1, edit_area = ea, single_entry = single_entry) 120 dlg.SetTitle(gmTools.coalesce(translation, _('Adding new translation'), _('Editing translation'))) 121 if dlg.ShowModal() == wx.ID_OK: 122 dlg.Destroy() 123 return True 124 dlg.Destroy() 125 return False
126 127 #------------------------------------------------------------------------------
128 -def manage_translations(parent=None, language=None):
129 130 if parent is None: 131 parent = wx.GetApp().GetTopWindow() 132 133 if language is None: 134 langs = gmPG2.get_translation_languages() 135 for lang in [gmI18N.system_locale_level['language'], gmI18N.system_locale_level['country']]: 136 if lang not in langs: 137 langs.append(lang) 138 139 curr_lang = gmPG2.get_current_user_language() 140 try: 141 selections = [langs.index(curr_lang)] 142 except ValueError: 143 selections = None 144 145 language = gmListWidgets.get_choices_from_list ( 146 parent = parent, 147 caption = _('Selecting language for translation'), 148 msg = _('Please select the language the translations for which you want to work on.'), 149 single_selection = True, 150 can_return_empty = False, 151 columns = [_('Language')], 152 choices = langs, 153 selections = selections 154 ) 155 #--------------------------------------------------------------------- 156 def refresh(lctrl): 157 txs = gmPG2.get_database_translations(language = language, order_by = 'orig, lang') 158 items = [ [ 159 tx['orig'], 160 gmTools.coalesce(tx['lang'], ''), 161 gmTools.coalesce(tx['trans'], '') 162 ] for tx in txs ] 163 lctrl.set_string_items(items) 164 lctrl.set_data(txs)
165 #--------------------------------------------------------------------- 166 def edit(translation=None): 167 return edit_translation(parent = parent, translation = translation, single_entry = True) 168 #--------------------------------------------------------------------- 169 def delete(translation=None): 170 msg = _( 171 'Are you sure you want to delete the translation of:\n' 172 '\n' 173 '%s\n' 174 '\n' 175 'into [%s] as:\n' 176 '\n' 177 '%s\n' 178 '\n' 179 '? (Note that you must know the database administrator password !)\n' 180 ) % ( 181 gmTools.wrap ( 182 text = translation['orig'], 183 width = 60, 184 initial_indent = ' ', 185 subsequent_indent = ' ' 186 ), 187 translation['lang'], 188 gmTools.wrap ( 189 text = translation['trans'], 190 width = 60, 191 initial_indent = ' ', 192 subsequent_indent = ' ' 193 ) 194 ) 195 delete_it = gmGuiHelpers.gm_show_question ( 196 aTitle = _('Deleting translation from database'), 197 aMessage = msg 198 ) 199 if not delete_it: 200 return False 201 202 conn = gmAuthWidgets.get_dbowner_connection(procedure = _('deleting a translation')) 203 if conn is None: 204 return False 205 206 return gmPG2.delete_translation_from_database(link_obj = conn, language = translation['lang'], original = translation['orig']) 207 #--------------------------------------------------------------------- 208 def contribute_translations(item=None): 209 210 do_it = gmGuiHelpers.gm_show_question ( 211 aTitle = _('Contributing translations'), 212 aMessage = _('Do you want to contribute your translations to the GNUmed project ?') 213 ) 214 if not do_it: 215 return False 216 217 fname = gmTools.get_unique_filename(prefix = 'gm-db-translations-', suffix = '.sql') 218 gmPG2.export_translations_from_database(filename = fname) 219 220 msg = ( 221 'These are database string translations contributed by a GNUmed user.\n' 222 '\n' 223 '\tThe GNUmed "%s" Client' 224 ) % gmI18N.system_locale 225 226 if not gmNetworkTools.compose_and_send_email ( 227 auth = {'user': gmNetworkTools.default_mail_sender, 'password': 'gnumed-at-gmx-net'}, 228 sender = 'GNUmed Client <gnumed@gmx.net>', 229 receiver = ['gnumed-bugs@gnu.org'], 230 subject = '<contribution>: database translation', 231 message = msg, 232 attachments = [[fname, 'text/plain', 'quoted-printable']] 233 ): 234 gmDispatcher.send(signal = 'statustext', msg = _('Unable to send mail. Cannot contribute translations to GNUmed community.') % report, beep = True) 235 return False 236 237 gmDispatcher.send(signal = 'statustext', msg = _('Thank you for your contribution to the GNUmed community!'), beep = True) 238 return True 239 #--------------------------------------------------------------------- 240 if language is None: 241 caption = _('Showing translatable database strings for all languages.') 242 else: 243 caption = _('Showing translatable database strings for target language [%s].') % language 244 gmListWidgets.get_choices_from_list ( 245 parent = parent, 246 caption = caption, 247 columns = [ _('String'), _('Language'), _('Translation') ], 248 single_selection = True, 249 can_return_empty = False, 250 refresh_callback = refresh, 251 edit_callback = edit, 252 new_callback = edit, 253 delete_callback = delete, 254 right_extra_button = (_('Contribute'), _('Contribute translations to GNUmed community by email.'), contribute_translations), 255 ignore_OK_button = True 256 ) 257 258 #================================================================ 259 if __name__ == '__main__': 260 261 gmI18N.activate_locale() 262 gmI18N.install_domain() 263 264 if (len(sys.argv) > 1): 265 if sys.argv[1] == 'test': 266 pass 267 268 #================================================================ 269