- def __init__(self, locale, encoding):
- self._collator = collate.icu._ucol.Collator(locale)
- self.locale = self._collator.locale
+ """ICU-based collation."""
+
+ def __init__(self, locale, encoding=None):
+ locale, encoding = collate._locale.getpair(locale, encoding)
+ icu_locale = "root" if locale == "C" else locale
+ self._collator = _icu.Collator(icu_locale)
+ self.locale = self._collator.locale
+ self.encoding = collate._locale.encoding(encoding)
+ if self._collator.used_default_information and locale != "C":
+ raise collate.errors.InvalidLocaleError(locale)
+
+ try:
+ self._breaker = _icu.WordBreaker(icu_locale)
+ except ValueError:
+ # Thai is the only language with a special break locale,
+ # so this is a harmless error.
+ self._breaker = _icu.WordBreaker("root")
+
+ def words(self, string):
+ """Split the string along word boundries."""
+ if isinstance(string, str):
+ string = string.decode(self.encoding)
+ words = self._breaker.words(string)
+ return [w for w in words if not w.isspace()]