Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
intl: add support/pojs.py tool
  • Loading branch information
perexg committed Jun 18, 2015
1 parent 2b2cc88 commit 61d89c3
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 6 deletions.
24 changes: 23 additions & 1 deletion Makefile.webui
Expand Up @@ -36,6 +36,7 @@ RUN_CSS = PYTHONIOENCODING=utf-8 $(PYTHON) vendor/rcssmin-1.0.5/rcssmin.py
GZIPCMD += -n
XGETTEXT ?= xgettext --language=JavaScript --from-code=utf-8 -k_ -kN_ -s
MSGMERGE ?= msgmerge
POJS_PY = PYTHONIOENCODING=utf-8 $(PYTHON) support/pojs.py

ifeq ($(WEBUI),std)
DEBUG =
Expand Down Expand Up @@ -150,6 +151,7 @@ CSS_TV_SRC = $(foreach f,$(CSS_TV),$(WEBDIR)/$(f))
# Internationalization
#
PO-FILES = $(foreach f,$(LANGUAGES),intl/js/tvheadend.js.$(f).po)
JSI-FILES = $(foreach f,$(LANGUAGES),src/webui/static/intl/tvh.$(f).js.gz)

endif # WEBUI defined

Expand Down Expand Up @@ -188,6 +190,14 @@ define merge-po
@mv $(1).new $(1)
endef

define go-po
@mkdir -p $(dir $(1))
$(VV)$(POJS_PY) --in="$(2)" > $(1).new
$(VV)$(GZIPCMD) -c $(1).new > $@.new2
@rm $(1).new
@mv $(1).new2 $(1)
endef

all:
$(MAKE) -f $(IAM) WEBUI=std compile-std
$(MAKE) -f $(IAM) WEBUI=debug compile-debug
Expand Down Expand Up @@ -223,7 +233,7 @@ $(WEBDIR)/$(ROOTPATH)/tvh-tv.css.gz: $(CSS_TV_SRC)
.PHONY: compile-std
compile-std: $(WEBDIR)/$(ROOTPATH)/tvh.js.gz $(WEBDIR)/$(ROOTPATH)/tvh.css.gz \
$(WEBDIR)/$(ROOTPATH)/tvh-tv.js.gz $(WEBDIR)/$(ROOTPATH)/tvh-tv.css.gz \
$(WEBDIR)/extjs-std.c $(WEBDIR)/extjs-tv-std.c $(PO-FILES)
$(WEBDIR)/extjs-std.c $(WEBDIR)/extjs-tv-std.c $(JSI-FILES)
@echo "WEBUI std finished"

.PHONY: compile-debug
Expand All @@ -247,6 +257,18 @@ intl/js/tvheadend.js.cs.po: intl/js/tvheadend.js.pot
intl/js/tvheadend.js.pl.po: intl/js/tvheadend.js.pot
$(call merge-po,$@,$<)

src/webui/static/intl/tvh.de.js.gz: intl/js/tvheadend.js.de.po
$(call go-po,$@,$<)

src/webui/static/intl/tvh.fr.js.gz: intl/js/tvheadend.js.fr.po
$(call go-po,$@,$<)

src/webui/static/intl/tvh.cs.js.gz: intl/js/tvheadend.js.cs.po
$(call go-po,$@,$<)

src/webui/static/intl/tvh.pl.js.gz: intl/js/tvheadend.js.pl.po
$(call go-po,$@,$<)

.PHONY:
clean:
rm -f $(foreach f,tvh.js tvh.css tvh-tv.js tvh-tv.css,\
Expand Down
2 changes: 1 addition & 1 deletion intl/js/tvheadend.js.cs.po
Expand Up @@ -471,7 +471,7 @@ msgstr ""

#: src/webui/static/app/epg.js:860 src/webui/static/app/epg.js:871
msgid "Electronic Program Guide"
msgstr ""
msgstr "Elektronický průvodce programem"

#: src/webui/static/app/esfilter.js:64 src/webui/static/app/esfilter.js:83
#: src/webui/static/app/esfilter.js:121 src/webui/static/app/esfilter.js:139
Expand Down
4 changes: 0 additions & 4 deletions support/css.py
Expand Up @@ -32,10 +32,6 @@ def ustrip(u, f):

def url(fn):

PATHS={
'../docresources':'../docresources',
}

f = utf8open(fn, 'r')
if fn[0] != '/':
fn = os.path.join(PWD, fn)
Expand Down
124 changes: 124 additions & 0 deletions support/pojs.py
@@ -0,0 +1,124 @@
#!/usr/bin/env python
#
# Convert .po file to javascript
#

import sys, os

VERBOSE = 'V' in os.environ and len(os.environ['V']) > 0
TVHDIR = os.path.realpath('.')
PWD = os.path.realpath(os.environ['PWD'])

def info(fmt, *msg):
sys.stderr.write(" [INFO ] " + (fmt % msg) + '\n')

def error(fmt, *msg):
sys.stderr.write(" [ERROR] " + (fmt % msg) + '\n')
sys.exit(1)

def utf8open(fn, mode):
if sys.version_info[0] < 3:
return open(fn, mode)
else:
return open(fn, mode, encoding='utf-8')

def po_str(text):
text = text.lstrip().rstrip()
if not text:
return ''
if text[0] != '"' and text[-1] != '"':
raise ValueError('Wrong text: %s' % text)
text = text[1:-1]
if not text:
return ''
r = ''
l = len(text)
i = 0
while i < l:
c = text[i]
if c == '\\':
i += 1
if i >= l:
continue
c = text[i]
if c == 'n':
c = '\n'
elif c == 'r':
c = '\r'
elif c == 't':
c = '\t'
r += c
i += 1
return r

def po_modify(out, str):
str = po_str(str)
return out + str

class PO:

def __init__(self):
self.strings = {}

def po_finish(self, msgid, msgstr):
if msgid and not msgstr:
msgstr = msgid
if msgid:
self.strings[msgid] = msgstr

def po_parse(self, text):
msgid = ''
msgstr = ''
msgidflag = False
for line in text.splitlines():
line = line.lstrip().rstrip()
if line and line[0] == '#':
continue
if not line:
self.po_finish(msgid, msgstr)
msgid = ''
msgstr = ''
if line.startswith('msgid '):
msgid = po_modify(msgid, line[6:])
msgidflag = True
elif line.startswith('msgstr '):
msgstr = po_modify(msgstr, line[7:])
msgidflag = False
elif msgidflag:
msgid = po_modify(msgid, line)
else:
msgstr = po_modify(msgstr, line)
self.po_finish(msgid, msgstr)

def cnv(fn):

f = utf8open(fn, 'r')
if fn[0] != '/':
fn = os.path.join(PWD, fn)
fn = os.path.normpath(fn)
if VERBOSE:
info('pojs: %s', fn)
text = f.read(2*1024*1024)
f.close()
po = PO()
po.po_parse(text)
sys.stdout.write("tvh_locale = {\n");
sep = ''
for s in po.strings:
if s != po.strings[s]:
sys.stdout.write("%s'%s':'%s'" % (sep, s, po.strings[s]))
sep = ',\n'
sys.stdout.write("\n}\n");

fn=''
for opt in sys.argv:
if opt.startswith('--in='):
fn=os.path.normpath(opt[5:]).split(' ')

if not fn:
error('Specify input file')
for f in fn:
try:
cnv(f)
except:
error('Unable to process file "%s": %s', f, sys.exc_info())

0 comments on commit 61d89c3

Please sign in to comment.