#!/usr/bin/env python3 import os from codecs import open import argparse from configparser import ConfigParser class FixLanguage: def __init__(self,home): self.config = ConfigParser() self.config.optionxform = str self.path_language='{home}/.config/plasma-localerc'.format(home=home) self.language_to_fix = ['es','ca@valencia'] def parse_file(self): if os.path.exists(self.path_language): with open(self.path_language, 'r', encoding='utf-8') as fd: self.config.read_file(fd) if 'Translations' not in self.config: self.config.add_section('Translations') if 'Formats' not in self.config: self.config.add_section('Formats') if 'LANGUAGE' in self.config['Translations'] and self.config["Translations"]["LANGUAGE"] != "": self.languages = self.config['Translations']['LANGUAGE'].split(':') return else: if 'Formats' in self.config and 'LANG' in self.config['Formats']: self.languages = self.get_language_from_lang(self.config['Formats']['LANG']) return else: self.languages = self.get_lang_from_locale_file() return else: self.config.add_section("Translations") self.config.add_section("Formats") self.languages = self.get_lang_from_locale_file() return self.languages = list() def get_language_from_lang(self, lang): if lang.startswith("es"): return ["es"] if lang.find("@valencia") > 0: return ["ca@valencia"] return [] def get_lang_from_locale_file(self): if os.path.exists("/etc/default/locale"): fd = open('/etc/default/locale',"r") options = list( map( lambda x : x.strip(), fd.readlines())) for line_option in options: if line_option.startswith("LANG="): value = line_option[len("LANG="):] return self.get_language_from_lang(value) return [] def generate_lang_and_language(self): if self.languages[0] == 'es': self.lang = 'es_ES.UTF-8' elif self.languages[0] == 'ca@valencia': self.lang = 'ca_ES.utf8@valencia' def is_need_fix(self): return self.languages[0] in self.language_to_fix def write_file(self): self.config['Translations']['LANGUAGE'] = ':'.join(self.languages) self.config['Formats']['LANG'] = self.lang self.config['Formats']['LC_CTYPE'] = self.lang.split("@")[0] with open(self.path_language, 'w', encoding='utf-8') as fd: self.config.write(fd) def run(self): self.parse_file() if len(self.languages) > 0 and self.is_need_fix(): self.generate_lang_and_language() self.write_file() if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('-f') arguments = vars(parser.parse_args()) if arguments['f'] != None: app = FixLanguage(arguments['f']) else: app = FixLanguage(os.environ['HOME']) app.run()