From 9b649284ef4952bc3d2ec8b86def3cd412bdd056 Mon Sep 17 00:00:00 2001 From: Svante Richter Date: Wed, 13 Apr 2016 14:28:22 +0200 Subject: [PATCH] Better way to handle translation of forms The old way is deprecated and will be removed in 1.0 --- README.md | 8 +-- .../twig/boltforms_theme_translated.twig | 69 +++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 assets/views/twig/boltforms_theme_translated.twig diff --git a/README.md b/README.md index 8f67397..52f282a 100755 --- a/README.md +++ b/README.md @@ -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. diff --git a/assets/views/twig/boltforms_theme_translated.twig b/assets/views/twig/boltforms_theme_translated.twig new file mode 100644 index 0000000..9b7cc49 --- /dev/null +++ b/assets/views/twig/boltforms_theme_translated.twig @@ -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 -%} + +{%- endblock choice_widget_collapsed -%} + +{%- block choice_widget_options -%} + {% for group_label, choice in options %} + {%- if choice is iterable -%} + + {% set options = choice %} + {{- block('choice_widget_options') -}} + + {%- else -%} + {% set attr = choice.attr %} + + {%- 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 -%} + +{%- 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 -%} + {{ app.request.get('_locale') is null ? label : l(label) }} + {%- endif -%} +{%- endblock form_label -%}