def setUp(self): self.filename = tempfile.mktemp() contents = dict() for i in range(1000): contents["key%d" % i] = "value%d" % i languages.write_dict(self.filename, contents) languages.read_dict(self.filename)
def edit_language(): """ Edit language file """ filename = '/'.join(request.args) from gluon.languages import read_dict, write_dict strings = read_dict(apath(filename, r=request)) keys = sorted(strings.keys()) rows = [] rows.append(H2(T('Original/Translation'))) for i,key in enumerate(keys): if len(key) <= 40: elem = INPUT(_type='text', _name=str(i),value=strings[key],_size=70) else: elem = TEXTAREA(_name=str(i), value=strings[key], _cols=70, _rows=5) # Making the short circuit compatible with <= python2.4 k = (strings[key] != key) and key or B(key) rows.append(P(k, BR(), elem, TAG.BUTTON(T('delete'), _onclick='return delkey("%s")' % i), _id=str(i))) rows.append(INPUT(_type='submit', _value=T('update'))) form = FORM(*rows) if form.accepts(request.vars, keepvalues=True): strs = dict() for i,key in enumerate(keys): if form.vars[str(i)]==chr(127): continue strs[key] = form.vars[str(i)] write_dict(apath(filename, r=request), strs) session.flash = T('file saved on %(time)s', dict(time=time.ctime())) redirect(URL(r=request,args=request.args)) return dict(app=request.args[0], filename=filename, form=form)
def edit_language(): """ Edit language file """ #app = get_app() lang = ("zh-tw",T("Mandarin")) args = ['devicedb','languages',lang[0]+'.py'] filename = '/'.join(args) response.title = args[-1] strings = read_dict(apath(filename, r=request)) if '__corrupted__' in strings: form = SPAN(strings['__corrupted__'], _class='error') return dict(filename=filename, form=form) keys = sorted(strings.keys(), lambda x, y: cmp( unicode(x, 'utf-8').lower(), unicode(y, 'utf-8').lower())) rows = [] rows.append(H2(T('Original/Translation'))) for key in keys: name = md5_hash(key) s = strings[key] (prefix, sep, key) = key.partition('\x01') if sep: prefix = SPAN(prefix + ': ', _class='tm_ftag') k = key else: (k, prefix) = (prefix, '') _class = 'untranslated' if k == s else 'translated' if len(s) <= 40: elem = INPUT(_type='text', _name=name, value=s, _size=70, _class=_class) else: elem = TEXTAREA(_name=name, value=s, _cols=70, _rows=5, _class=_class) # Making the short circuit compatible with <= python2.4 k = (s != k) and k or B(k) new_row = DIV( LABEL(prefix, k, _style="font-weight:normal;"), CAT(elem, '\n', TAG.BUTTON( T('delete'), _onclick='return delkey("%s")' % name, _class='btn' )), _id=name, _class='span6 well well-small') rows.append(DIV(new_row,_class="row-fluid")) rows.append(DIV(INPUT(_type='submit', _value=T('update'), _class="btn btn-primary"), _class='controls')) form = FORM(*rows) if form.accepts(request.vars, keepvalues=True): strs = dict() for key in keys: name = md5_hash(key) if form.vars[name] == chr(127): continue strs[key] = form.vars[name] write_dict(apath(filename, r=request), strs) session.flash = T('saved on UTC') + request.utcnow.strftime(" %Y-%m-%d %H:%M") redirect(URL(r=request, args=request.args)) return dict(app=args[0], filename=filename, form=form, lang=lang)
def read_write(args): (filename, iterations) = args for i in range(0, iterations): content = languages.read_dict(filename) if not len(content): return False languages.write_dict(filename, content) return True
def dc_question_l10n_onaccept(form): """ On-accept routine for dc_question_l10n: - Update the Translations file with translated Options """ try: question_l10n_id = form.vars.id except AttributeError: return db = current.db # Read the Question qtable = db.dc_question ltable = db.dc_question_l10n query = (qtable.id == ltable.question_id) & \ (ltable.id == question_l10n_id) question = db(query).select(qtable.field_type, qtable.options, ltable.options_l10n, ltable.language, limitby=(0, 1)).first() if question["dc_question.field_type"] != 6: # Nothing we need to do return options = question["dc_question.options"] options_l10n = question["dc_question_l10n.options_l10n"] len_options = len(options) if len_options != len(options_l10n): current.session.error( T("Number of Translated Options don't match original!")) return # Read existing translations (if any) w2pfilename = os.path.join( current.request.folder, "languages", "%s.py" % question["dc_question_l10n.language"]) if os.path.exists(w2pfilename): translations = read_dict(w2pfilename) else: translations = {} # Add ours for i in range(len_options): original = s3_str(options[i]) translated = s3_str(options_l10n[i]) if original != translated: translations[original] = translated # Write out new file write_dict(w2pfilename, translations)
def edit_language(): """ Edit language file """ filename = '/'.join(request.args) from gluon.languages import read_dict, write_dict strings = read_dict(apath(filename, r=request)) keys = sorted(strings.keys(), lambda x, y: cmp(x.lower(), y.lower())) rows = [] rows.append(H2(T('Original/Translation'))) for key in keys: name = md5_hash(key) if key == strings[key]: _class = 'untranslated' else: _class = 'translated' if len(key) <= 40: elem = INPUT(_type='text', _name=name, value=strings[key], _size=70, _class=_class) else: elem = TEXTAREA(_name=name, value=strings[key], _cols=70, _rows=5, _class=_class) # Making the short circuit compatible with <= python2.4 k = (strings[key] != key) and key or B(key) rows.append( P(k, BR(), elem, TAG.BUTTON(T('delete'), _onclick='return delkey("%s")' % name), _id=name)) rows.append(INPUT(_type='submit', _value=T('update'))) form = FORM(*rows) if form.accepts(request.vars, keepvalues=True): strs = dict() for key in keys: name = md5_hash(key) if form.vars[name] == chr(127): continue strs[key] = form.vars[name] write_dict(apath(filename, r=request), strs) session.flash = T('file saved on %(time)s', dict(time=time.ctime())) redirect(URL(r=request, args=request.args)) return dict(app=request.args[0], filename=filename, form=form)
def edit_language(): """ Edit language file """ app = get_app() filename = "/".join(request.args) from gluon.languages import read_dict, write_dict strings = read_dict(apath(filename, r=request)) keys = sorted(strings.keys(), lambda x, y: cmp(x.lower(), y.lower())) rows = [] rows.append(H2(T("Original/Translation"))) for key in keys: name = md5_hash(key) if key == strings[key]: _class = "untranslated" else: _class = "translated" if len(key) <= 40: elem = INPUT(_type="text", _name=name, value=strings[key], _size=70, _class=_class) else: elem = TEXTAREA(_name=name, value=strings[key], _cols=70, _rows=5, _class=_class) # Making the short circuit compatible with <= python2.4 k = (strings[key] != key) and key or B(key) rows.append(P(k, BR(), elem, TAG.BUTTON(T("delete"), _onclick='return delkey("%s")' % name), _id=name)) rows.append(INPUT(_type="submit", _value=T("update"))) form = FORM(*rows) if form.accepts(request.vars, keepvalues=True): strs = dict() for key in keys: name = md5_hash(key) if form.vars[name] == chr(127): continue strs[key] = form.vars[name] write_dict(apath(filename, r=request), strs) session.flash = T("file saved on %(time)s", dict(time=time.ctime())) redirect(URL(r=request, args=request.args)) return dict(app=request.args[0], filename=filename, form=form)
def edit_language(): """ Edit language file, copied from admin/controllers/default.py """ import os import re import time from gluon.languages import (read_dict, write_dict, to_native) from gluon.utils import md5_hash if len(request.args) == 0: raise HTTP(400, "No language provided") if re.match(request.args[0], r'[^\w\.\-]') is None: filename = os.path.join(request.folder, "languages", request.args[0] + ".py") if os.path.isfile(filename): response.title = "OneZoom language strings for " + request.args[0] strings = read_dict(filename) if '__corrupted__' in strings: form = SPAN(strings['__corrupted__'], _class='error') return dict(filename=filename, form=form) keys = sorted(strings.keys(), key=lambda x: to_native(x).lower()) rows = [] rows.append(H2(T('Original/Translation'))) for key in keys: name = md5_hash(key) s = strings[key] (prefix, sep, key) = key.partition('\x01') if sep: prefix = SPAN(prefix + ': ', _class='tm_ftag') k = key else: (k, prefix) = (prefix, '') _class = 'untranslated' if k == s else 'translated' if len(s) <= 40 and '\n' not in s: elem = INPUT(_type='text', _name=name, value=s, _size=70, _class=_class) else: elem = TEXTAREA(_name=name, value="\n" + s, _cols=80, _rows=5, _class=_class) # Making the short circuit compatible with <= python2.4 k = (s != k) and k.replace('\n', '¶') or B(k.replace( '\n', '¶')) new_row = DIV( LABEL(prefix, k, _style="font-weight:normal; display:block;"), CAT( elem, '\n', TAG.BUTTON(IMG(_src=URL("static", "images", "close_red.png"), _width="20", _height="20"), XML(' '), T('Delete'), _onclick='return delkey("%s")' % name, _class='btn')), _id=name, _class='span6 well well-small') rows.append(DIV(new_row, _class="row-fluid")) rows.append( DIV(INPUT(_type='submit', _value=T('Update'), _class="btn btn-primary"), _class='controls')) form = FORM(*rows) if form.accepts(request.vars, keepvalues=True): strs = dict() for key in keys: name = md5_hash(key) with warnings.catch_warnings(record=True) as w: try: if form.vars[name] == chr(127): continue except UnicodeWarning: if form.vars[name] == oldchr(127): continue strs[key] = form.vars[name] write_dict(filename, strs) session.flash = T('file saved on %(time)s', dict(time=time.ctime())) redirect(URL(r=request, args=request.args)) return dict(filename=filename, form=form)
def edit_language(): """ Edit language file, copied from admin/controllers/default.py """ import os import re import time from gluon.languages import (read_dict, write_dict) from gluon.utils import md5_hash if len(request.args) == 0: raise HTTP(400 , "No language provided") if re.match(request.args[0], r'[^\w\.\-]') is None: filename = os.path.join(request.folder,"languages",request.args[0] + ".py") if os.path.isfile(filename): response.title = "OneZoom language strings for " + request.args[0] strings = read_dict(filename) if '__corrupted__' in strings: form = SPAN(strings['__corrupted__'], _class='error') return dict(filename=filename, form=form) keys = sorted(strings.keys(), lambda x, y: cmp( unicode(x, 'utf-8').lower(), unicode(y, 'utf-8').lower())) rows = [] rows.append(H2(T('Original/Translation'))) for key in keys: name = md5_hash(key) s = strings[key] (prefix, sep, key) = key.partition('\x01') if sep: prefix = SPAN(prefix + ': ', _class='tm_ftag') k = key else: (k, prefix) = (prefix, '') _class = 'untranslated' if k == s else 'translated' if len(s) <= 40 and '\n' not in s: elem = INPUT(_type='text', _name=name, value=s, _size=70, _class=_class) else: elem = TEXTAREA(_name=name, value="\n"+s, _cols=80, _rows=5, _class=_class) # Making the short circuit compatible with <= python2.4 k = (s != k) and k.replace('\n','¶') or B(k.replace('\n','¶')) new_row = DIV(LABEL(prefix, k, _style="font-weight:normal; display:block;"), CAT(elem, '\n', TAG.BUTTON( IMG(_src=URL("static","images","close_red.png"),_width="20",_height="20"), XML(' '), T('Delete'), _onclick='return delkey("%s")' % name, _class='btn')), _id=name, _class='span6 well well-small') rows.append(DIV(new_row, _class="row-fluid")) rows.append(DIV(INPUT(_type='submit', _value=T('Update'), _class="btn btn-primary"), _class='controls')) form = FORM(*rows) if form.accepts(request.vars, keepvalues=True): strs = dict() for key in keys: name = md5_hash(key) if form.vars[name] == chr(127): continue strs[key] = form.vars[name] write_dict(filename, strs) session.flash = T('file saved on %(time)s', dict(time=time.ctime())) redirect(URL(r=request, args=request.args)) return dict(filename=filename, form=form)