1
|
|
|
import polib |
2
|
|
|
import os |
3
|
|
|
from pages.models import Page, Content |
4
|
|
|
import sys |
5
|
|
|
from pages import settings |
6
|
|
|
|
7
|
|
|
do_not_msg = "DO NOT MODIFIY BELOW THIS LINE" |
8
|
|
|
po_comment = """Page %s |
9
|
|
|
%s |
10
|
|
|
placeholder=%s |
11
|
|
|
page_id=%d |
12
|
|
|
content_id=%s""" |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
def export_po_files(path='poexport', stdout=None): |
16
|
|
|
""" |
17
|
|
|
Export all the content from the published pages into |
18
|
|
|
po files. The files will be automatically updated |
19
|
|
|
with the new content if you run the command again. |
20
|
|
|
""" |
21
|
|
|
if stdout is None: |
22
|
|
|
stdout = sys.stdout |
23
|
|
|
if not path.endswith('/'): |
24
|
|
|
path += '/' |
25
|
|
|
|
26
|
|
|
source_language = settings.PAGE_DEFAULT_LANGUAGE |
27
|
|
|
source_list = [] |
28
|
|
|
for page in Page.objects.published(): |
29
|
|
|
source_list.extend(page.content_by_language(source_language)) |
30
|
|
|
|
31
|
|
|
for lang in settings.PAGE_LANGUAGES: |
32
|
|
|
if lang[0] != settings.PAGE_DEFAULT_LANGUAGE: |
33
|
|
|
try: |
34
|
|
|
os.mkdir(path) |
35
|
|
|
except OSError: |
36
|
|
|
pass |
37
|
|
|
po_path = path + lang[0] + '.po' |
38
|
|
|
stdout.write("Export language %s.\n" % lang[0]) |
39
|
|
|
po = polib.pofile(po_path) |
40
|
|
|
po.metadata['Content-Type'] = 'text/plain; charset=utf-8' |
41
|
|
|
|
42
|
|
|
for source_content in source_list: |
43
|
|
|
page = source_content.page |
44
|
|
|
try: |
45
|
|
|
target_content = Content.objects.get_content_object( |
46
|
|
|
page, lang[0], source_content.type) |
47
|
|
|
msgstr = target_content.body |
48
|
|
|
except Content.DoesNotExist: |
49
|
|
|
target_content = None |
50
|
|
|
msgstr = "" |
51
|
|
|
if source_content.body: |
52
|
|
|
if target_content: |
53
|
|
|
tc_id = str(target_content.id) |
54
|
|
|
else: |
55
|
|
|
tc_id = "" |
56
|
|
|
entry = polib.POEntry(msgid=source_content.body, |
57
|
|
|
msgstr=msgstr) |
58
|
|
|
entry.tcomment = po_comment % (page.title(), do_not_msg, |
59
|
|
|
source_content.type, page.id, tc_id) |
60
|
|
|
if entry not in po: |
61
|
|
|
po.append(entry) |
62
|
|
|
po.save(po_path) |
63
|
|
|
stdout.write("""Export finished. The files are available """ |
64
|
|
|
"""in the %s directory.\n""" % path) |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def import_po_files(path='poexport', stdout=None): |
68
|
|
|
""" |
69
|
|
|
Import all the content updates from the po files into |
70
|
|
|
the pages. |
71
|
|
|
""" |
72
|
|
|
source_language = settings.PAGE_DEFAULT_LANGUAGE |
73
|
|
|
source_list = [] |
74
|
|
|
pages_to_invalidate = [] |
75
|
|
|
for page in Page.objects.published(): |
76
|
|
|
source_list.extend(page.content_by_language(source_language)) |
77
|
|
|
if stdout is None: |
78
|
|
|
stdout = sys.stdout |
79
|
|
|
if not path.endswith('/'): |
80
|
|
|
path += '/' |
81
|
|
|
|
82
|
|
|
for lang in settings.PAGE_LANGUAGES: |
83
|
|
|
if lang[0] != settings.PAGE_DEFAULT_LANGUAGE: |
84
|
|
|
stdout.write("Update language %s.\n" % lang[0]) |
85
|
|
|
po_path = path + lang[0] + '.po' |
86
|
|
|
po = polib.pofile(po_path) |
87
|
|
|
for entry in po: |
88
|
|
|
meta_data = entry.tcomment.split(do_not_msg)[1].split("\n") |
89
|
|
|
placeholder_name = meta_data[1].split('=')[1] |
90
|
|
|
page_id = int(meta_data[2].split('=')[1]) |
91
|
|
|
try: |
92
|
|
|
content_id = int(meta_data[3].split('=')[1]) |
93
|
|
|
except ValueError: |
94
|
|
|
content_id = None |
95
|
|
|
|
96
|
|
|
page = Page.objects.get(id=page_id) |
97
|
|
|
current_content = Content.objects.get_content(page, lang[0], |
98
|
|
|
placeholder_name) |
99
|
|
|
if current_content != entry.msgstr: |
100
|
|
|
stdout.write("Update page %d placeholder %s.\n" % (page_id, |
101
|
|
|
placeholder_name)) |
102
|
|
|
Content.objects.create_content_if_changed( |
103
|
|
|
page, lang[0], placeholder_name, entry.msgstr) |
104
|
|
|
if page not in pages_to_invalidate: |
105
|
|
|
pages_to_invalidate.append(page) |
106
|
|
|
|
107
|
|
|
for page in pages_to_invalidate: |
108
|
|
|
page.invalidate() |
109
|
|
|
stdout.write("Import finished from %s.\n" % path) |