Large texts are actually fine as long as they're kind of static.

For dynamic long texts I store them in a JSON field where I have a dict 
with a key for each language I want to support, I actually made a plugin 
which gives you a nice widget for this field json field which you can put 
in your modules folder and import it. I'll annex it.

Example usage:

from plugin_json_translation import TranslateWidget

def represent_lang(v, row):
    if v is None:
        return v
    if T.accepted_language[:2] in v:
        return v[LANG]
    else:
        return v['en']

db.define_table('my_test',
    Field('body', 'json', widget=TranslateWidget().widget, requires=
IS_EMPTY_OR(IS_JSON()), represent=represent_lang),
)


Then you just need to call render when you get my_test rows and you will 
get a translated version.


-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
# -*- coding: utf-8 -*-
# author: Leonel Câmara
"""
You can also use it with plugin_ckeditor example:
db.thing.body.widget=TranslateWidget(input_widget=ckeditor.widget).widget
"""
from gluon.html import *
from gluon.sqlhtml import FormWidget
from gluon import current
import json

LANGS = ['en' , 'pt']


class TranslateWidget(FormWidget):
    _class = 'string'

    def __init__(self, langs=None, input_widget=None):
        self.input_widget = input_widget
        self.langs = langs or LANGS
    
    def widget(self, field, value, **attributes):
        """
        Generates an INPUT text tag.

        see also: `FormWidget.widget`
        """

        default = dict(
            _type='hidden',
            value=(value is not None and str(value)) or '',
        )
        attr = FormWidget._attributes(field, default, **attributes)

        realvalue = INPUT(**attr)
        _id = attr['_id']

        
        nav_tabs = UL(
                  *[LI(A(lang, **{'_href': '#' + _id + '_' + lang, '_data-toggle':'tab', '_class': 'nav-link'}), _class='nav-item') for lang in self.langs],
                  _class='nav nav-tabs', _role="tablist"
                  )

        values = json.loads(value) if value else {lang:'' for lang in self.langs}

        if self.input_widget is None:
            tab_content = DIV(
                    *[DIV(TEXTAREA(values[lang], _name=_id + '_' + lang, _class='form-control', _placeholder=current.T('Write me...'), _rows=2), _id=_id + '_' + lang, _class='tab-pane fade') for lang in self.langs],

            _class='tab-content')
        else:
            tab_content = DIV(_class='tab-content')
            for lang in self.langs:
                attributes = {'_id': _id + '_' + lang + '_widget',
                              '_name':_id + '_' + lang, 
                              '_placeholder':current.T('Write me...')
                             }
                tab_content.append(
                    DIV(self.input_widget(field, values[lang], **attributes), _class='tab-pane fade', _role='tabpanel', _id=_id + '_' + lang)
                    )


        script = SCRIPT("""
                        $('#%(_id)s').closest('form').submit(function(){
                            $('#%(_id)s').val(JSON.stringify({
                                %(langvals)s
                            }));
                            %(removelangvals)s;
                        });
                        // This bullshit is needed for tabs with widgets using plugin simplemde
                        $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                          var target = $(e.target).attr("href") // activated tab
                          window[target.substring(1) + "_widget_mde"].codemirror.refresh();
                        });
                        """ % { '_id': _id, 
                                'langvals': ','.join('"%s":$("[name=\'%s\']").val()' % (lang, _id + '_' + lang) for lang in self.langs), 
                                'removelangvals': ';'.join('$("[name=\'%s\']").remove()' % (_id + '_' + lang) for lang in self.langs)
                               }
                       )
        nav_tabs[0][0]['_class'] +=' active'
        tab_content[0]['_class'] += ' show active'
        if current.request.post_vars:
            # Make sure we validate our extra inputs for IS_JSON
            for lang in self.langs:
                name=_id + '_' + lang
                current.request.post_vars[name] = '{}'
        return CAT(nav_tabs, tab_content, realvalue, script)

Reply via email to