aboutsummaryrefslogtreecommitdiffstats
path: root/tools/translated_converter.py
blob: 76fcd9cd6d71dab58068829e16f7ab72959085a0 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This file is free software. It come without any warranty, to the extent
# permitted by applicable law. You can redistribute it and/or modify them under
# the terms of the Do What The Fuck You Want To Public License, Version 2, as
# published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more details.

# author yurchor
# http://gitweb.mageia.org/software/i18n/tools/tree/websites
# small adaptation by filip

import errno, glob, polib, re, os, getopt, sys
from time import strftime

def usage():
    print '\nUsage: python %s [OPTION]' %os.path.basename(sys.argv[0])
    print '       generates po file for existing lang translations'
    print 'Options: -h, --help                         : usage'
    print '         -f filename, --filename filename   : target filename'
    sys.exit(2)
try:
    opts, args = getopt.getopt(sys.argv[1:], "hf:", ["help", "filename="])
except getopt.GetoptError:
    usage() # print help information and exit

filename=''
for o,a in opts:
    if o in ("-h", "--help"):
        usage()
    if o in ("-f", "--filename"):
        filename=a

if filename == '':
    sys.exit('No filename given')

def po_append(message_id, message_str, langfile):
    # Strip ';' from msgid
    message_id = message_id.lstrip(';')
    if message_str == message_id:
        message_str=''
    message_str = message_str.replace('{ok}', '').rstrip()
    potentry = polib.POEntry(
      msgid = message_id.decode('utf-8'),
      msgstr = message_str.decode('utf-8'),
      occurrences=[(langfile,'')]
      )
    po.append(potentry)

file_mask = filename + '.*.lang'
for langfile in glob.glob(file_mask):
#  print langfile # useful for debuging
  nofilename = langfile.replace(filename + '.','')
  language = nofilename.replace('.lang','')
  #open lang file
  text = open(langfile,"r").read()+"\n"
  #Remove trailing spaces from lines
  spaces=' {1,}\n'
  spattern=re.compile(spaces,re.DOTALL)
  for emptyline in spattern.findall(text):
    text = text.replace(emptyline,'\n')
  text = text.replace('\n\n','\n\n\n\n')+'\n'

  # Write PO file
  po = polib.POFile(wrapwidth=999) # increase default wrap limit
  pocreationtime = strftime('%Y-%m-%d %H:%M%z')
  po.metadata = {
    'Project-Id-Version': langfile,
    'Report-Msgid-Bugs-To': 'mageia-i18n@mageia.org',
    'POT-Creation-Date': pocreationtime,
    'PO-Revision-Date': pocreationtime,
    'Last-Translator': 'Duffy Duck <d_duck@nowhere.net>',
    'Language-Team': 'LANGUAGE <LL@li.org>',
    'Language': language,
    'MIME-Version': '1.0',
    'Content-Type': 'text/plain; charset=UTF-8',
    'Content-Transfer-Encoding': '8bit',
    }

  # Parse contents and add them to PO
  messagetemplate='\n\n#\ .*?\n\n'
  mpattern=re.compile(messagetemplate,re.DOTALL)
  for mblock in mpattern.findall(text):
    message_comment, message_text = mblock.strip('\n').split('\n;',1)
    # Strip '# ' from comments
    message_comment = message_comment.lstrip('# ')
    message_id, message_str = message_text.split('\n',1)
#    print message_id # useful for debuging
    po_append(message_id, message_str, langfile)
  messagetemplate='\n\n;.*?\n\n'
  mpattern=re.compile(messagetemplate,re.DOTALL)
  for mblock in mpattern.findall(text):
    message_id, message_str = mblock.strip('\n').split('\n',1)
#    print message_id # useful for debuging
    po_append(message_id, message_str, langfile)
  if text[0] == ';':
    message_id, message_str = text.partition('\n\n')[0].strip('\n').split('\n',1)
#    print message_id # useful for debuging
    po_append(message_id, message_str, langfile)
  po.save(filename + '.po')