[web2py] Re: Long text internationalization

2019-02-12 Thread Leonel Câmara
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)


[web2py] Re: Long text internationalization

2019-02-12 Thread Dave S


On Tuesday, February 12, 2019 at 3:57:36 AM UTC-8, Jonsubs wrote:
>
> Hi everyone,
> Which is the common practice when facing long text internationalization?
>
> For instance, What should be done when you want to render a web page, 
> sharing a common layout, that has several paragraphs that must be 
> completelly translated?
>
> Based on the manual ( 
> http://www.web2py.com/books/default/chapter/29/04/the-core#Internationalization-and-Pluralization-with-T
>  
> ) only string constants should be used with T. My guess is that large texts 
> should be treated differently.
>
> Thanks & Regards, Jon.
>


It looks like you're set for paragraphs of [previously] translated text.  
For really large pieces (pages long), you can take a look at how the web2py 
book does it:
https://github.com/web2py/web2py-book>
(just another web2py app, but the book content is in the sources 
sub-directory)

Does Google Translate have an API?  If it does, that would probably be like 
using jQuery divs.

FWIW, there's an interesting article (one of 3, it seems) about using a VPN 
to block app's from accessing Google resources;
https://gizmodo.com/i-cut-google-out-of-my-life-it-screwed-up-everything-1839565500>
[It would certainly be hard to use this group for support if you blocked 
access to Google servers.]
[[ Google controls 8,699,648 IP addresses, per the article]]

/dps

-- 
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.


Re: [web2py] Re: Long text internationalization

2019-02-12 Thread Jon Subscripted
Thanks Leonel!!
I'll use your plugin then.
Regards, Jon.

On Tue, Feb 12, 2019 at 1:37 PM Leonel Câmara 
wrote:

> 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.
>

-- 
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.


Re: [web2py] Re: Long text internationalization

2019-02-13 Thread Jon Subscripted
Thanks Dave,
For now I do not expect to go for pages long texts.
But still I'll take a look at how web2py-book is implemented.
Regards, Jon.

On Wed, Feb 13, 2019 at 12:39 AM Dave S  wrote:

>
>
> On Tuesday, February 12, 2019 at 3:57:36 AM UTC-8, Jonsubs wrote:
>>
>> Hi everyone,
>> Which is the common practice when facing long text internationalization?
>>
>> For instance, What should be done when you want to render a web page,
>> sharing a common layout, that has several paragraphs that must be
>> completelly translated?
>>
>> Based on the manual (
>> http://www.web2py.com/books/default/chapter/29/04/the-core#Internationalization-and-Pluralization-with-T
>> ) only string constants should be used with T. My guess is that large texts
>> should be treated differently.
>>
>> Thanks & Regards, Jon.
>>
>
>
> It looks like you're set for paragraphs of [previously] translated text.
> For really large pieces (pages long), you can take a look at how the web2py
> book does it:
> https://github.com/web2py/web2py-book>
> (just another web2py app, but the book content is in the sources
> sub-directory)
>
> Does Google Translate have an API?  If it does, that would probably be
> like using jQuery divs.
>
> FWIW, there's an interesting article (one of 3, it seems) about using a
> VPN to block app's from accessing Google resources;
>  https://gizmodo.com/i-cut-google-out-of-my-life-it-screwed-up-everything-1839565500
> >
> [It would certainly be hard to use this group for support if you blocked
> access to Google servers.]
> [[ Google controls 8,699,648 IP addresses, per the article]]
>
> /dps
>
> --
> 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.
>

-- 
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.