summaryrefslogtreecommitdiff
path: root/build/TranslatorExtractor.cs
blob: 4e4ffa035c4568422f6b9d208a74bb778599ebad (plain)
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
//
// Licensed under same license as Banshee
// Copyright (C) 2006 Novell, Inc.
// Written by Aaron Bockover <abock@gnome.org>
//
// This tool extracts translator information from .po files
// to generate information for display in the about dialog
//

using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class TranslatorExtractor
{
    public static void Main(string [] args)
    {
        Console.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
        Console.WriteLine("<!-- AUTOGENERATED - DO NOT EDIT -->");
        Console.WriteLine("<translators>");
        foreach(string filename in Directory.GetFiles(args.Length == 0 ? "." : args[0], "*.po")) {
            using(FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) {
                using(StreamReader reader = new StreamReader(stream)) {
                    ParseFile(filename, reader);
                }
            }
        }
        Console.WriteLine("</translators>");
    }

    private static void ParseFile(string file, TextReader reader)
    {
        Dictionary<string, string> names = new Dictionary<string, string>();
        string language_code = Path.GetFileNameWithoutExtension(file);
        string language_name = null;
    
        bool past_plural_forms = false;
    
        while(true) {
            string line = reader.ReadLine();
            if(line == null || (line.StartsWith("#:") && past_plural_forms)) {
                break;
            } else if(line.StartsWith("\"Plural-Forms:")) {
                past_plural_forms = true;
            }
            
            try {
                Match match = Regex.Match(line, ".*#(.*)<(.*)>(.*,.*|[ \n]*$)");
                if(!match.Success) {
                    match = Regex.Match(line, ".*\\\"Last-Translator:(.*)<(.*)>.*");
                    if(!match.Success) {
                        match = Regex.Match(line, ".*\\\"Language-Team:(.*)<.*>.*");
                        if(!match.Success) {
                            continue;
                        }

                        language_name = match.Groups[1].Value.Trim();
                        int pos = language_name.IndexOf('(');
                        if(pos > 0) {
                            language_name = language_name.Substring(0, pos).Trim();
                        }
                        
                        pos = language_name.IndexOf("GNOME");
                        if(pos > 0) {
                            language_name = language_name.Substring(0, pos).Trim();
                        }
                    }
                }
                
                string name = match.Groups[1].Value.Trim();
                string email = match.Groups[2].Value.Trim();
            
                if(name == String.Empty || email == String.Empty || name == "FIRST AUTHOR") {
                    continue;
                } else if(name.StartsWith("Maintainer:")) {
                    name = name.Substring(11).Trim();
                }

                if(!names.ContainsKey(email)) {
                    bool skip = false;
                    
                    foreach(string iter_name in names.Values) {
                        if(String.Compare(iter_name, name, true) == 0) {
                            skip = true;
                            break;
                        }
                    }
                    
                    if(!skip) {
                        names.Add(email, name);
                    }
                }
            } catch {
                continue;
            }
        }

        // hack for banshee, remove if used elsewhere
        if(language_code == "sr") {
            return;
        } else if(language_code == "sr@latin") {
            language_code = "sr";
        }
        // end special

        // override language names from extracted
        switch(language_code) {
            case "ast": language_name = "Asturian"; break;
            case "ca": language_name = "Catalan"; break;
            case "ca@valencia": language_name = "Catalan (Valencian)"; break;
            case "cs": language_name = "Czech"; break;
            case "de": language_name = "German"; break;
            case "fr": language_name = "French"; break;
            case "el": language_name = "Greek"; break;
            case "en_GB": language_name = "British English"; break;
            case "gu": language_name = "Gujarati"; break;
            case "he": language_name = "Hebrew"; break;
            case "id": language_name = "Indonesian"; break;
            case "ko": language_name = "Korean"; break;
            case "lt": language_name = "Lithuanian"; break;
            case "pt": language_name = "Portuguese"; break;
            case "zh_CN": language_name = "Simplified Chinese"; break;
            case "zh_HK": language_name = "Chinese (Hong Kong)"; break;
            case "zh_TW": language_name = "Chinese (Taiwan)"; break;
            default: break;
        }
        
        Console.WriteLine("  <language code=\"{0}\" name=\"{1}\">", language_code, language_name);
        List<string> sorted_names = new List<string> (names.Values);
        sorted_names.Sort ();
        foreach(string name in sorted_names) {
            Console.WriteLine("    <person>{0}</person>", name.Replace("\"", "&quot;"));
        }
        Console.WriteLine("  </language>");
    }
}