web commit by FaidonLiambotis: Revise the locale patch
[ikiwiki.git] / doc / patchqueue / locale_patch.mdwn
1 This v2 patch is a different approach after Joey's comments and some though.
2
3 It achieves:
4
5 1. Proper local time, if the locale configuration option is used,
6 2. Support for UTF-8 (or ISO-8859-X) filenames in SVN. Before this
7 patch, commiting (or even rcs_updating) on repositories with UTF-8
8 filenames was impossible.
9
10 The svn backend sets `LC_CTYPE` to the following, in order of preference:
11  
12 * The current locale, if it contains utf8/UTF-8,
13 * The current locale with the string ".UTF-8" appended to it,
14 * `en_US.UTF-8`/`en_GB.UTF-8` -- a bit hacky, but they're _very_ common and
15   they can help avoiding a call to `locale -a`, which may not be available
16   in the current system,
17 * The first UTF-8 locale it encounters from `locale -a`. Note that `LC_CTYPE`
18   is the same for every UTF-8 locale, so it doesn't matter which one will be used.
19
20 -- [[Faidon]]
21
22 ----
23         Index: IkiWiki/Rcs/svn.pm
24         ===================================================================
25         --- IkiWiki/Rcs/svn.pm  (revision 967)
26         +++ IkiWiki/Rcs/svn.pm  (working copy)
27         @@ -4,11 +4,35 @@
28          use warnings;
29          use strict;
30          use IkiWiki;
31         +use POSIX qw(setlocale LC_CTYPE);
32          
33          package IkiWiki;
34                         
35          my $svn_webcommit=qr/^web commit (by (\w+)|from (\d+\.\d+\.\d+\.\d+)):?(.*)/;
36          
37         +sub find_lc_ctype() {
38         +       my $current = setlocale(LC_CTYPE);
39         +
40         +       # Respect current locale if it's a UTF-8 one
41         +       return $current if $current =~ m/UTF-?8$/i;
42         +
43         +       # Make some obvious attempts to avoid calling `locale -a`
44         +       foreach my $locale ("$current.UTF-8", "en_US.UTF-8", "en_GB.UTF-8") {
45         +               return $locale if setlocale(LC_CTYPE, $locale);
46         +       }
47         +
48         +       # Try to get all available locales and pick the first UTF-8 one if found
49         +       if (my @locale = grep(/UTF-?8$/i, `locale -a`)) {
50         +               chomp @locale;
51         +               return $locale[0] if setlocale(LC_CTYPE, $locale[0]);
52         +       }
53         +
54         +       # fallback to the current locale
55         +       return $current;
56         +
57         +} # }}}
58         +$ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
59         +
60          sub svn_info ($$) { #{{{
61                 my $field=shift;
62                 my $file=shift;
63         Index: IkiWiki.pm
64         ===================================================================
65         --- IkiWiki.pm  (revision 967)
66         +++ IkiWiki.pm  (working copy)
67         @@ -49,9 +49,21 @@
68                 adminemail => undef,
69                 plugin => [qw{mdwn inline htmlscrubber}],
70                 timeformat => '%c',
71         +       locale => undef,
72          } #}}}
73             
74          sub checkconfig () { #{{{
75         +       # locale stuff; avoid LC_ALL since it overrides everything
76         +       if (defined $ENV{LC_ALL}) {
77         +               $ENV{LANG} = $ENV{LC_ALL};
78         +               delete $ENV{LC_ALL};
79         +       }
80         +       if (defined $config{locale}) {
81         +               eval q{use POSIX};
82         +               $ENV{LANG} = $config{locale}
83         +                       if POSIX::setlocale(&POSIX::LANG, $config{locale});
84         +       }
85         +
86                 if ($config{w3mmode}) {
87                         eval q{use Cwd q{abs_path}};
88                         $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
89         Index: doc/ikiwiki.setup
90         ===================================================================
91         --- doc/ikiwiki.setup   (revision 967)
92         +++ doc/ikiwiki.setup   (working copy)
93         @@ -72,6 +72,9 @@
94                 #exclude => qr/\*.wav/,
95                 # Time format (for strftime)
96                 #timeformat => '%c',
97         +       # Locale to be used, useful for language customization of last-modified
98         +       # time. WARNING: Must be a UTF-8 locale!
99         +       #locale => 'en_US.UTF-8',
100                 
101                 # To add plugins, list them here.
102                 #add_plugins => [qw{meta tag pagecount brokenlinks search smiley