def generateDateParsers(): res = {} alllocales = Locale('').getAvailableLocales() for lkey in alllocales.keys(): vals = [] l = alllocales[lkey] res[str(l)] = generateDateParser(l) return res
def testAll(): from PyICU import Locale alllocales = Locale('').getAvailableLocales() for lkey in alllocales.keys(): print 'testing',str(lkey) l = alllocales[lkey] ctx = ParseCtx() p = LocaleParser.getInstance(str(l)) try: p.parse(ctx,'10 p') except Exception,e: print 'failed to parse simple time on with',str(l)
def __init__(self, txt, locale, stripEndOfLine=True): if type(txt) == StringType: txt = unicode(txt) assert(type(txt) == UnicodeType) self.txt = txt.replace(u"\t", u" ").strip() #XXX The decision of whether to replace returns with spaces # is still up for debate. It helps the BreakIterator which # will otherwise assume sentences end at the '\n' even if # it really continues on the next line. # However, if the BreakIterator is not able to find sentences # with returns stripped the entire body of self.txt will be returned. if stripEndOfLine: self.txt = self.txt.replace(u"\r", u"").replace(u"\n", u" ") if type(locale) == UnicodeType: locale = str(locale) assert(type(locale) == StringType) #XXX This could be an invalid PyICU Locale. # The i18n.i18nmanager.isValidPyICULocale # could be used to check the value. pyLocale = Locale(locale) self.iterator = BreakIterator.createSentenceInstance(pyLocale) self.iterator.setText(self.txt)
def _init(self): if self._locale is not None: self._collator = Collator.createInstance(Locale(self._locale)) else: self._collator = Collator.createInstance() if self._strength is not None: self._collator.setStrength(self._strength)
def yieldFallbackLocales(self, localeSet): yielded = set() for locale in localeSet: if not locale in yielded: yielded.add(locale) yield locale localeObj = Locale(locale) country = localeObj.getCountry() language = localeObj.getLanguage() if language: l_plus_c = "%s_%s" % (language, country) if not l_plus_c in yielded: yielded.add(l_plus_c) yield l_plus_c if language and not language in yielded: yielded.add(language) yield language
def setPyICULocale(locale): """ Sets the c{PyICU.Locale} to the value passed in c{str} locale argument. If the locale passed in not a valid / supported PyICU locale a c{I18nException} is raised. @param locale: a c{str} locale @type locale: ASCII c{str} """ lc = Locale(locale) if not isValidPyICULocale(lc): raise I18nException, "Invalid PyICU Locale: '%s'" % locale Locale.setDefault(lc) # Store a reference to the PyICU Locale instance global _PYICU_LOCALE _PYICU_LOCALE = lc
def show_settings(self, widget): dialog = gtk.Dialog("Settings" , None, gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR) dialog.add_button(gtk.STOCK_SAVE, gtk.RESPONSE_OK) main_vbox = gtk.VBox() ts = hildon.TouchSelector() ts.add(main_vbox) locales = [] locales_dict = {} for code, locale in Locale.getAvailableLocales().iteritems(): locales.append(locale.getDisplayName()) locales_dict[code] = locale.getDisplayName() locales.sort() locale_btn = hildon.PickerButton(gtk.HILDON_SIZE_FULLSCREEN_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_HORIZONTAL) locale_btn.set_text("Language:", '-') locale_select = hildon.TouchSelector(text=True) for locale in locales: locale_select.append_text(locale) locale_select.set_active(0, locales.index(Locale(self.locale).getDisplayName())) locale_btn.set_selector(locale_select) locale_btn.show_all() calendar_btn = hildon.PickerButton(gtk.HILDON_SIZE_FULLSCREEN_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_HORIZONTAL) calendar_btn.set_text("Calendar:", "-") calendar_select = hildon.TouchSelector(text=True) calendars = ["Buddhist", "Chinese", "Coptic", "Ethiopic", "Gregorian", "Hebrew", "Indian", "Islamic", "Islamic-Civil", "Japanese", "Persian", "Taiwan"] for calendar in calendars: calendar_select.append_text(calendar) calendar_select.set_active(0, calendars.index(self.calendar)) calendar_btn.set_selector(calendar_select) calendar_btn.show_all() main_vbox.pack_start(locale_btn) main_vbox.pack_start(calendar_btn) dialog.vbox.add(ts) dialog.show_all() response = dialog.run() if response == gtk.RESPONSE_OK: self.locale = [k for k, v in locales_dict.iteritems() if v == locale_select.get_current_text()][0] self.calendar = calendar_select.get_current_text() conf_client = gconf.client_get_default() conf_client.add_dir(self.gconfpath, gconf.CLIENT_PRELOAD_NONE) conf_client.set_string(self.gconfpath + "/" + self.widget_id + ".locale", self.locale) conf_client.set_string(self.gconfpath + "/" + self.widget_id + ".calendar", self.calendar) self.update_calendar() dialog.destroy()
def discoverLocaleSet(self): """ Queries the Operating System for the current locale sets. The Operating System may return one or more locales. In the case where more than one locale is returned the translation fallback will try each locale in the set in order till a resource or translation is encountered. Note: For Preview only the primary locale will be used. The discoverLocaleSet method can raise the following exceptions: 1. c{I18nException} @return: c{str} The Operating System primary locale """ assert self._init locale = None if wxIsAvailable(): # Try to get the Locale from WxPython locale = I18nLocale(wx.LANGUAGE_DEFAULT, i18nMan=self).GetName() if locale is None or len(locale.strip()) == 0: # Try to ge the Locale from PyICU locale = Locale.getDefault().getName() if locale is None or len(locale.strip()) == 0: # Try to get the Locale from a System environmental # variable locale = os.getenv("LANG") if locale is None or len(locale.strip()) == 0: # Try to get the Locale from a System environmental # variable locale = os.getenv("LANGUAGE") return locale
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from createBase import LocalizationBase from poUtility import * from distutils.dir_util import copy_tree, remove_tree, mkpath from distutils.file_util import copy_file import os, sys import build_lib try: from PyICU import Locale # We want the English textual representation # of the locale to display to the user. Locale.setDefault(Locale("en")) PYICU_INSTALLED = True except: PYICU_INSTALLED = False if sys.platform == 'darwin': # This might be specific to Intel Mac "Leopard", but playing safe # See bug 11645. TIMEOUT = -1 else: TIMEOUT = 60 def ignore(output): pass
def setUp(self): super(DatetimeFrenchFormatTestCase, self).setUp() view = self.view ICULocale.setDefault(ICULocale.getFrance()) view.tzinfo.setDefault(view.tzinfo.getInstance("Europe/Paris"))
def normalizeLocale(self, locale): return Locale.createCanonical(locale).getName()
def tearDown(self): if self.oldLocale is not None: ICULocale.setDefault(self.oldLocale) if self.oldTzinfo is not None: self.view.tzinfo.setDefault(self.oldTzinfo)
def setUp(self): super(DatetimeFormatTestCase, self).setUp() view = self.view ICULocale.setDefault(ICULocale.getUS()) view.tzinfo.setDefault(view.tzinfo.getInstance("America/Los_Angeles"))
def setUp(self): super(AbstractTimeZoneTestCase, self).setUp() self.oldLocale = ICULocale.getDefault() self.oldTzinfo = self.view.tzinfo.default
def isValidLocaleForm(self, locale): return (super(I18nManager, self).isValidLocaleForm(locale) or locale in Locale.getAvailableLocales())
def compress(text): compressed = text cfunc = None for func in (_zlib, _bz2): c = func(text) if len(c) < len(compressed): compressed = c cfunc = func if cfunc: compress_counts[cfunc.__name__] += 1 else: compress_counts['none'] += 1 return compressed collator = Collator.createInstance(Locale('')) collator.setStrength(Collator.QUATERNARY) collation_key = collator.getCollationKey def make_output_file_name(input_file, options): """ Return output file name based on input file name. >>> from minimock import Mock >>> opts = Mock('options') >>> opts.output_file = 'abc' >>> make_output_file_name('123.tar.bz2', opts) 'abc' >>> opts.output_file = None
def initOptions(chandlerDirectory, **kwds): """ Load and parse the command line options, with overrides in **kwds. Returns options """ # option name, (value, short cmd, long cmd, type flag, default, environment variable, help text) _configItems = { "parcelPath": ("-p", "--parcelPath", "s", None, "PARCELPATH", "Parcel search path"), "webserver": ("-W", "--webserver", "b", False, "CHANDLERWEBSERVER", "Activate the built-in webserver"), "profileDir": ("-P", "--profileDir", "s", None, "PROFILEDIR", "location of the Chandler Repository"), "profile": ("", "--prof", "b", False, None, "save profiling data"), "script": ("-s", "--script", "s", None, None, "script to execute after startup"), "scriptFile": ("-f", "--scriptFile", "s", None, None, "script file to execute after startup"), "stderr": ("-e", "--stderr", "b", False, None, "Echo error output to log file"), "create": ("-c", "--create", "b", False, "CREATE", "Force creation of a new repository"), "ramdb": ("-d", "--ramdb", "b", False, None, ""), "restore": ("-r", "--restore", "s", None, None, "repository backup to restore from before repository open"), "recover": ("-R", "--recover", "b", False, None, "open repository with recovery"), "nocatch": ("-n", "--nocatch", "b", False, "CHANDLERNOCATCH", ""), "wing": ("-w", "--wing", "b", False, None, ""), "komodo": ("-k", "--komodo", "b", False, None, ""), "refreshui": ("-u", "--refresh-ui", "b", False, None, "Refresh the UI from the repository during startup"), "locale": ("-l", "--locale", "s", None, None, "Set the default locale"), "encrypt": ("-S", "--encrypt", "b", False, None, "Request prompt for password for repository encryption"), "nosplash": ("-N", "--nosplash", "b", False, "CHANDLERNOSPLASH", ""), "logging": ("-L", "--logging", "s", "logging.conf", "CHANDLERLOGCONFIG", "The logging config file"), "createData": ("-C", "--createData", "s", None, None, "csv file with items definition to load after startup"), } # %prog expands to os.path.basename(sys.argv[0]) usage = "usage: %prog [options]" parser = OptionParser(usage=usage, version="%prog " + __version__) for key in _configItems: (shortCmd, longCmd, optionType, defaultValue, environName, helpText) = _configItems[key] if environName and os.environ.has_key(environName): defaultValue = os.environ[environName] if optionType == "b": parser.add_option(shortCmd, longCmd, dest=key, action="store_true", default=defaultValue, help=helpText) else: parser.add_option(shortCmd, longCmd, dest=key, default=defaultValue, help=helpText) (options, args) = parser.parse_args() if not options.profileDir: profileDir = locateProfileDir() if profileDir is None: profileDir = chandlerDirectory options.profileDir = os.path.expanduser(profileDir) for (opt, val) in kwds.iteritems(): setattr(options, opt, val) if options.locale is not None: from PyICU import Locale Locale.setDefault(Locale(options.locale)) return options
def getLocaleName(self): if PYICU_INSTALLED: return Locale(self.LOCALE).getDisplayName() return self.LOCALE
# See the License for the specific language governing permissions and # limitations under the License. from createBase import LocalizationBase from poUtility import * from distutils.dir_util import copy_tree, remove_tree, mkpath from distutils.file_util import copy_file import os, sys import build_lib try: from PyICU import Locale # We want the English textual representation # of the locale to display to the user. Locale.setDefault(Locale("en")) PYICU_INSTALLED = True except: PYICU_INSTALLED = False if sys.platform == 'darwin': # This might be specific to Intel Mac "Leopard", but playing safe # See bug 11645. TIMEOUT = -1 else: TIMEOUT = 60 def ignore(output): pass
def __init__(self): super(LocalePickerDialog, self).__init__(wx.GetApp().mainFrame, -1, u"", style=wx.DEFAULT_DIALOG_STYLE) self._locales = {} localeCodes = getAvailableChandlerLocales() icuLocales = Locale.getAvailableLocales() icuLocaleCodes = icuLocales.keys() langCodeAdded = {} linux = sys.platform.startswith("linux") for localeCode in localeCodes: langCode = stripCountryCode(localeCode) if linux and not hasWxLocale(langCode): # Not all WxPython translations are # installed by default on English # versions of Linux. Thus even # though there is a translation # egg available for Chandler, wx # will raise an error on Chandler # start up continue if hasCountryCode(localeCode) and not langCode in localeCodes: # There is no translation egg available for # the langCode (fallback). In this case, country variations # can not be made available to the user. This will # be rare if ever happen that say a "fr_CA" egg is # registered with Chandler but not an "fr" egg. self._locales[Locale(localeCode).getDisplayName()] = localeCode continue if not langCodeAdded.has_key(langCode): added = False for icuLocale in icuLocaleCodes: if icuLocale.startswith(langCode): self._locales[icuLocales[icuLocale].getDisplayName()] = icuLocale added = True if added: langCodeAdded[langCode] = True currentLocale = getLocale() currentLocaleName = Locale(currentLocale).getDisplayName() if currentLocale not in localeCodes: # Add the current locale to the locale selection list. # This case occurs when a user manual specifies a locale # on the command line or via prefs that does not have # a translation egg installed. self._locales[currentLocaleName] = currentLocale choices = self._locales.keys() choices.sort() # Now continue with the normal construction of the dialog # contents sizer = wx.BoxSizer(wx.VERTICAL) label = wx.StaticText(self, -1, _(u"Please select a language:"), size=(DIALOG_WIDTH, -1)) label.Wrap(DIALOG_WIDTH) sizer.Add(label, 0, wx.ALIGN_CENTER | wx.ALL, 10) self._localeChoices = wx.Choice(self, -1, choices=choices, size=(DIALOG_WIDTH - 70, -1)) sizer.Add(self._localeChoices, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 10) pos = self._localeChoices.FindString(currentLocaleName) if pos != wx.NOT_FOUND: self._localeChoices.SetSelection(pos) sizer.AddSpacer(10, 0) bsizer = wx.BoxSizer(wx.HORIZONTAL) # Load the wx.ICON_EXCLAMATION icon bmp = wx.ArtProvider.GetBitmap(wx.ART_WARNING, wx.ART_MESSAGE_BOX, (32, 32)) icon = wx.StaticBitmap(self, -1, bmp) bsizer.Add(icon, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5) note = wx.StaticText(self, -1, _(u"Switching languages will cause Chandler to automatically restart.")) note.Wrap(DIALOG_WIDTH) bsizer.Add(note, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5) sizer.Add(bsizer) sizer.AddSpacer(5, 0) box = wx.BoxSizer(wx.HORIZONTAL) btn = wx.Button(self, wx.ID_OK) btn.SetDefault() box.Add(btn, 0, wx.ALIGN_RIGHT | wx.ALL, 5) btn = wx.Button(self, wx.ID_CANCEL) box.Add(btn, 0, wx.ALIGN_RIGHT | wx.ALL, 5) sizer.Add(box, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.ALL, 10) self.SetSizer(sizer) self.SetAutoLayout(True) sizer.Fit(self)