Changeset View
Changeset View
Standalone View
Standalone View
packaging/linux/snap/kf5-locale-gen
- This file was added.
Property | Old Value | New Value |
---|---|---|
File Mode | null | 100755 |
1 | #!/usr/bin/perl | ||||
---|---|---|---|---|---|
2 | # | ||||
3 | # Copyright 2019 Harald Sitter <sitter@kde.org> | ||||
4 | # | ||||
5 | # This program is free software; you can redistribute it and/or | ||||
6 | # modify it under the terms of the GNU General Public License as | ||||
7 | # published by the Free Software Foundation; either version 3 of | ||||
8 | # the License or any later version accepted by the membership of | ||||
9 | # KDE e.V. (or its successor approved by the membership of KDE | ||||
10 | # e.V.), which shall act as a proxy defined in Section 14 of | ||||
11 | # version 3 of the license. | ||||
12 | # | ||||
13 | # This program is distributed in the hope that it will be useful, | ||||
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
16 | # GNU General Public License for more details. | ||||
17 | # | ||||
18 | # You should have received a copy of the GNU General Public License | ||||
19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
20 | | ||||
21 | use strict; | ||||
22 | | ||||
23 | my $debug = 0; | ||||
24 | if (defined $ENV{'LOCALE_DEBUG'}) { | ||||
25 | $debug = 1; | ||||
26 | } | ||||
27 | | ||||
28 | my $config_dir = ($ENV{'XDG_CONFIG_HOME'} or "$ENV{'HOME'}/.config"); | ||||
29 | my $conf="$config_dir/klanguageoverridesrc"; | ||||
30 | | ||||
31 | my @languages = ('C.UTF-8'); | ||||
32 | | ||||
33 | sub debug { | ||||
34 | print(@_) if $debug; | ||||
35 | } | ||||
36 | | ||||
37 | # Grab langs from languageoverrides file (in-app language switch feature). | ||||
38 | if (-e $conf) { | ||||
39 | my $group; | ||||
40 | open my $ini, '<', $conf or die "Can't open $conf: $!\n"; | ||||
41 | while (<$ini>) { # loops in $_ | ||||
42 | chomp; | ||||
43 | my $keyword; | ||||
44 | my $value; | ||||
45 | if (/^\s*\[(.+)\].*/) { | ||||
46 | $group = $1; | ||||
47 | } elsif (/^([^=]+?)\s*=\s*(.*?)\s*$/) { # https://code-maven.com/slides/perl-programming/solution-parse-ini-file | ||||
48 | if ($group ne 'Language') { | ||||
49 | next; | ||||
50 | } | ||||
51 | $keyword = $1; | ||||
52 | $value = $2; | ||||
53 | my $pattern = '@ByteArray\((.+)\)'; | ||||
54 | (my $langs = $value) =~ s/$pattern/$1/g; | ||||
55 | push @languages, split(/:/, $langs); | ||||
56 | # print("-- keyword: $1 -- value: $2\n"); | ||||
57 | } | ||||
58 | } | ||||
59 | close($ini); | ||||
60 | } | ||||
61 | | ||||
62 | # Grab values from ENV. | ||||
63 | push @languages, ($ENV{'LANG'} or 'C.UTF-8'); | ||||
64 | foreach my $key (sort keys %ENV) { | ||||
65 | my $needle = 'LC_'; | ||||
66 | if (substr($key, 0, length($needle)) eq $needle) { | ||||
67 | push @languages, $ENV{$key}; | ||||
68 | } | ||||
69 | } | ||||
70 | if (my $language = $ENV{'LANGUAGE'}) { | ||||
71 | push @languages, split(':', $language); | ||||
72 | } | ||||
73 | push @languages, ($ENV{'LANG'} or 'C.UTF-8'); | ||||
74 | | ||||
75 | # Uniqify; can't really use any perl modules as the base requirements of KF5 are | ||||
76 | # mostly perl core only. | ||||
77 | @languages = do { my %seen; grep { !$seen{$_}++ } @languages }; | ||||
78 | | ||||
79 | # Generate as necessary. | ||||
80 | # Note: this actually generates invalid stuff as well (e.g. fr.UTF-8) while we | ||||
81 | # apparently do not need these there isn't much of a downside to generate them | ||||
82 | # lest something in glibc falls over if the language is entirely unknown in | ||||
83 | # the locpath. I should point out that we can't actually get from | ||||
84 | # language-only settings (as seen in klanguageoverrides) to full locales, so | ||||
85 | # there is no other option here anyway. | ||||
86 | my $env_locpath = $ENV{'LOCPATH'} or die "LOCPATH not set"; | ||||
87 | my @locpaths = split(/:/, $env_locpath); | ||||
88 | my $master_locpath = $locpaths[0]; | ||||
89 | foreach my $lang (@languages) { | ||||
90 | my ($locale, $encoding) = split(/\./, $lang); | ||||
91 | $encoding //= 'UTF-8'; | ||||
92 | debug("lang: $lang loc: $locale, char: $encoding\n"); | ||||
93 | | ||||
94 | my $found = 0; | ||||
95 | foreach my $locpath (@locpaths) { | ||||
96 | my $loc_target = "$locpath/$locale.$encoding"; | ||||
97 | debug("checking $loc_target\n"); | ||||
98 | if (-e $loc_target) { | ||||
99 | debug("exists! skipping\n"); | ||||
100 | $found = 1; | ||||
101 | last; | ||||
102 | } | ||||
103 | } | ||||
104 | next if $found; | ||||
105 | my $target = "$master_locpath/$locale.$encoding"; | ||||
106 | | ||||
107 | debug("generating $target\n"); | ||||
108 | # localedef will exit !0 for unknown reasons, even when everything was | ||||
109 | # generated fine. | ||||
110 | # may be: /snap/krita/32/usr/share/i18n/locales/iso14651_t1_common:7120: LC_COLLATE: symbol `pure-ta-zh' not known | ||||
111 | system('localedef', '-i', $locale, '-f', $encoding, $target); | ||||
112 | } | ||||
113 | | ||||
114 | # use Data::Dumper; | ||||
115 | # print Dumper(@languages); |