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