File: intltool-unicodify.in

package info (click to toggle)
intltool 0.35.0-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 936 kB
  • ctags: 67
  • sloc: sh: 654; xml: 298; perl: 165; makefile: 150; ansic: 88; lisp: 7
file content (172 lines) | stat: -rwxr-xr-x 4,334 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!@INTLTOOL_PERL@ -w
        
#
#  The i18n Unicode Encoding Utility
#
#  Copyright (C) 2001 Free Software Foundation.
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This script is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this library; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#  Authors:  Kenneth Christiansen <kenneth@gnu.org>
#

## Release information
my $PROGRAM = "intltool-unicodify";
my $VERSION = "@VERSION@";
my $PACKAGE = "@PACKAGE@";

## Loaded modules
use strict;
use Getopt::Long;
use Cwd;
use File::Copy;

## Scalars used by the option stuff
my $LANG     	   = $ARGV[0];
my $HELP_ARG 	   = 0;
my $VERSION_ARG    = 0;
my $OVERWRITE_ARG  = 0;
my $VERBOSE        = 0;

## Always print as the first thing
$| = 1;

## Handle options
GetOptions 
(
 "help" 	       => \$HELP_ARG,
 "version" 	       => \$VERSION_ARG,
 "verbose|v"           => \$VERBOSE,
 "overwrite|o"         => \$OVERWRITE_ARG,
 ) or &print_error_invalid_option;

sub split_on_argument
{
    if ($VERSION_ARG) {
	&print_version;
    }
    elsif ($HELP_ARG) {
	&print_help;
    }

    ## Give error if script is run without an argument
    if (! $LANG){
	print "${PROGRAM}:  missing file arguments\n";
	print "Try `${PROGRAM} --help' for more information.\n";
	exit;
    }
}

&split_on_argument;
&main;

sub print_version
{
    ## Print version information
    print "${PROGRAM} (${PACKAGE}) $VERSION\n";
    print "Written by Kenneth Christiansen\n\n";
    print "Copyright (C) 2001-2002 Free Software Foundation, Inc.\n";
    print "This is free software; see the source for copying conditions.  There is NO\n";
    print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
    exit;
}

sub print_help
{
    ## Print usage information
    print "Usage: ${PROGRAM} [OPTIONS] ...LANGCODE\n";
    print "Convert PO files containing translations to UTF8.\n\n";
    print "  -o, --owerwrite        overwrite original file\n";
    print "  -x, --verbose          display lots of feedback\n";
    print "      --help             display this help and exit\n";
    print "      --version          output version information and exit\n";
    print "\nExamples of use:\n";
    print "${PROGRAM} da    ports da.po to UTF8 and saves as da.po-1\n\n";
    print "Report bugs to bugzilla.gnome.org, module 'intltool'.\n";
    exit;
}

sub get_local_charset
{
    my ($encoding) = @_;
    my $alias_file = $ENV{"G_CHARSET_ALIAS"} || "@INTLTOOL_LIBDIR@/charset.alias";

    # seek character encoding aliases in charset.alias (glib)

    if (open CHARSET_ALIAS, $alias_file)
    {
	while (<CHARSET_ALIAS>) 
        {
            next if /^\#/;
            return $1 if (/^\s*([-._a-zA-Z0-9]+)\s+$encoding\b/i)
        }

        close CHARSET_ALIAS;
    }

    # if not found, return input string

    return $encoding;
}

sub main
{
    my $encoding_code_orig;

    open IN, "$LANG.po";
    
    while (<IN>) 
    {
	## example: "Content-Type: text/plain; charset=ISO-8859-1\n"
	if (/Content-Type\:.*charset=(.*)\\n/) 
        {
	    $encoding_code_orig = $1; 
	    last;
	}
    }
    
    close IN;
    
    $encoding_code_orig = get_local_charset($encoding_code_orig);

    print "Converting from $encoding_code_orig\n" if $VERBOSE;
    
    my $extern_conv="iconv -f $encoding_code_orig "
	."-t UTF-8 $LANG.po > $LANG.po-1";
    
    system ($extern_conv);
    
    my $source_orig;
    {
	local (*IN);
	local $/; #slurp mode
	open (IN, "<$LANG.po-1") || die "can't open $LANG.po-1: $!";
	$source_orig = <IN>;
    }
    
    $source_orig =~ s/Content-Type\:(.*)$encoding_code_orig/Content-Type\:$1UTF-8/;
    
    close IN;
    open OUT, ">$LANG.po-1";

    print OUT $source_orig;
    close OUT;

    if ($OVERWRITE_ARG) 
    {
	copy ("$LANG.po-1", "$LANG.po");
	system ("rm -rf $LANG.po-1");
    }
}