Skip to content

Instantly share code, notes, and snippets.

@tyrylu
Created September 7, 2012 19:24
Show Gist options
  • Save tyrylu/3668849 to your computer and use it in GitHub Desktop.
Save tyrylu/3668849 to your computer and use it in GitHub Desktop.
Localization issues
From 2c5b0fdff23bbfb7fa5a9f761478269e2d431513 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Tyrychtr?= <lukastyrychtr@gmail.com>
Date: Fri, 7 Sep 2012 21:12:26 +0200
Subject: [PATCH] Localization fixes.
---
speech.py | 9 ++++++---
united_guards.py | 18 ++++++++++++++++--
2 files changed, 22 insertions(+), 5 deletions(-)
diff --git a/speech.py b/speech.py
index 5e74935..5b860b6 100644
--- a/speech.py
+++ b/speech.py
@@ -1,9 +1,9 @@
-#!/usr/bin/env python
+#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#simple speech interface for speech dispatcher and windows screenreaders + SAPI
#released on September 03, 2012 by Vojtěch Polášek <vojtech.polasek@gmail.com>
#This is helper module for my software
-import sys
+import sys, locale
class Speaker:
"""class for speaking"""
@@ -12,6 +12,9 @@ class Speaker:
import speechd
self.used = "speechd"
self.s = speechd.Speaker("pyspeech", "pyspeech")
+ #Try to set a voice which corresponds to the language used.
+ lc, enc = locale.getdefaultlocale()
+ self.s.set_language(lc.split("_")[0])
self.say = self.spdSpeak
self.stop = self.s.cancel
elif sys.platform == "win32":
@@ -19,7 +22,7 @@ class Speaker:
self.used = "srapi"
self.s = ctypes.windll.ScreenreaderAPI
self.s.sapiEnable(1)
- self.say = self.s.sayStringA
+ self.say = self.s.sayStringW
self.stop = self.s.stopSpeech
def spdSpeak(self, text, interrupt):
if interrupt == 1:
diff --git a/united_guards.py b/united_guards.py
index b4128e4..71d8e82 100644
--- a/united_guards.py
+++ b/united_guards.py
@@ -5,12 +5,26 @@
#see included README file for more info
#initialisation
-import time, pygame, menu, os.path, random,speech, sys, gettext
+import time, pygame, menu, os.path, random,speech, sys, gettext, locale
#gettext initialisation
gettext.bindtextdomain('messages', 'lang')
gettext.textdomain('messages')
-_ = gettext.gettext
+#Determine used language
+langs = []
+lang, cp = locale.getdefaultlocale()
+if lang:
+ #It will be something like cs_cz. So we will try the full and the language code.
+ langs.append(lang)
+langs.append(lang.split("_")[0])
+#Ensure at-least en and en_US are there.
+if "en" not in langs:
+ langs.append("en")
+if "en_US" not in langs:
+ langs.append("en_US")
+trans = gettext.translation("messages", "lang", languages=langs, fallback=True)
+#We must be prepared for unicode strings.
+_ = trans.ugettext
#pygame initialisation
pygame.init()
--
1.7.10.msysgit.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment