add internatialization support for Beremiz_service.py
authorAndrey Skvortsov <andrej.skvortzov@gmail.com>
Wed, 07 Dec 2016 18:32:34 +0300
changeset 1595 b3cc68dd037d
parent 1594 049b7144a710
child 1596 f5868f866d2b
add internatialization support for Beremiz_service.py
Beremiz.py
Beremiz_service.py
connectors/PYRO/__init__.py
connectors/WAMP/__init__.py
runtime/NevowServer.py
--- a/Beremiz.py	Wed Dec 07 18:25:37 2016 +0300
+++ b/Beremiz.py	Wed Dec 07 18:32:34 2016 +0300
@@ -574,7 +574,7 @@
                     {False : "-x 0", True :"-x 1"}[taskbaricon],
                     self.local_runtime_tmpdir),
                 no_gui=False,
-                timeout=500, keyword = "working",
+                timeout=500, keyword = self.local_runtime_tmpdir,
                 cwd = self.local_runtime_tmpdir)
             self.local_runtime.spin()
         return self.runtime_port
--- a/Beremiz_service.py	Wed Dec 07 18:25:37 2016 +0300
+++ b/Beremiz_service.py	Wed Dec 07 18:32:34 2016 +0300
@@ -112,6 +112,48 @@
 if __name__ == '__main__':
     __builtin__.__dict__['_'] = lambda x: x
 
+def Bpath(*args):
+    return os.path.join(beremiz_dir,*args)
+
+def SetupI18n():
+    # Import module for internationalization
+    import gettext
+
+    # Get folder containing translation files
+    localedir = os.path.join(beremiz_dir,"locale")
+    # Get the default language
+    langid = wx.LANGUAGE_DEFAULT
+    # Define translation domain (name of translation files)
+    domain = "Beremiz"
+
+    # Define locale for wx
+    loc = __builtin__.__dict__.get('loc', None)
+    if loc is None:
+        loc = wx.Locale(langid)
+        __builtin__.__dict__['loc'] = loc
+        # Define location for searching translation files
+    loc.AddCatalogLookupPathPrefix(localedir)
+    # Define locale domain
+    loc.AddCatalog(domain)
+
+
+    import locale
+    global default_locale
+    default_locale = locale.getdefaultlocale()[1]
+
+
+    # sys.stdout.encoding = default_locale
+    # if Beremiz_service is started from Beremiz IDE
+    # sys.stdout.encoding is None (that means 'ascii' encoding').
+    # And unicode string returned by wx.GetTranslation() are
+    # automatically converted to 'ascii' string.
+    def unicode_translation(message):
+        return wx.GetTranslation(message).encode(default_locale)
+
+    if __name__ == '__main__':
+        __builtin__.__dict__['_'] = unicode_translation
+        # __builtin__.__dict__['_'] = wx.GetTranslation
+
 if enablewx:
     try:
         import wxversion
@@ -133,34 +175,8 @@
             app = wx.PySimpleApp(redirect=False)
         app.SetTopWindow(wx.Frame(None, -1))
 
-        # Import module for internationalization
-        import gettext
-
-        def Bpath(*args):
-            return os.path.join(beremiz_dir,*args)
-
-        # Get folder containing translation files
-        localedir = os.path.join(beremiz_dir,"locale")
-        # Get the default language
-        langid = wx.LANGUAGE_DEFAULT
-        # Define translation domain (name of translation files)
-        domain = "Beremiz"
-
-        # Define locale for wx
-        loc = __builtin__.__dict__.get('loc', None)
-        if loc is None:
-            loc = wx.Locale(langid)
-            __builtin__.__dict__['loc'] = loc
-        # Define location for searching translation files
-        loc.AddCatalogLookupPathPrefix(localedir)
-        # Define locale domain
-        loc.AddCatalog(domain)
-
-        def unicode_translation(message):
-            return wx.GetTranslation(message).encode("utf-8")
-
-        if __name__ == '__main__':
-            __builtin__.__dict__['_'] = wx.GetTranslation#unicode_translation
+        default_locale = None
+        SetupI18n()
 
         defaulticon = wx.Image(Bpath("images", "brz.png"))
         starticon = wx.Image(Bpath("images", "icoplay24.png"))
@@ -175,7 +191,7 @@
                         event(self, function)
 
 
-            def __init__(self, parent, message, caption = "Please enter text", defaultValue = "",
+            def __init__(self, parent, message, caption = _("Please enter text"), defaultValue = "",
                                style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition):
                 wx.TextEntryDialog.__init__(self, parent, message, caption, defaultValue, style, pos)
 
@@ -406,9 +422,13 @@
                                 self.pyruntimevars)
         uri = self.daemon.connect(self.plcobj,"PLCObject")
 
-        print "Pyro port :",self.port
-        print "Pyro object's uri :",uri
-        print "Current working directory :",self.workdir
+        print _("Pyro port :"), self.port
+        print _("Pyro object's uri :"), uri
+
+        # Beremiz IDE detects daemon start by looking
+        # for self.workdir in the daemon's stdout.
+        # Therefore don't delete the following line
+        print _("Current working directory :"), self.workdir
 
         # Configure and publish service
         # Not publish service if localhost in address params
@@ -416,7 +436,7 @@
             self.ip_addr is not None and
             self.ip_addr != "localhost" and
             self.ip_addr != "127.0.0.1"):
-            print "Publishing service on local network"
+            print _("Publishing service on local network")
             self.servicepublisher = ServicePublisher.ServicePublisher()
             self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port)
 
@@ -451,7 +471,7 @@
 
             havetwisted = True
         except:
-            print "Twisted unavailable."
+            print _("Twisted unavailable.")
             havetwisted = False
 
 pyruntimevars = {}
@@ -529,7 +549,7 @@
         try:
             import runtime.NevowServer as NS
         except Exception, e:
-            print "Nevow/Athena import failed :", e
+            print _("Nevow/Athena import failed :"), e
             webport = None
         NS.WorkingDir = WorkingDir
 
@@ -537,7 +557,7 @@
         try:
             import runtime.WampClient as WC
         except Exception, e:
-            print "WAMP import failed :", e
+            print _("WAMP import failed :"), e
             wampconf = None
 
 # Load extensions
@@ -553,7 +573,7 @@
             pyruntimevars["website"] = website
             statuschange.append(NS.website_statuslistener_factory(website))
         except Exception, e:
-            print "Nevow Web service failed.", e
+            print _("Nevow Web service failed. "), e
 
     if wampconf is not None :
         try:
@@ -561,7 +581,7 @@
             pyruntimevars["wampsession"] = WC.GetSession
             WC.SetServer(pyroserver)
         except Exception, e:
-            print "WAMP client startup failed.", e
+            print _("WAMP client startup failed. "), e
 
 
 if havetwisted or havewx:
--- a/connectors/PYRO/__init__.py	Wed Dec 07 18:25:37 2016 +0300
+++ b/connectors/PYRO/__init__.py	Wed Dec 07 18:32:34 2016 +0300
@@ -112,10 +112,10 @@
             try:
                 return func(*args, **kwargs)
             except Pyro.errors.ConnectionClosedError, e:
-                confnodesroot.logger.write_error("Connection lost!\n")
+                confnodesroot.logger.write_error(_("Connection lost!\n"))
                 confnodesroot._SetConnector(None)
             except Pyro.errors.ProtocolError, e:
-                confnodesroot.logger.write_error("Pyro exception: " + str(e) + "\n")
+                confnodesroot.logger.write_error(_("Pyro exception: %s\n") % e)
             except Exception, e:
                 # confnodesroot.logger.write_error(traceback.format_exc())
                 errmess = ''.join(Pyro.util.getPyroTraceback(e))
--- a/connectors/WAMP/__init__.py	Wed Dec 07 18:25:37 2016 +0300
+++ b/connectors/WAMP/__init__.py	Wed Dec 07 18:32:34 2016 +0300
@@ -108,7 +108,7 @@
                         reactor, _WampSession.call, wampfuncname,
                         *args,**kwargs)
                 except TransportLost, e:
-                    confnodesroot.logger.write_error("Connection lost!\n")
+                    confnodesroot.logger.write_error(_("Connection lost!\n"))
                     confnodesroot._SetConnector(None)
                 except Exception,e:
                     errmess = traceback.format_exc()
--- a/runtime/NevowServer.py	Wed Dec 07 18:25:37 2016 +0300
+++ b/runtime/NevowServer.py	Wed Dec 07 18:32:34 2016 +0300
@@ -178,7 +178,7 @@
 
     listening = False
     reactor.listenTCP(port, site)
-    print "Http interface port :",port
+    print _("HTTP interface port :"), port
     return website
 
 class statuslistener: