Skip to content

Commit

Permalink
Better way to handle translation of forms
Browse files Browse the repository at this point in the history
The old way is deprecated and will be removed in 1.0
  • Loading branch information
SvanteRichter committed Apr 13, 2016
1 parent 0c09b7c commit 9b64928
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ preferred default locale, a full example is at the bottom of this file:
```

6. Use the `localeswitcher` twig-function to render a locale switcher in your
theme: `{{ localeswitcher()|raw }}` or `{{ localeswitcher('_my_localeswitcher_template.twig')|raw }}`
theme: `{{ localeswitcher() }}` or `{{ localeswitcher('_my_localeswitcher_template.twig') }}`
7. Activate/install the labels extension, set your languages in it's config
and mark any hardcoded text in your templates with `{{l("Your text here")}}`.
8. Translate your boltforms by adding `{% set form = translate_form(form) %}`
at the top of a form template. This requires the labels extension. (the current
solution is very hacky, WIP)
8. Translate your boltforms by switching `{% form_theme form 'boltforms_custom.twig' %}`
to `{% form_theme form 'twig/boltforms_theme_translated.twig' %}` at the top of
a form template. This requires the labels extension.
9. If you use the menueditor extension it will automatically add fields for
translated labels, use these by adding `{{ item[app.request.get('_locale')~'label'] ?: item.label|default('-') }}`
in your menu template.
Expand Down
69 changes: 69 additions & 0 deletions assets/views/twig/boltforms_theme_translated.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{%- block choice_widget_collapsed -%}
{%- if required and placeholder is none and not placeholder_in_choices and not multiple and (attr.size is not defined or attr.size <= 1) -%}
{% set required = false %}
{%- endif -%}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
{%- if placeholder is not none -%}
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder != '' ? (app.request.get('_locale') is null ? placeholder : l(placeholder)) }}</option>
{%- endif -%}
{%- if preferred_choices|length > 0 -%}
{% set options = preferred_choices %}
{{- block('choice_widget_options') -}}
{%- if choices|length > 0 and separator is not none -%}
<option disabled="disabled">{{ separator }}</option>
{%- endif -%}
{%- endif -%}
{%- set options = choices -%}
{{- block('choice_widget_options') -}}
</select>
{%- endblock choice_widget_collapsed -%}

{%- block choice_widget_options -%}
{% for group_label, choice in options %}
{%- if choice is iterable -%}
<optgroup label="{{ choice_translation_domain is same as(false) ? group_label : group_label|trans({}, choice_translation_domain) }}">
{% set options = choice %}
{{- block('choice_widget_options') -}}
</optgroup>
{%- else -%}
{% set attr = choice.attr %}
<option value="{{ choice.value }}" {{ block('attributes') }}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ app.request.get('_locale') is null ? choice.label : l(choice.label) }}</option>
{%- endif -%}
{% endfor %}
{%- endblock choice_widget_options -%}

{%- block button_widget -%}
{%- if label is empty -%}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
'%id%': id,
}) %}
{%- else -%}
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{{ app.request.get('_locale') is null ? label : l(label) }}</button>
{%- endblock button_widget -%}

{%- block form_label -%}
{% if label is not same as(false) -%}
{% if not compound -%}
{% set label_attr = label_attr|merge({'for': id}) %}
{%- endif -%}
{% if required -%}
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
{%- endif -%}
{% if label is empty -%}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
'%id%': id,
}) %}
{%- else -%}
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ app.request.get('_locale') is null ? label : l(label) }}</label>
{%- endif -%}
{%- endblock form_label -%}

0 comments on commit 9b64928

Please sign in to comment.