~christhehorton/ubuntu-community-accomplishments/add-askubuntu-student-badge

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
import os
import glob
import polib
import ConfigParser


class GenerateTranslations():
    def __init__(self):
    
        print "Scanning for accomplishments:"
        accoms = {}
        collslist = os.listdir("accomplishments")
        for collection in collslist:
            colldir = os.path.join("accomplishments",collection)
            cfg = ConfigParser.RawConfigParser()
            cfg.read(os.path.join(colldir,"ABOUT"))
            deflang = cfg.get("general","langdefault")
            deflangdir = os.path.join(colldir,deflang)
            deflangdirlist = os.listdir(deflangdir)
            for item in deflangdirlist:
                itempath = os.path.join(deflangdir,item)
                if os.path.isdir(itempath):
                    dislist = os.listdir(itempath)
                    for accom in dislist:
                        accompath = os.path.join(itempath,accom)
                        accID = collection + "/" + accom[:-15]
                        print " " + accID
                        accoms[accID] = {'origfile' : accompath, 'inlangfile' : os.path.join(item,accom), 'coll' : collection}
                else:
                    accID = collection + "/" + item[:-15]
                    print " " + accID
                    accoms[accID] = {'origfile' : itempath,'inlangfile' : item, 'coll' : collection}
    
        self.pofiles = glob.glob("generated/po/*.po")

        for f in self.pofiles:
            langcode = os.path.split(f)[1].split(".")[0]
            popath = "generated/po/" + langcode + ".po"
            
            print "Opening: " + langcode + " at " + popath
            
            self.currentpo = polib.pofile(popath)
                
            for accomID in accoms:
                collection = accoms[accomID]['coll']
                origfile = accoms[accomID]['origfile']
                inlangfile = accoms[accomID]['inlangfile']
                langdir = os.path.join(os.path.join("accomplishments/", collection), langcode)
                if not os.path.exists(langdir):
                    os.makedirs(langdir)
                    
                print "...processing: " + accomID
                self.masterconfig = ConfigParser.ConfigParser()
                self.masterconfig.read(origfile)
                #title = self.masterconfig.get("accomplishment", "title")
                self.outputconfig = ConfigParser.ConfigParser()
                self.outputconfig.add_section("accomplishment")

                title = self.process_field(accomID, "title")
                self.outputconfig.set("accomplishment", "title", title)
                
                description = self.process_field(accomID, "description")
                self.outputconfig.set("accomplishment", "description", description)
                
                if self.masterconfig.has_option("accomplishment", "collection"):
                    print self.masterconfig.get("accomplishment", "collection")                    
                    self.outputconfig.set("accomplishment", "collection", self.masterconfig.get("accomplishment", "collection"))
                
                if self.masterconfig.has_option("accomplishment", "category"):
                    self.outputconfig.set("accomplishment", "category", self.masterconfig.get("accomplishment", "category"))                

                if self.masterconfig.has_option("accomplishment", "needs-signing"):
                    self.outputconfig.set("accomplishment", "needs-signing", self.masterconfig.get("accomplishment", "needs-signing"))                
                
                if self.masterconfig.has_option("accomplishment", "needs-information"):
                    self.outputconfig.set("accomplishment", "needs-information", self.masterconfig.get("accomplishment", "needs-information")) 
                    
                if self.masterconfig.has_option("accomplishment", "icon"):
                    self.outputconfig.set("accomplishment", "icon", self.masterconfig.get("accomplishment", "icon"))                
                
                if self.masterconfig.has_option("accomplishment", "depends"):
                    self.outputconfig.set("accomplishment", "depends", self.masterconfig.get("accomplishment", "depends"))                

                if self.masterconfig.has_option("accomplishment", "author"):
                    self.outputconfig.set("accomplishment", "author", self.masterconfig.get("accomplishment", "author"))                

                # things that can be translated
                
                summary = self.process_field(accomID, "summary")
                self.outputconfig.set("accomplishment", "summary", summary)                

                steps = self.process_field(accomID, "steps")
                self.outputconfig.set("accomplishment", "steps", steps)

                tips = self.process_field(accomID, "tips")
                self.outputconfig.set("accomplishment", "tips", tips)
                
                pitfalls = self.process_field(accomID, "pitfalls")
                self.outputconfig.set("accomplishment", "pitfalls", pitfalls)
                
                help = self.process_field(accomID, "help")
                self.outputconfig.set("accomplishment", "help", help)

                inlangfile_splitted = inlangfile.split("/")
                if len(inlangfile_splitted) == 2:
                    if not os.path.exists(os.path.join(langdir, inlangfile_splitted[0])):
                        os.makedirs(os.path.join(langdir, inlangfile_splitted[0]))
                path = os.path.join(langdir, inlangfile)

                outfile = open(path, "w")
                self.outputconfig.write(outfile)
                outfile.close()
                
        print "Done."

    def process_field(self, accomID, field):
        print field
        try:
            val = None
            final = None
            val = self.currentpo.find(accomID + "_" + field).msgstr.split("\r\n")
            
            while True:
                try:
                    val.remove("")
                except ValueError:
                    break
            final = ""
            
            for l in val:
                final = final + (l + "\n")
            
            # remove the final \n
            final = final.rstrip()
            
            if final == "":
                final = self.masterconfig.get("accomplishment", field)
            return final.encode("utf-8")
        except:
            if self.masterconfig.has_option("accomplishment", field):
                print "......" + field + " not found,using original translation."
                content = self.masterconfig.get("accomplishment", field)
                return content.rstrip().encode("utf-8")

if __name__=="__main__":
    trans = GenerateTranslations()