Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for setting labels, hints and legends via Rails' i18n #68

Closed
6 tasks done
peteryates opened this issue Nov 12, 2019 · 3 comments
Closed
6 tasks done

Support for setting labels, hints and legends via Rails' i18n #68

peteryates opened this issue Nov 12, 2019 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@peteryates
Copy link
Member

peteryates commented Nov 12, 2019

Currently all of the helpers require that the text that's passed in to form labels, hints and legends is supplied inline. Many projects prefer to store the text in the locales files and use Rails' i18n to populate the relevant strings.

There's a working implementation in the GOV.UK elements form builder that can be used for inspiration.

  • investigate the best approach for generically retrieving localisation values from Rails' store
  • ensure that strings are chosen in the correct preference order:
    1. provided
    2. localised
    3. automatic
  • work out the best structure to store hints and legends in the locales file
  • document the process in the guide

Additional things to check

  • Should there be translations added for radio buttons and check boxes labels and hints? This might be nice addition and could definitely be useful, but there's already text_method: and :hint_method which probably already do everything that's required 🤔
  • Should there be a shorthand way to specify to the helper which localisation key to use? Rather than doing something like label: { text: I18n.t('my.favourite.type.of.pasta') } perhaps label: { t: 'my.favourite.type.of.pasta' }? 🤷🏽‍♂️
@peteryates peteryates added the enhancement New feature or request label Nov 12, 2019
@peteryates peteryates added this to the Version 1.1 milestone Nov 15, 2019
peteryates added a commit that referenced this issue Nov 16, 2019
The with_localisations method allows us to temporarily set the locale
backend and always cleans up after itself.

Refs #68
peteryates added a commit that referenced this issue Nov 16, 2019
This will pull the correct localisation string from the backend and
display it unless it has been overridden by manually supplying text

Refs #68
peteryates added a commit that referenced this issue Nov 16, 2019
This can now be optionally set and will be used for the retrieval of
localisations from the I18n backend

Refs #68
peteryates added a commit that referenced this issue Nov 16, 2019
Compacting the array and joining the remains can return a hash if it
matches the structure of the backend. For example, "helpers.person.."
will return the other translations for the object. Instead, return nil
unless all parts are present

Refs #68
peteryates added a commit that referenced this issue Nov 16, 2019
@leeky
Copy link

leeky commented Dec 2, 2019

This is a great addition!

Are there plans to make the i18n scopes configurable? For example, I'd like to use the ActiveRecord activerecord.attributes.(model).(attribute_name) scope for labels, because this would make the form labels and validations use the same text.

@peteryates
Copy link
Member Author

Thank you!

I'm in agreement regarding being able to configure it, and generally being able to configure how the builder works is definitely on my radar (mainly for things like setting default label/legend sizes and tags, etc).

The automatic building of the path is very straightforward and should be easily-overridable, but I think individually configuring the structure labels, hints and legends might get a bit complicated.

@peteryates peteryates mentioned this issue Dec 3, 2019
3 tasks
peteryates added a commit that referenced this issue Dec 5, 2019
peteryates added a commit that referenced this issue Dec 7, 2019
CollectionItem will hold functionality that's specific to items
belonging to a collection. Initially, this covers the retrieval of the
value depending on what's passed into the :text_method, :value_method or
:hint_method params.

These arguments now accept and properly deal with procs

Refs #68
peteryates added a commit that referenced this issue Dec 7, 2019
Instead of using `#send` inline to retrieve the value from `@item`, make
use of `#retrieve` which works with procs.

Refs #68
peteryates added a commit that referenced this issue Dec 7, 2019
The :value_method, :text_method and :hint_method params passed to radio
collections and the :hint_method params passed to checkbox collections
now accept procs which can be used to customise the value retrieved from
the item.

This could be used for simple transformations like upcasing the text or
more-complex operations like localising the value, as per the provided
specs

Refs #68
peteryates added a commit that referenced this issue Dec 10, 2019
CollectionItem will hold functionality that's specific to items
belonging to a collection. Initially, this covers the retrieval of the
value depending on what's passed into the :text_method, :value_method or
:hint_method params.

These arguments now accept and properly deal with procs

Refs #68
peteryates added a commit that referenced this issue Dec 10, 2019
Instead of using `#send` inline to retrieve the value from `@item`, make
use of `#retrieve` which works with procs.

Refs #68
peteryates added a commit that referenced this issue Dec 10, 2019
The :value_method, :text_method and :hint_method params passed to radio
collections and the :hint_method params passed to checkbox collections
now accept procs which can be used to customise the value retrieved from
the item.

This could be used for simple transformations like upcasing the text or
more-complex operations like localising the value, as per the provided
specs

Refs #68
@peteryates
Copy link
Member Author

This is now in the 1.1.0 branch

peteryates added a commit that referenced this issue Dec 21, 2019
Add support for customising the localisation schema (provided in #68) as part of the
application config.

The configuration allows for the value localisation_schema_fallback to be set, which
provides a default 'base' schema; and localisation_schema_label, localisation_schema_hint,
and localisation_schema_legend to be set which configures labels, hints and legends
respectively.

One slightly-breaking change (from the initial version of localisation functionality) is
that now we're referring to fieldset legends by legend rather than fieldset. This is
more inkeeping with the language used everywhere else in the project and definitely
feels cleaner.
peteryates added a commit that referenced this issue Jan 9, 2020
The with_localisations method allows us to temporarily set the locale
backend and always cleans up after itself.

Refs #68
peteryates added a commit that referenced this issue Jan 9, 2020
This will pull the correct localisation string from the backend and
display it unless it has been overridden by manually supplying text

Refs #68
peteryates added a commit that referenced this issue Jan 9, 2020
This can now be optionally set and will be used for the retrieval of
localisations from the I18n backend

Refs #68
peteryates added a commit that referenced this issue Jan 9, 2020
Compacting the array and joining the remains can return a hash if it
matches the structure of the backend. For example, "helpers.person.."
will return the other translations for the object. Instead, return nil
unless all parts are present

Refs #68
peteryates added a commit that referenced this issue Jan 9, 2020
peteryates added a commit that referenced this issue Jan 9, 2020
peteryates added a commit that referenced this issue Jan 9, 2020
CollectionItem will hold functionality that's specific to items
belonging to a collection. Initially, this covers the retrieval of the
value depending on what's passed into the :text_method, :value_method or
:hint_method params.

These arguments now accept and properly deal with procs

Refs #68
peteryates added a commit that referenced this issue Jan 9, 2020
Instead of using `#send` inline to retrieve the value from `@item`, make
use of `#retrieve` which works with procs.

Refs #68
peteryates added a commit that referenced this issue Jan 9, 2020
The :value_method, :text_method and :hint_method params passed to radio
collections and the :hint_method params passed to checkbox collections
now accept procs which can be used to customise the value retrieved from
the item.

This could be used for simple transformations like upcasing the text or
more-complex operations like localising the value, as per the provided
specs

Refs #68
peteryates added a commit that referenced this issue Jan 9, 2020
Add support for customising the localisation schema (provided in #68) as part of the
application config.

The configuration allows for the value localisation_schema_fallback to be set, which
provides a default 'base' schema; and localisation_schema_label, localisation_schema_hint,
and localisation_schema_legend to be set which configures labels, hints and legends
respectively.

One slightly-breaking change (from the initial version of localisation functionality) is
that now we're referring to fieldset legends by legend rather than fieldset. This is
more inkeeping with the language used everywhere else in the project and definitely
feels cleaner.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants