Skip to content

Commit

Permalink
[FIX] formio: Fix #2: language switcher buttons and translations in F…
Browse files Browse the repository at this point in the history
…orm Builder and Forms.

- Replace iso_code by code (e.g. nl now nl_NL).
- Add field "English Enable" on Form Builder, if English is installed
  this shows the English language button, even if no translations (so
- Formio defaults).

Concerns #91, #93
  • Loading branch information
bobslee committed Dec 9, 2020
1 parent e020f4a commit 62c249d
Show file tree
Hide file tree
Showing 11 changed files with 2,186 additions and 18 deletions.
2 changes: 1 addition & 1 deletion formio/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
'name': 'Forms',
'summary': 'Form Builder & integration of professional and versatile Forms to collect any information you need for your business.',
'version': '8.1',
'version': '8.2',
'license': 'LGPL-3',
'author': 'Nova Code',
'website': 'https://www.novacode.nl',
Expand Down
12 changes: 9 additions & 3 deletions formio/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def builder_root(self, builder_id, **kwargs):

builder = request.env['formio.builder'].browse(builder_id)
languages = builder.languages
lang_en = request.env.ref('base.lang_en')

if lang_en.active and builder.language_en_enable and 'en_US' not in languages.mapped('code'):
languages |= request.env.ref('base.lang_en')

values = {
'languages': languages,
Expand Down Expand Up @@ -88,8 +92,10 @@ def form_root(self, uuid, **kwargs):
request.env.context = context

languages = form.builder_id.languages
# if 'en_US' not in languages.mapped('code'):
# languages |= request.env.ref('base.lang_en')
lang_en = request.env.ref('base.lang_en')

if lang_en.active and form.builder_id.language_en_enable and 'en_US' not in languages.mapped('code'):
languages |= request.env.ref('base.lang_en')

values = {
'languages': languages.sorted('name'),
Expand Down Expand Up @@ -273,7 +279,7 @@ def _get_form_js_options(self, form):
options = form._get_js_options()

# default language
if request.env.user.lang in form.languages.mapped('iso_code'):
if request.env.user.lang in form.languages.mapped('code'):
language = request.env.user.lang
else:
language = request._context['lang']
Expand Down
4 changes: 2 additions & 2 deletions formio/controllers/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def _get_public_form_js_options(self, form):

lang = request.env['res.lang']._lang_get(request.env.user.lang)
if lang:
options['language'] = lang.iso_code
options['language'] = lang.code
options['i18n'] = form.i18n_translations()
return options

Expand All @@ -179,7 +179,7 @@ def _get_public_create_form_js_options(self, builder):

lang = request.env['res.lang']._lang_get(request.env.user.lang)
if lang:
options['language'] = lang.iso_code
options['language'] = lang.code
else:
context = request.env.context
options['language'] = context['lang'] if context.get('lang') else self.env.ref('base.lang_en')
Expand Down
2,146 changes: 2,146 additions & 0 deletions formio/i18n/pt_BR.po

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions formio/models/formio_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class Builder(models.Model):
'res.groups', string='Allow groups to force update State',
help="User groups allowed to manually force an update of the Form state."
"If no groups are specified it's allowed for every user.")
language_en_enable = fields.Boolean(default=True, string='English Enabled')
component_partner_name = fields.Char(string='Component Partner Name', track_visibility='onchange')
component_partner_email = fields.Char(string='Component Partner Email', track_visibility='onchange')
component_partner_add_follower = fields.Boolean(
Expand Down Expand Up @@ -251,6 +252,9 @@ def _compute_display_fields(self):
def _compute_languages(self):
for r in self:
languages = r.translations.mapped('lang_id')
lang_en = self.env.ref('base.lang_en')
if lang_en.active and r.language_en_enable and 'en_US' not in languages.mapped('code'):
languages |= lang_en
r.languages = languages.sorted('name')

def _compute_edit_url(self):
Expand Down Expand Up @@ -365,7 +369,7 @@ def _get_js_options(self):
options['i18n'] = self.i18n_translations()

# default language
if self.env.user.lang in self.languages.mapped('iso_code'):
if self.env.user.lang in self.languages.mapped('code'):
language = self.env.user.lang
else:
language = self._context['lang']
Expand Down Expand Up @@ -411,16 +415,16 @@ def i18n_translations(self):
i18n = {}
# Formio GUI/API translations
for trans in self.formio_version_id.translations:
if trans.lang_id.iso_code not in i18n:
i18n[trans.lang_id.iso_code] = {trans.property: trans.value}
if trans.lang_id.code not in i18n:
i18n[trans.lang_id.code] = {trans.property: trans.value}
else:
i18n[trans.lang_id.iso_code][trans.property] = trans.value
i18n[trans.lang_id.code][trans.property] = trans.value
# Form Builder translations (labels etc).
# These could override the former GUI/API translations, but
# that's how the Javascript API works.
for trans in self.translations:
if trans.lang_id.iso_code not in i18n:
i18n[trans.lang_id.iso_code] = {trans.source: trans.value}
if trans.lang_id.code not in i18n:
i18n[trans.lang_id.code] = {trans.source: trans.value}
else:
i18n[trans.lang_id.iso_code][trans.source] = trans.value
i18n[trans.lang_id.code][trans.source] = trans.value
return i18n
10 changes: 9 additions & 1 deletion formio/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ <h4 class="oe_mb16 text-center">Download &amp; Install Available (Form.io GUI) V
<section class="oe_container">
<div class="oe_row oe_spaced">
<h2 class="oe_slogan" style="color:#875A7B;">Releases</h2>
<h4 class="oe_mb16">8.2</h4>
<ul>
<li>
Fix #2: language switcher buttons and translations in Form Builder and Forms.<br/>
<code>https://github.com/novacode-nl/odoo-formio/issues/91</code><br/>
<code>https://github.com/novacode-nl/odoo-formio/issues/93</code>
</li>
</ul>
<h4 class="oe_mb16">8.1</h4>
<ul>
<li>
Expand All @@ -184,7 +192,7 @@ <h4 class="oe_mb16">8.1</h4>
<code>https://github.com/novacode-nl/odoo-formio/issues/93</code>
</li>
<li>
Add translation pt_BR: Portuguese (BR) / Portugu&ecirc;s (BR)
Add translations pt_BR &rarr; Portuguese (BR) / Portugu&ecirc;s (BR)<br/>
</li>
</ul>
<h4 class="oe_mb16">8.0</h4>
Expand Down
1 change: 1 addition & 0 deletions formio/static/src/js/builder/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class App extends Component {
builder.instance.ready.then(function() {
if ('language' in self.options) {
builder.language = self.options['language'];
// builder.instance.webform.language = self.options['language'];
}
window.setLanguage = function(lang) {
builder.instance.webform.language = lang;
Expand Down
2 changes: 1 addition & 1 deletion formio/views/formio_builder_templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ See LICENSE file for full licensing details. -->
<t t-if="len(languages) > 1">
<div class="formio_languages">
<t t-foreach="languages" t-as="lang">
<button class="btn btn-sm btn-default" t-attf-onclick="setLanguage('{{ lang.iso_code }}')"><span t-field="lang.name"/></button>
<button class="btn btn-sm btn-default" t-attf-onclick="setLanguage('{{ lang.code }}')"><span t-field="lang.name"/></button>
</t>
</div>
</t>
Expand Down
3 changes: 3 additions & 0 deletions formio/views/formio_builder_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ See LICENSE file for full licensing details. -->
</group>
</page>
<page string="Translations" name="translations" groups="formio.group_formio_admin">
<group>
<field name="language_en_enable"/>
</group>
<field name="translations" context="{'default_builder_id': id}">
<tree editable="bottom">
<field name="builder_id" invisible="1"/>
Expand Down
2 changes: 1 addition & 1 deletion formio/views/formio_form_templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ See LICENSE file for full licensing details. -->
<t t-if="len(languages) > 1">
<div class="formio_languages">
<t t-foreach="languages" t-as="lang">
<button class="btn btn-sm btn-default" t-attf-onclick="setLanguage('{{ lang.iso_code }}')"><span t-field="lang.name"/></button>
<button class="btn btn-sm btn-default" t-attf-onclick="setLanguage('{{ lang.code }}')"><span t-field="lang.name"/></button>
</t>
</div>
</t>
Expand Down
4 changes: 2 additions & 2 deletions formio/views/formio_public_templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ See LICENSE file for full licensing details. -->
<t t-if="len(languages) > 1">
<div class="formio_languages">
<t t-foreach="languages" t-as="lang">
<button class="btn btn-sm btn-default" t-attf-onclick="setLanguage('{{ lang.iso_code }}')"><span t-field="lang.name"/></button>
<button class="btn btn-sm btn-default" t-attf-onclick="setLanguage('{{ lang.code }}')"><span t-field="lang.name"/></button>
</t>
</div>
</t>
Expand Down Expand Up @@ -100,7 +100,7 @@ See LICENSE file for full licensing details. -->
<t t-if="len(languages) > 1">
<div class="formio_languages">
<t t-foreach="languages" t-as="lang">
<button class="btn btn-sm btn-default" t-attf-onclick="setLanguage('{{ lang.iso_code }}')"><span t-field="lang.name"/></button>
<button class="btn btn-sm btn-default" t-attf-onclick="setLanguage('{{ lang.code }}')"><span t-field="lang.name"/></button>
</t>
</div>
</t>
Expand Down

0 comments on commit 62c249d

Please sign in to comment.