[Sugar-devel] [PATCH sugar] Restore setting a language with the command line OLPC #10681

Simon Schampijer simon at schampijer.de
Thu Feb 17 17:28:01 EST 2011


Move the code that handles the list given by the view
to a separate function to make it clearer.
---
 extensions/cpsection/language/model.py |   48 ++++++++++++++++---------------
 extensions/cpsection/language/view.py  |    2 +-
 2 files changed, 26 insertions(+), 24 deletions(-)

diff --git a/extensions/cpsection/language/model.py b/extensions/cpsection/language/model.py
index 48bb496..c9a4cc8 100644
--- a/extensions/cpsection/language/model.py
+++ b/extensions/cpsection/language/model.py
@@ -67,9 +67,7 @@ def _initialize():
                                            lang[1].replace(' ', '_'))
 
 
-def _write_i18n(langs):
-    colon = ':'
-    langstr = colon.join(langs)
+def _write_i18n(lang_env, language_env):
     path = os.path.join(os.environ.get('HOME'), '.i18n')
     if not os.access(path, os.W_OK):
         print _standard_msg
@@ -79,8 +77,8 @@ def _write_i18n(langs):
         fd.close()
     else:
         fd = open(path, 'w')
-        fd.write('LANG="%s"\n' % langs[0].strip("\n"))
-        fd.write('LANGUAGE="%s"\n' % langstr)
+        fd.write('LANG="%s"\n' % lang_env)
+        fd.write('LANGUAGE="%s"\n' % language_env)
         fd.close()
 
 
@@ -132,26 +130,30 @@ def print_languages():
 
 
 def set_languages(languages):
-    """Set the system language.
-    languages :
+    """Set the system language using the Country/Language format or
+    specify the utf8 code directly
     """
-    if isinstance(languages, str):
-        # This came from the commandline
-        #TODO: Support multiple languages from the command line
-        if languages.endswith('utf8'):
-            _write_i18n(languages)
-            return 1
-        else:
-            langs = read_all_languages()
-            for lang, territory, locale in langs:
-                code = lang.replace(' ', '_') + '/' \
-                        + territory.replace(' ', '_')
-                if code == languages:
-                    _write_i18n(locale)
-                    return 1
-            print (_("Sorry I do not speak \'%s\'.") % languages)
+
+    if languages.endswith('utf8'):
+        set_languages_list([languages])
+        return 1
     else:
-        _write_i18n(languages)
+        langs = read_all_languages()
+        for lang, territory, locale in langs:
+            code = lang.replace(' ', '_') + '/' \
+                + territory.replace(' ', '_')
+            if code == languages:
+                set_languages_list([locale])
+                return 1
+        print (_("Sorry I do not speak \'%s\'.") % languages)
+
+
+def set_languages_list(languages):
+    """Set the system language using a list of preferred languages"""
+    colon = ':'
+    language_env = colon.join(languages)
+    lang_env = languages[0].strip('\n')
+    _write_i18n(lang_env, language_env)
 
 
 # inilialize the docstrings for the language
diff --git a/extensions/cpsection/language/view.py b/extensions/cpsection/language/view.py
index c58b334..1553959 100644
--- a/extensions/cpsection/language/view.py
+++ b/extensions/cpsection/language/view.py
@@ -273,7 +273,7 @@ class Language(SectionView):
 
     def __lang_timeout_cb(self, codes):
         self._lang_sid = 0
-        self._model.set_languages(codes)
+        self._model.set_languages_list(codes)
         self.restart_alerts.append('lang')
         self.needs_restart = True
         self._lang_alert.props.msg = self.restart_msg
-- 
1.7.4



More information about the Sugar-devel mailing list