Skip to content

Commit

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

Convert the Odoo language code (underscore "_") to Formio JS/i18n
expected IETF code (hyphen "-").

Concerns #91, #93.
  • Loading branch information
bobslee committed Dec 15, 2020
1 parent a29ae0e commit f987700
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 14 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.2',
'version': '8.3',
'license': 'LGPL-3',
'author': 'Nova Code',
'website': 'https://www.novacode.nl',
Expand Down
7 changes: 4 additions & 3 deletions formio/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,12 @@ def form_res_data(self, uuid, **kwargs):
def _get_form_js_options(self, form):
options = form._get_js_options()

# default language
# language
Lang = request.env['res.lang']
if request.env.user.lang in form.languages.mapped('code'):
language = request.env.user.lang
language = Lang._formio_ietf_code(request.env.user.lang)
else:
language = request._context['lang']
language = Lang._formio_ietf_code(request._context['lang'])
options['language'] = language
return options

Expand Down
1 change: 1 addition & 0 deletions formio/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
from . import formio_translation
from . import ir_view
from . import res_config_settings
from . import res_lang
19 changes: 11 additions & 8 deletions formio/models/formio_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,14 @@ def _get_js_options(self):

options['i18n'] = self.i18n_translations()

# default language
# language
Lang = self.env['res.lang']
if self.env.user.lang in self.languages.mapped('code'):
language = self.env.user.lang
language = Lang._formio_ietf_code(self.env.user.lang)
else:
language = self._context['lang']
language = Lang._formio_ietf_code(self._context['lang'])

# only set default language if exist in i18n translations
# only set language if exist in i18n translations
if options['i18n'].get(language):
options['language'] = language

Expand Down Expand Up @@ -398,16 +399,18 @@ def i18n_translations(self):
i18n = {}
# Formio GUI/API translations
for trans in self.formio_version_id.translations:
code = trans.lang_id.formio_ietf_code
if trans.lang_id.code not in i18n:
i18n[trans.lang_id.code] = {trans.property: trans.value}
i18n[code] = {trans.property: trans.value}
else:
i18n[trans.lang_id.code][trans.property] = trans.value
i18n[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:
code = trans.lang_id.formio_ietf_code
if trans.lang_id.code not in i18n:
i18n[trans.lang_id.code] = {trans.source: trans.value}
i18n[code] = {trans.source: trans.value}
else:
i18n[trans.lang_id.code][trans.source] = trans.value
i18n[code][trans.source] = trans.value
return i18n
18 changes: 18 additions & 0 deletions formio/models/res_lang.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright Nova Code (http://www.novacode.nl)
# See LICENSE file for full licensing details.

from odoo import api, fields, models


class ResLang(models.Model):
_inherit = 'res.lang'

formio_ietf_code = fields.Char(compute='_compute_formio_ietf_code', string='IETF Code')

def _compute_formio_ietf_code(self):
for lang in self:
lang.formio_ietf_code = self._formio_ietf_code(lang.code)

@api.model
def _formio_ietf_code(self, code):
return code.replace('_', '-')
9 changes: 9 additions & 0 deletions formio/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ <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.3</h4>
<ul>
<li>
Fix #3: language switcher buttons and translations in Form Builder and Forms.<br/>
Convert the Odoo language code (underscore "_") to Formio JS/i18n expected IETF code (hyphen "-").<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.2</h4>
<ul>
<li>
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.code }}')"><span t-field="lang.name"/></button>
<button class="btn btn-sm btn-default" t-attf-onclick="setLanguage('{{ lang.formio_ietf_code }}')"><span t-field="lang.name"/></button>
</t>
</div>
</t>
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.code }}')"><span t-field="lang.name"/></button>
<button class="btn btn-sm btn-default" t-attf-onclick="setLanguage('{{ lang.formio_ietf_code }}')"><span t-field="lang.name"/></button>
</t>
</div>
</t>
Expand Down

0 comments on commit f987700

Please sign in to comment.