Browse Source

Refat. leitura de yaml's para internacionalização

- As leituras dos arquivos subnav.yaml e layouts.yaml foram
  refatoradas para suportar internacionalização.
- Falta fazer neste branch:
  - Mover todos os arquivos [app_label]/layouts.yaml para
    templates/[app_label]/layouts.yaml
  - aplicar a tag {% load i18n %} no início de todo arquivo layouts.yaml
  - aplicar a tag {% trans '' %} em todo texto a ser internacionalizado
  - DICA: deixe os ":" (dois pontos) de fora da tag trans.
pull/507/head
LeandroRoberto 9 years ago
parent
commit
b69ccfdfd7
  1. 3
      sapl/base/templatetags/menus.py
  2. 25
      sapl/crispy_layout_mixin.py
  3. 4
      sapl/templates/parlamentares/layouts.yaml
  4. 4
      sapl/templates/parlamentares/subnav.yaml

3
sapl/base/templatetags/menus.py

@ -43,7 +43,8 @@ def subnav(context, path=None):
A função nativa abaixo busca em todos os Loaders Configurados. A função nativa abaixo busca em todos os Loaders Configurados.
""" """
yaml_template = template.loader.get_template(yaml_path) yaml_template = template.loader.get_template(yaml_path)
menu = yaml.load(open(yaml_template.origin.name, 'r')) rendered = yaml_template.render()
menu = yaml.load(rendered)
resolve_urls_inplace(menu, root_pk, app, context) resolve_urls_inplace(menu, root_pk, app, context)
except: except:
pass pass

25
sapl/crispy_layout_mixin.py

@ -1,12 +1,13 @@
from math import ceil from math import ceil
from os.path import dirname, join from os.path import dirname, join
import rtyaml
from crispy_forms.bootstrap import FormActions from crispy_forms.bootstrap import FormActions
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import HTML, Div, Fieldset, Layout, Submit from crispy_forms.layout import HTML, Div, Fieldset, Layout, Submit
from django import template
from django.utils import formats from django.utils import formats
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
import rtyaml
def heads_and_tails(list_of_lists): def heads_and_tails(list_of_lists):
@ -70,8 +71,8 @@ def get_field_display(obj, fieldname):
elif 'FieldFile' in str(type(value)): elif 'FieldFile' in str(type(value)):
if value: if value:
display = '<a href="{}">{}</a>'.format( display = '<a href="{}">{}</a>'.format(
value.url, value.url,
value.name.split('/')[-1:][0]) value.name.split('/')[-1:][0])
else: else:
display = '' display = ''
else: else:
@ -89,10 +90,8 @@ class CrispyLayoutFormMixin:
return self.model.__name__ return self.model.__name__
def get_layout(self): def get_layout(self):
filename = join( yaml_layout = '%s/layouts.yaml' % self.model._meta.app_config.label
dirname(self.model._meta.app_config.models_module.__file__), return read_layout_from_yaml(yaml_layout, self.layout_key)
'layouts.yaml')
return read_layout_from_yaml(filename, self.layout_key)
@property @property
def fields(self): def fields(self):
@ -146,15 +145,17 @@ class CrispyLayoutFormMixin:
} for legend, rows in heads_and_tails(self.get_layout())] } for legend, rows in heads_and_tails(self.get_layout())]
def read_yaml_from_file(filename): def read_yaml_from_file(yaml_layout):
# TODO cache this at application level # TODO cache this at application level
with open(filename, 'r') as yamlfile: t = template.loader.get_template(yaml_layout)
return rtyaml.load(yamlfile) rendered = t.render()
return rtyaml.load(rendered)
def read_layout_from_yaml(filename, key): def read_layout_from_yaml(yaml_layout, key):
# TODO cache this at application level # TODO cache this at application level
yaml = read_yaml_from_file(filename) yaml = read_yaml_from_file(yaml_layout)
base = yaml[key] base = yaml[key]
def line_to_namespans(line): def line_to_namespans(line):

4
sapl/parlamentares/layouts.yaml → sapl/templates/parlamentares/layouts.yaml

@ -1,6 +1,6 @@
{% load i18n %}
CargoMesa: CargoMesa:
Cargo na Mesa: {% trans 'Cargo na Mesa'%}:
- descricao:10 unico - descricao:10 unico
Legislatura: Legislatura:

4
sapl/templates/parlamentares/subnav.yaml

@ -1,4 +1,5 @@
- title: Início {% load i18n %}
- title: {% trans 'Início' %}
url: parlamentar_detail url: parlamentar_detail
- title: Mandatos - title: Mandatos
url: mandato_list url: mandato_list
@ -6,4 +7,3 @@
url: filiacao_list url: filiacao_list
- title: Dependentes - title: Dependentes
url: dependente_list url: dependente_list

Loading…
Cancel
Save