[Tails-dev] Fixed default country and applying of pt_BR tran…

Borrar esta mensaxe

Responder a esta mensaxe
Autor: winterfairy
Data:  
Para: tails-dev
Asunto: [Tails-dev] Fixed default country and applying of pt_BR translation in tails-greeter
Here is seven patches against the master branch in tails-greeter.

0001-Language-and-syntax-fixes.patch
0002-Added-new-file-default_langcodes.patch
0003-Install-default_langcodes-and-read-it-in-first.patch
0004-Sort-the-default_langcodes-file-and-really-only-list.patch
0005-Remove-old-incorrect-way-of-inferring-default-countr.patch
0006-Take-country-code-into-consideration-when-translatin.patch
0007-Prefer-keyboard-layout-for-selected-country.patch

The current behaviour of Tails Greeter:

- Select Swedish, and Finland is chosen as country.
- Select Portuguese, and Portugal is chosen as country.
- Select Portuguese/Brazil, and the translation is not applied.

The new fixed behaviour:

- Select Swedish, and Sweden is chosen as country.
- Select Portuguese, and Brazil is chosen as country.
- The translation for Portuguese/Brazil is applied correctly.

I am a bit uncertain about the change to Brazil as default country
for Portuguese, but we only have a pt_BR translation, and Wikipedia
supports my intuition that Portuguese is mostly spoken in Brazil.

Some more languages had their default country changed.
The list used is taken from the Ubuntu installer, so the
default choices should be sensible.

It would also make it easier for me to continue contribution
small bugfixes if I either get push access to the greeter and
tails repository, or get my own clones of those repositories with
push access. That way I don't have to send patches, but can request
review and merge of branches instead. Possible?>From 4a9824cb2150071bd2dc9b3ecddee044805c6447 Mon Sep 17 00:00:00 2001
From: WinterFairy <winterfairy@???>
Date: Sat, 2 Nov 2013 14:11:12 +0000
Subject: [PATCH 1/7] Language and syntax fixes

---
 GdmGreeter/language.py |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)


diff --git a/GdmGreeter/language.py b/GdmGreeter/language.py
index d489f13..eda5d3d 100644
--- a/GdmGreeter/language.py
+++ b/GdmGreeter/language.py
@@ -18,7 +18,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
#
-"""Localistaion handeling
+"""Localization handling

"""

@@ -65,7 +65,7 @@ def get_native_langs(lang_list):
     langs_dict = {}
     for l in lang_list:
         # English = Locale(en_GB)...
-        lang =  Locale(l).getDisplayLanguage(Locale(l)).title()
+        lang = Locale(l).getDisplayLanguage(Locale(l)).title()
         try:
             langs_dict[lang]
         except: #XXX specify exception
@@ -513,8 +513,8 @@ class LocalisationSettings(object):
 # List of system locale codes
 langcodes = __get_langcodes()


-# dictionnary of native language: language code
+# dictionary of native languages: language code
_languages_dict = get_native_langs(langcodes)

-# dictionnary of layout codes: layout name
+# dictionary of layout codes: layout name
_system_layouts_dict = __fill_layouts_dict()
--
1.7.2.5
>From 24c339ab97ff57bc2ffba138178ef2b5f3ca73be Mon Sep 17 00:00:00 2001
From: WinterFairy <winterfairy@???>
Date: Sat, 2 Nov 2013 15:21:44 +0000
Subject: [PATCH 2/7] Added new file "default_langcodes"

This file lists the default language country pairs,
e.g. en_US or sv_SE. This is needed because the latter
is currently incorrectly set to sv_FI when sv is selected,
for example.

The list was derived from a similar file in Ubiquity,
which in turn appears to have derived it from
Debian Installer's localechooser.
---
default_langcodes | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 74 insertions(+), 0 deletions(-)
create mode 100644 default_langcodes

diff --git a/default_langcodes b/default_langcodes
new file mode 100644
index 0000000..a9f3361
--- /dev/null
+++ b/default_langcodes
@@ -0,0 +1,74 @@
+sq_AL
+am_ET
+ar_EG
+ast_ES
+eu_ES
+be_BY
+bn_BD
+bs_BA
+br_FR
+bg_BG
+bo_IN
+ca_ES
+zh_CN
+zh_TW
+hr_HR
+cs_CZ
+da_DK
+nl_NL
+dz_BT
+en_US
+et_EE
+fi_FI
+fr_FR
+gl_ES
+ka_GE
+de_DE
+el_GR
+gu_IN
+he_IL
+hi_IN
+hu_HU
+is_IS
+id_ID
+ga_IE
+it_IT
+ja_JP
+kk_KZ
+km_KH
+kn_IN
+ko_KR
+ku_TR
+lo_LA
+lv_LV
+lt_LT
+ml_IN
+mr_IN
+mk_MK
+my_MM
+ne_NP
+se_NO
+nb_NO
+nn_NO
+fa_IR
+pl_PL
+pt_PT
+pt_BR
+pa_IN
+ro_RO
+ru_RU
+si_LK
+sr_RS
+sk_SK
+sl_SI
+es_ES
+sv_SE
+tl_PH
+ta_IN
+te_IN
+th_TH
+tr_TR
+ug_CN
+uk_UA
+vi_VN
+cy_GB
--
1.7.2.5
>From 6257fd77c64e965eefd060570e17ceea8e197863 Mon Sep 17 00:00:00 2001
From: WinterFairy <winterfairy@???>
Date: Sat, 2 Nov 2013 16:12:49 +0000
Subject: [PATCH 3/7] Install default_langcodes, and make use of it.

By reading it in before the complete language_codes file,
those default will end up first in the list over language codes,
and have precedence when selecting language.
---
 GdmGreeter/config.py   |    3 +++
 GdmGreeter/language.py |    4 +++-
 debian/rules           |    2 ++
 3 files changed, 8 insertions(+), 1 deletions(-)


diff --git a/GdmGreeter/config.py b/GdmGreeter/config.py
index da90d21..a5e725f 100644
--- a/GdmGreeter/config.py
+++ b/GdmGreeter/config.py
@@ -26,6 +26,9 @@ LUSER = 'amnesia'
# file where supported language codes (such as en_US) are saved at Tails build time
language_codes_path = '/usr/share/tails-greeter/language_codes'

+# file where default language code for languages are saved at Tails build time
+default_langcodes_path = '/usr/share/tails-greeter/default_langcodes'
+
# locales path
locales_path = '/usr/share/locale/'

diff --git a/GdmGreeter/language.py b/GdmGreeter/language.py
index eda5d3d..2d1d221 100644
--- a/GdmGreeter/language.py
+++ b/GdmGreeter/language.py
@@ -205,10 +205,12 @@ def layouts_with_names(layouts, locale='C'):
     return layouts_with_names


 def __get_langcodes():
+    with open(GdmGreeter.config.default_langcodes_path, 'r') as f:
+        defcodes = [ line.rstrip('\n') for line in f.readlines() ]
     with open(GdmGreeter.config.language_codes_path, 'r') as f:
         langcodes = [ line.rstrip('\n') for line in f.readlines() ]
     logging.debug('%s languages found', len(langcodes))
-    return langcodes
+    return defcodes + langcodes


 class TranslatableWindow(object):
     """Interface providing functions to translate a window on the fly
diff --git a/debian/rules b/debian/rules
index d9b9bbe..6c6d77d 100755
--- a/debian/rules
+++ b/debian/rules
@@ -10,6 +10,8 @@ override_dh_auto_install:
     dh_auto_install
     install -m 0755 $(CURDIR)/PostLogin.default $(DESTDIR)/etc/gdm3/PostLogin/Default
     install -m 0440 $(CURDIR)/sudoers.d/* $(DESTDIR)/etc/sudoers.d/
+    install -m 0644 $(CURDIR)/default_langcodes \
+        $(DESTDIR)/usr/share/tails-greeter/default_langcodes
     perl -n -E 'next unless m{_}xms;        \
                 next if m{\@}xms;           \
                 say $$1 if m{(.*?) [. ]}xms' \
-- 
1.7.2.5

>From d5c8d7b8877858da32162f765b6961f745b76dc0 Mon Sep 17 00:00:00 2001
From: WinterFairy <winterfairy@???>
Date: Sat, 2 Nov 2013 16:33:20 +0000
Subject: [PATCH 4/7] Sort the default_langcodes file, and really only list one country per language.

I kept China for Chinese, and Brazil for Portuguese.
Only the latter may be controversial, but Wikipedia supports
my intuition that Portuguese is mostly spoken in Brazil.

---
default_langcodes | 48 +++++++++++++++++++++++-------------------------
1 files changed, 23 insertions(+), 25 deletions(-)

diff --git a/default_langcodes b/default_langcodes
index a9f3361..60d9703 100644
--- a/default_langcodes
+++ b/default_langcodes
@@ -1,74 +1,72 @@
-sq_AL
am_ET
ar_EG
ast_ES
-eu_ES
be_BY
-bn_BD
-bs_BA
-br_FR
bg_BG
+bn_BD
bo_IN
+br_FR
+bs_BA
ca_ES
-zh_CN
-zh_TW
-hr_HR
cs_CZ
+cy_GB
da_DK
-nl_NL
+de_DE
dz_BT
+el_GR
en_US
+es_ES
et_EE
+eu_ES
+fa_IR
fi_FI
fr_FR
+ga_IE
gl_ES
-ka_GE
-de_DE
-el_GR
gu_IN
he_IL
hi_IN
+hr_HR
hu_HU
-is_IS
id_ID
-ga_IE
+is_IS
it_IT
ja_JP
+ka_GE
kk_KZ
km_KH
kn_IN
ko_KR
ku_TR
lo_LA
-lv_LV
lt_LT
+lv_LV
+mk_MK
ml_IN
mr_IN
-mk_MK
my_MM
-ne_NP
-se_NO
nb_NO
+ne_NP
+nl_NL
nn_NO
-fa_IR
+pa_IN
pl_PL
-pt_PT
pt_BR
-pa_IN
ro_RO
ru_RU
+se_NO
si_LK
-sr_RS
sk_SK
sl_SI
-es_ES
+sq_AL
+sr_RS
sv_SE
-tl_PH
ta_IN
te_IN
th_TH
+tl_PH
tr_TR
ug_CN
uk_UA
vi_VN
-cy_GB
+zh_CN
--
1.7.2.5
>From 75ecf17a2cfa850be35798864971ffdee857db54 Mon Sep 17 00:00:00 2001
From: WinterFairy <winterfairy@???>
Date: Sat, 2 Nov 2013 17:20:38 +0000
Subject: [PATCH 5/7] Remove old incorrect way of inferring default country.

The old way assumed the country's two letters equaled
the language's two letters. For many language/country
pairs this is incorrect, and the previous commits would
have fixed it, but this code was still interferring.
---
GdmGreeter/language.py | 10 ++--------
1 files changed, 2 insertions(+), 8 deletions(-)

diff --git a/GdmGreeter/language.py b/GdmGreeter/language.py
index 2d1d221..5605017 100644
--- a/GdmGreeter/language.py
+++ b/GdmGreeter/language.py
@@ -381,14 +381,8 @@ class LocalisationSettings(object):
         default_locale = None
         default_locales = self.get_default_locales()
         logging.debug("default_locales = %s" % default_locales)
-        if default_locales: 
-            for locale in default_locales:
-                if ((locale == 'en_US')
-                    or (language_from_locale(locale).lower() ==
-                        country_from_locale(locale).lower())):
-                    default_locale = locale
-            if not default_locale:
-                default_locale = default_locales[0]
+        if default_locales:
+            default_locale = default_locales[0]
         else:
             default_locale = 'en_US'
         logging.debug("setting default locale to %s" % default_locale)
-- 
1.7.2.5

>From ab1579330d41157363ae820426dc87df29a2b8b1 Mon Sep 17 00:00:00 2001
From: WinterFairy <winterfairy@???>
Date: Sat, 2 Nov 2013 18:03:04 +0000
Subject: [PATCH 6/7] Take country code into consideration when translating the window.

The translation for pt_BR was not applied because the greeter was
looking for a translation pt, which didn't exist.
Now the translations is applied in this case, and the correct
translation is applied if multiple translation for the same
language but different regions exists.
---
GdmGreeter/language.py | 26 +++++---------------------
1 files changed, 5 insertions(+), 21 deletions(-)

diff --git a/GdmGreeter/language.py b/GdmGreeter/language.py
index 5605017..06e59ca 100644
--- a/GdmGreeter/language.py
+++ b/GdmGreeter/language.py
@@ -74,17 +74,6 @@ def get_native_langs(lang_list):
             langs_dict[lang].append(l)
     return langs_dict


-def get_texts(langs):
-    """obtain texts for a given locale using gettext"""
-    result = {}
-    for k, l in langs.iteritems():
-        loc = l[0].split('_')[0]
-        try:
-            result[str(loc)] = gettext.translation(GdmGreeter.__appname__, GdmGreeter.config.locales_path, [str(loc)])
-        except IOError:
-            logging.debug('Failed to get texts for %s locale', loc)
-    return result
-
 def __fill_layouts_dict():
     """assemble dictionary of layout codes to corresponding layout name


@@ -222,7 +211,6 @@ class TranslatableWindow(object):
         self.labels = []
         self.tips = []
         self.store_translations(self.window)
-        self.__texts = get_texts(_languages_dict)


     def store_translations(self, widget):
         """Go through all widgets and store the translatable elements"""
@@ -234,14 +222,6 @@ class TranslatableWindow(object):
             if child.get_has_tooltip():
                 self.tips.append( (child, child.get_tooltip_text()) )


-    def language(self, lang):
-        """Return normalised language for use in this process"""
-        if '_' in lang:
-            lang = lang.split('_')[0]
-        if '.' in lang:
-            lang = lang.split('.')[0]
-        return lang.lower()
-
     def gettext(self, lang, text):
         """Return a translated string or string"""
         if lang:
@@ -250,7 +230,11 @@ class TranslatableWindow(object):


     def translate_to(self, lang):
         """Loop through everything and translate on the fly"""
-        lang = self.__texts.get(self.language(lang), None)
+        try:
+            lang = gettext.translation(GdmGreeter.__appname__, GdmGreeter.config.locales_path, [str(lang)])
+        except IOError:
+            lang = None
+
         for (child, text) in self.labels:
             child.set_label(self.gettext(lang, text))
         for (child, text) in self.tips:
-- 
1.7.2.5

>From 84fef13c7a2812b29c5fe6af8068ff7360f573d1 Mon Sep 17 00:00:00 2001
From: WinterFairy <winterfairy@???>
Date: Sat, 2 Nov 2013 18:50:39 +0000
Subject: [PATCH 7/7] Prefer keyboard layout for selected country.

Here we still use the rather ugly hack of matching
country or language code with keyboard layout codes.
But country is better than language, as the previous
commits now make better guesses of countries.
At least pt_BR now gets Brazil keyboard layout.
---
 GdmGreeter/language.py |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)


diff --git a/GdmGreeter/language.py b/GdmGreeter/language.py
index 06e59ca..d674d54 100644
--- a/GdmGreeter/language.py
+++ b/GdmGreeter/language.py
@@ -449,9 +449,9 @@ class LocalisationSettings(object):
                 (layout_code,
                  language_from_locale(self._locale).lower(),
                  country_from_locale(self._locale).lower()))
-            if language_from_locale(self._locale).lower() == layout_code:
+            if country_from_locale(self._locale).lower() == layout_code:
                 default_layout = layout_code
-            elif country_from_locale(self._locale).lower() == layout_code:
+            elif language_from_locale(self._locale).lower() == layout_code:
                 backup_layout = layout_code
         if not default_layout:
             if backup_layout:
-- 
1.7.2.5