~ubuntu-branches/ubuntu/trusty/accountsservice/trusty

« back to all changes in this revision

Viewing changes to debian/patches/1002-language-tools.patch

  • Committer: Martin Pitt
  • Date: 2011-08-16 06:09:15 UTC
  • mfrom: (15.1.1 accountsservice-0.6.13)
  • Revision ID: martin.pitt@canonical.com-20110816060915-pukshtg2opjtq6a7
* debian/patches/1002-language-tools.patch:
  Helper scripts for managing the language environment are made available
  in /usr/share/language-tools. They are supporting accountsservice's
  "SetLanguage" method, but for now some of the files may also be
  called from other packages directly. This brings back the functionality
  that previously was in gdm and language-selector.
* debian/patches/1003-set-language.patch:
  When a new language is set, update the user's ~/.profile file with
  the corresponding environment variables.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Description: Help files for dealing with language settings.
 
2
Author: Gunnar Hjalmarsson <ubuntu@gunnar.cc>
 
3
 
 
4
--- accountsservice.orig/configure.ac   2011-07-19 21:48:12 +0200
 
5
+++ accountsservice/configure.ac        2011-08-10 15:08:50 +0200
 
6
@@ -175,6 +175,7 @@
 
7
 Makefile
 
8
 po/Makefile.in
 
9
 data/Makefile
 
10
+data/langtools/Makefile
 
11
 src/Makefile
 
12
 src/libaccountsservice/Makefile
 
13
 src/libaccountsservice/accountsservice.pc
 
14
 
 
15
--- accountsservice.orig/data/langtools/language2locale 1970-01-01 01:00:00 +0100
 
16
+++ accountsservice/data/langtools/language2locale      2011-07-25 17:48:30 +0200
 
17
@@ -0,0 +1,65 @@
 
18
+#!/bin/sh -e
 
19
+#
 
20
+# - takes the first choice language in the LANGUAGE priority list as argument
 
21
+# - outputs locale name corresponding to that language
 
22
+
 
23
+lang=$1
 
24
+locale_name=
 
25
+
 
26
+test -n "$lang" || exit 0
 
27
+
 
28
+langtoolsdir=/usr/share/language-tools
 
29
+
 
30
+langcode=${lang%%[_@]*}
 
31
+locales=
 
32
+for loc in $( locale -a | grep -F .utf8 ); do
 
33
+    # skip locales for other languages
 
34
+    if [ $langcode = ${loc%%[._@]*} ]; then
 
35
+        loc=${loc%.*}${loc#*.utf8}
 
36
+        locales="$locales $loc"
 
37
+    fi
 
38
+done
 
39
+
 
40
+# exact match
 
41
+for loc in $locales; do
 
42
+    if [ $lang = $loc ]; then
 
43
+        locale_name=$( echo $loc | sed -r 's/([^@]+)/\1.UTF-8/' )
 
44
+        break
 
45
+    fi
 
46
+done
 
47
+
 
48
+if [ -z "$locale_name" -a $lang = ${lang%_[A-Z]*} ]; then
 
49
+
 
50
+    # try the "main" country code if any
 
51
+    main_country=
 
52
+    while read line; do
 
53
+        if [ "${line%%[[:space:]]*}" = $langcode ]; then
 
54
+            main_country=${line##*[[:space:]]}
 
55
+            if [ $lang != ${lang#*@} ]; then
 
56
+                main_country=$main_country@${lang#*@}
 
57
+            fi
 
58
+            break
 
59
+        fi
 
60
+    done < $langtoolsdir/main-countries
 
61
+    if [ -n "$main_country" ]; then
 
62
+        for loc in $locales; do
 
63
+            if [ $main_country = $loc ]; then
 
64
+                locale_name=$( echo $loc | sed -r 's/([^@]+)/\1.UTF-8/' )
 
65
+                break
 
66
+            fi
 
67
+        done
 
68
+    fi
 
69
+
 
70
+    # try out fitting locale with any country code
 
71
+    if [ -z "$locale_name" -a $langcode != 'zh' ]; then
 
72
+        for loc in $locales; do
 
73
+            if [ "${loc%%[_@]*}" = $langcode ]; then
 
74
+                locale_name=$( echo $loc | sed -r 's/([^@]+)/\1.UTF-8/' )
 
75
+                break
 
76
+            fi
 
77
+        done
 
78
+    fi
 
79
+fi
 
80
+
 
81
+echo $locale_name
 
82
+
 
83
 
 
84
--- accountsservice.orig/data/langtools/language-options        1970-01-01 01:00:00 +0100
 
85
+++ accountsservice/data/langtools/language-options     2011-07-25 17:49:25 +0200
 
86
@@ -0,0 +1,66 @@
 
87
+#!/usr/bin/perl
 
88
+use strict;
 
89
+use warnings;
 
90
+
 
91
+my $langtoolsdir = '/usr/share/language-tools';
 
92
+
 
93
+# get the locales available on the system
 
94
+my @avail_locales = map { chomp; s/\.utf8//; $_ } qx( locale -a | grep -F .utf8 );
 
95
+
 
96
+# add items without country code to facilitate lookups
 
97
+my %extended_localelist;
 
98
+for my $loc (@avail_locales) {
 
99
+    ( my $lang = $loc ) =~ s/_[A-Z]+//;
 
100
+    @extended_localelist{$loc, $lang} = (1, 1);
 
101
+}
 
102
+
 
103
+# get the union of /usr/share/locale-langpack and /usr/share/locale
 
104
+my %translation_dirs;
 
105
+for my $dir ('/usr/share/locale-langpack', '/usr/share/locale') {
 
106
+    if ( opendir my ($dh), $dir ) {
 
107
+        $translation_dirs{$_} = 1 for readdir $dh;
 
108
+    }
 
109
+}
 
110
+
 
111
+# get the intersection of available translation_dirs and the extended locale list
 
112
+my %intersection;
 
113
+for ( keys %extended_localelist ) {
 
114
+    $intersection{$_} = 1 if $translation_dirs{$_};
 
115
+}
 
116
+
 
117
+# adjustments
 
118
+if ( open my $fh, '<', "$langtoolsdir/main-countries" ) {
 
119
+    # If country code items in a language exist:
 
120
+    # - Remove the item without country code, since gettext won't find a
 
121
+    #   translation under e.g. 'de_DE' if the first item in LANGUAGE is 'de'
 
122
+    #   (see https://launchpad.net/bugs/700213). 'en' is kept, though, since
 
123
+    #   it's always the last item in LANGUAGE per design.
 
124
+    # - Make sure that the main dialect of the language is represented among
 
125
+    #   the country code items (see https://launchpad.net/bugs/710148).
 
126
+    my %main;
 
127
+    while ( <$fh> ) {
 
128
+        next if /^\s*(?:#|$)/;
 
129
+        my ($k, $v) = split;
 
130
+        $main{$k} = $v;
 
131
+    }
 
132
+    my %count;
 
133
+    for ( keys %intersection ) {
 
134
+        next if /^en[^a-z]/;
 
135
+        ( my $not_country = $_ ) =~ s/_[A-Z]+//;
 
136
+        $count{$not_country} ++;
 
137
+    }
 
138
+    for my $langcode ( keys %count ) {
 
139
+        if ( $count{$langcode} > 1 ) {
 
140
+            delete $intersection{$langcode};
 
141
+            $intersection{ $main{$langcode} } = 1 if $main{$langcode};
 
142
+        }
 
143
+    }
 
144
+} else {
 
145
+    # not access to the language-to-main-dialect map
 
146
+    # => stick with a minimum of list manipulation
 
147
+    delete $intersection{'zh'};
 
148
+}
 
149
+
 
150
+# print the resulting list of language options
 
151
+print join("\n", keys %intersection) || 'en';
 
152
+
 
153
 
 
154
--- accountsservice.orig/data/langtools/language-validate       1970-01-01 01:00:00 +0100
 
155
+++ accountsservice/data/langtools/language-validate    2011-07-25 17:50:31 +0200
 
156
@@ -0,0 +1,73 @@
 
157
+#!/bin/sh -e
 
158
+
 
159
+lang=$1
 
160
+validated_language=
 
161
+
 
162
+test -n "$lang" || exit 0
 
163
+
 
164
+langtoolsdir=/usr/share/language-tools
 
165
+
 
166
+#
 
167
+# remove possible encoding part
 
168
+#
 
169
+if [ $lang != ${lang%.utf8*} ]; then
 
170
+    lang=${lang%.*}${lang#*.utf8}
 
171
+elif [ $lang != ${lang%.UTF-8*} ]; then
 
172
+    lang=${lang%.*}${lang#*.UTF-8}
 
173
+fi
 
174
+
 
175
+#
 
176
+# make sure that the output is a valid language option
 
177
+#
 
178
+options=$( $langtoolsdir/language-options )
 
179
+
 
180
+# exact match
 
181
+for opt in $options; do
 
182
+    if [ $opt = $lang ]; then
 
183
+        validated_language=$lang
 
184
+        break
 
185
+    fi
 
186
+done
 
187
+
 
188
+if [ -z "$validated_language" ]; then
 
189
+    langcode=${lang%%[_@]*}
 
190
+
 
191
+    # try the "main" country code if any
 
192
+    main_country=
 
193
+    while read line; do
 
194
+        if [ "${line%%[[:space:]]*}" = $langcode ]; then
 
195
+            main_country=${line##*[[:space:]]}
 
196
+            if [ $lang != ${lang#*@} ]; then
 
197
+                main_country=$main_country@${lang#*@}
 
198
+                is_variant=true
 
199
+            fi
 
200
+            break
 
201
+        fi
 
202
+    done < $langtoolsdir/main-countries
 
203
+    if [ -n "$main_country" ]; then
 
204
+        for opt in $options; do
 
205
+            if [ $main_country = $opt ]; then
 
206
+                validated_language=$main_country
 
207
+                break
 
208
+            fi
 
209
+        done
 
210
+    fi
 
211
+
 
212
+    # try out fitting language option without paying regard to
 
213
+    # country code or variant
 
214
+    if [ -z "$validated_language" ]; then
 
215
+        for opt in $options; do
 
216
+            if [ "${opt%%[_@]*}" = $langcode -a $langcode != 'zh' ]; then
 
217
+                validated_language=$opt
 
218
+                break
 
219
+            fi
 
220
+        done
 
221
+    fi
 
222
+fi
 
223
+
 
224
+if [ -z "$validated_language" ]; then
 
225
+    validated_language='en'
 
226
+fi
 
227
+
 
228
+echo $validated_language
 
229
+
 
230
 
 
231
--- accountsservice.orig/data/langtools/main-countries  1970-01-01 01:00:00 +0100
 
232
+++ accountsservice/data/langtools/main-countries       2011-05-17 13:19:55 +0200
 
233
@@ -0,0 +1,28 @@
 
234
+# If multiple country codes are present among the available locales for
 
235
+# a language, we may want to map the language code to the language's
 
236
+# main or origin country. The list below aims to serve that purpose.
 
237
+#
 
238
+aa     aa_ET
 
239
+ar     ar_EG
 
240
+bn     bn_BD
 
241
+ca     ca_ES
 
242
+de     de_DE
 
243
+el     el_GR
 
244
+en     en_US
 
245
+es     es_ES
 
246
+eu     eu_ES
 
247
+fr     fr_FR
 
248
+fy     fy_NL
 
249
+it     it_IT
 
250
+li     li_NL
 
251
+nl     nl_NL
 
252
+om     om_ET
 
253
+pa     pa_PK
 
254
+pt     pt_PT
 
255
+ru     ru_RU
 
256
+so     so_SO
 
257
+sr     sr_RS
 
258
+sv     sv_SE
 
259
+ti     ti_ER
 
260
+tr     tr_TR
 
261
+
 
262
 
 
263
--- accountsservice.orig/data/langtools/Makefile.am     1970-01-01 01:00:00 +0100
 
264
+++ accountsservice/data/langtools/Makefile.am  2011-08-10 15:26:00 +0200
 
265
@@ -0,0 +1,16 @@
 
266
+langtoolsdir = ${datadir}/language-tools
 
267
+
 
268
+install-data-hook:
 
269
+       if test '!' -d $(DESTDIR)$(langtoolsdir); then \
 
270
+               $(mkinstalldirs) $(DESTDIR)$(langtoolsdir); \
 
271
+               chmod 755 $(DESTDIR)$(langtoolsdir); \
 
272
+       fi
 
273
+
 
274
+       $(INSTALL_SCRIPT)  language-options     $(DESTDIR)$(langtoolsdir)/language-options
 
275
+       $(INSTALL_SCRIPT)  language-validate    $(DESTDIR)$(langtoolsdir)/language-validate
 
276
+       $(INSTALL_SCRIPT)  language2locale      $(DESTDIR)$(langtoolsdir)/language2locale
 
277
+       $(INSTALL_DATA)    main-countries       $(DESTDIR)$(langtoolsdir)/main-countries
 
278
+       $(INSTALL_SCRIPT)  save-to-profile      $(DESTDIR)$(langtoolsdir)/save-to-profile
 
279
+       $(INSTALL_SCRIPT)  set-language-helper  $(DESTDIR)$(langtoolsdir)/set-language-helper
 
280
+       $(INSTALL_SCRIPT)  update-langlist      $(DESTDIR)$(langtoolsdir)/update-langlist
 
281
+
 
282
 
 
283
--- accountsservice.orig/data/langtools/save-to-profile 1970-01-01 01:00:00 +0100
 
284
+++ accountsservice/data/langtools/save-to-profile      2011-08-11 13:53:10 +0200
 
285
@@ -0,0 +1,54 @@
 
286
+#!/bin/sh
 
287
+#
 
288
+# attempts to update the language settings in the user's ~/.profile config file
 
289
+
 
290
+user=$1
 
291
+language_list=$2
 
292
+locale_name=$3
 
293
+
 
294
+test -n "$user" || exit 0
 
295
+
 
296
+# look up the user's home directory first
 
297
+while read line; do
 
298
+    [ "${line%%:*}" = $user ] && break
 
299
+done < '/etc/passwd'
 
300
+if [ -n "$line" ]; then
 
301
+    OLDIFS=$IFS
 
302
+    IFS=':'
 
303
+    i=1
 
304
+    for homedir in $line; do
 
305
+        if [ $i -ne 6 ]; then
 
306
+            i=$(( $i + 1 ))
 
307
+            continue
 
308
+        fi
 
309
+        break
 
310
+    done
 
311
+    IFS=$OLDIFS
 
312
+fi
 
313
+if [ "$( echo $homedir )" = '' -o ! -d "$homedir" ]; then
 
314
+    exit 0
 
315
+fi
 
316
+
 
317
+# create ~/.profile if it doesn't exist
 
318
+test -f "$homedir/.profile" || {
 
319
+    touch $homedir/.profile
 
320
+    chown $user:$user $homedir/.profile
 
321
+}
 
322
+
 
323
+save_to_profile() {
 
324
+    var=$1; value=$2
 
325
+    if [ "$( grep "^[[:space:]]*export $var=" $homedir/.profile )" ]; then
 
326
+        sed -r -i "s/^[[:space:]]*(export $var=).*/\1\"$value\"/" $homedir/.profile
 
327
+    else
 
328
+        echo "export $var=\"$value\"" >> $homedir/.profile
 
329
+    fi
 
330
+}
 
331
+test -n "$language_list" && save_to_profile 'LANGUAGE' $language_list
 
332
+test -n "$locale_name" && {
 
333
+    save_to_profile 'LC_MESSAGES' $locale_name
 
334
+    save_to_profile 'LC_CTYPE'    $locale_name
 
335
+    save_to_profile 'LC_COLLATE'  $locale_name
 
336
+}
 
337
+
 
338
+exit 0
 
339
+
 
340
 
 
341
--- accountsservice.orig/data/langtools/set-language-helper     1970-01-01 01:00:00 +0100
 
342
+++ accountsservice/data/langtools/set-language-helper  2011-08-07 13:19:08 +0200
 
343
@@ -0,0 +1,25 @@
 
344
+#!/bin/sh -e
 
345
+
 
346
+user=$1
 
347
+language=$2
 
348
+validated_language=
 
349
+
 
350
+[ -n "$user" -a -n "$language" ] || exit 0
 
351
+
 
352
+langtoolsdir=/usr/share/language-tools
 
353
+
 
354
+if [ $language = "${language%:*}" ]; then
 
355
+    validated_language=$( $langtoolsdir/language-validate $language )
 
356
+    renewed_langlist=$( $langtoolsdir/update-langlist $validated_language $user )
 
357
+else
 
358
+    # $language contains a priority list
 
359
+    validated_language=$( $langtoolsdir/language-validate ${language%%:*} )
 
360
+    renewed_langlist=$validated_language:${language#*:}
 
361
+fi
 
362
+
 
363
+locale_name=$( $langtoolsdir/language2locale $validated_language )
 
364
+
 
365
+( $langtoolsdir/save-to-profile $user $renewed_langlist $locale_name )
 
366
+
 
367
+echo $validated_language
 
368
+
 
369
 
 
370
--- accountsservice.orig/data/langtools/update-langlist 1970-01-01 01:00:00 +0100
 
371
+++ accountsservice/data/langtools/update-langlist      2011-08-11 13:53:29 +0200
 
372
@@ -0,0 +1,61 @@
 
373
+#!/bin/sh -e
 
374
+#
 
375
+# update-langlist maintains the LANGUAGE priority list. It does so in
 
376
+# a simplified manner, unlike the principal UI for setting the user
 
377
+# language which provides full control.
 
378
+
 
379
+first_language=$1
 
380
+user=$2
 
381
+renewed_langlist=
 
382
+
 
383
+[ -n "$first_language" -a -n "$user" ] || exit 0
 
384
+
 
385
+get_old_langlist() {
 
386
+    while read line; do
 
387
+        [ "${line%%:*}" = $user ] && break
 
388
+    done < /etc/passwd
 
389
+    if [ -n "$line" ]; then
 
390
+        OLDIFS=$IFS
 
391
+        IFS=':'
 
392
+        i=1
 
393
+        for homedir in $line; do
 
394
+            if [ $i -ne 6 ]; then
 
395
+                i=$(( $i + 1 ))
 
396
+                continue
 
397
+            fi
 
398
+            break
 
399
+        done
 
400
+        IFS=$OLDIFS
 
401
+    fi
 
402
+
 
403
+    if [ -r "$homedir/.profile" ]; then
 
404
+        old_langlist=$( grep "^[[:space:]]*export LANGUAGE=" \
 
405
+                        $homedir/.profile | cut -d '"' -f 2 )
 
406
+    fi
 
407
+}
 
408
+
 
409
+old_langlist=
 
410
+english=true
 
411
+if [ ${first_language%%[_@]*} != 'en' ]; then
 
412
+    get_old_langlist
 
413
+    english=false
 
414
+fi
 
415
+
 
416
+if [ -n "$old_langlist" ] && ! $english; then
 
417
+    if expr $old_langlist : ".*:$first_language:" > /dev/null ; then
 
418
+        renewed_langlist=$( echo $old_langlist | sed -r "s/(.+:)($first_language:)/\2\1/" )
 
419
+    elif [ ${old_langlist%%:*} = $first_language ]; then
 
420
+        # no change
 
421
+        renewed_langlist=$old_langlist
 
422
+    else
 
423
+        renewed_langlist=$first_language:$old_langlist
 
424
+    fi
 
425
+else
 
426
+    # build fresh list with 'en' as the last element
 
427
+    # no other languages needed if English was selected
 
428
+    renewed_langlist=$first_language
 
429
+    [ $first_language != 'en' ] && renewed_langlist=$renewed_langlist:en
 
430
+fi
 
431
+
 
432
+echo $renewed_langlist
 
433
+
 
434
 
 
435
--- accountsservice.orig/data/Makefile.am       2011-07-19 21:48:12 +0200
 
436
+++ accountsservice/data/Makefile.am    2011-08-10 15:10:27 +0200
 
437
@@ -1,3 +1,4 @@
 
438
+SUBDIRS = langtools
 
439
 
 
440
 dbusifdir   = $(datadir)/dbus-1/interfaces
 
441
 dbusif_DATA = \
 
442