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