def get_gettext_languages(self): """Finds the languages that already exist inside the gettext directory. This is a little more though than on the Android side, since we give the user a lot of flexibility in configuring how the .po files are layed out. Return value is a list of ``Language`` instances. """ # Build a glob pattern based on the layout. This will enable # us to easily get a list of files that match the pattern. glob_pattern = self.config.layout % { 'domain': self.config.domain, 'group': '*', 'locale': '*', } # Temporarily switch to the gettext directory. This allows us # to simply call glob() using the relative pattern, rather than # having to deal with making a full path, and then later on # stripping the full path again for the regex matching, and # potentially even running into problems when, say, the pattern # contains references like ../ to a parent directory. old_dir = os.getcwd() os.chdir(self.gettext_dir) try: list = glob.glob(glob_pattern) # We now have a list of matching .po files, but now idea # which languages they represent, because we don't know # which part of the filename is the locale. To solve this, # we build a regular expression from the format string, # one with a capture group where the locale code should be. regex = re.compile(format_to_re(self.config.layout)) # We then try to match every single file returned by glob. # In this way, we can build a list of unique locale codes. languages = {} for item in list: m = regex.match(item) if not m: continue code = m.groupdict()['locale'] if code not in languages: language = resolve_locale(code, self) if language: languages[code] = language return languages.values() finally: os.chdir(old_dir)