Add Mobility gem and configure text fields to be translatable#2272
Merged
Conversation
cf7e8b8 to
b8c421f
Compare
801c7e8 to
7efde9f
Compare
thomasiles
reviewed
Oct 15, 2025
thomasiles
reviewed
Oct 15, 2025
7efde9f to
c299a98
Compare
thomasiles
reviewed
Oct 17, 2025
c299a98 to
3431900
Compare
Configures the Mobility gem to fall back to English and to generate locale accessor methods (e.g. `question_text_en`) for translated fields.
We use Condition#exit_page_markdown to check if the condition is an exit page. In our tests setup we set exit_page_markdown to "" and check the result of #exit_page?. Before adding the mobility gem, this test passed. Adding mobility translations causes the test to fail because "" is normalised to nil. The translation is set to presence: false so that empty strings are preserved. We might need to be careful about similar issues in the future. We may also want to explicitly check the English version of the translation to ensure the English and welsh versions are consistent.
This commit adds tests for the translations of the form model. We might not want to do this for every attribute, but I found an issue when manually setting the name of a form.
In the Page model, the jsonb answer_settings column is used to store settings for the page, such as the selection options for a selection page. The page model uses the DataStructType when loading and saving the answers from the database. Answer_settings is part of the translations for the page, so we need to make the same change to the answer_settings attribute for the translations. Extending from the Mobility dynamically creates a new model class for the translations. https://www.rubydoc.info/gems/mobility/Mobility/Backends/Table We open the translation model class and add the answer_settings attribute after this.
Add tests for Page translations. We take to set specific values for answer_settings and answer_type in the translations. answer_type has validation with a limited set of values. answer_settings is a json hash converted to a DataStruct object.
We set the form_slug of form when the name is set. Form_name is translated, but form_slug stays in English regardless of locale. Previously we were setting [:form_slug] directly, which meant that form_slug was being set to the Welsh text for both English and Welsh if the Welsh name of the form changed. This commit changes the way we set form_slug, to always use the English name to generate the slug.
3431900 to
2afa0dc
Compare
|
|
🎉 A review copy of this PR has been deployed! You can reach it at: https://pr-2272.admin.review.forms.service.gov.uk/ It may take 5 minutes or so for the application to be fully deployed and working. If it still isn't ready For the sign in details and more information, see the review apps wiki page. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



What problem does this pull request solve?
Trello card: https://trello.com/c/WvPnJSu4/2612-backend-parts-of-option-2-for-welsh-feature-add-mobility-gem-and-add-tables-for-welsh-content
Add Mobility Gem
This PR adds the mobility gem for storing Welsh translations. It configures the
gem to use the table backend and contains migrations to add translations to Form, Page and Condition.
See ADR 46 for more context on the table backend, ADR 43 for more on the gem and other backend strategies and ADR 44 for a high-level overview for how welsh forms will be to form fillers.
This PR doesn't include any UI to edit translations, or any logic to update form documents in other languages.
Merging this PR should not affect any existing forms or any features.
A high-level overview of the Mobility gem
Mobility is a gem which allows you to store translations for your models. It's configurable to use different storage methods (known as backends) and behaviours for accessing translations.
We're using the table backend, which stores translations in a separate table alongside the main model table.
For example, the translations for a Form model would be stored in a table called
form_translations.This table contains columns for each attribute being translated, and a
localecolumn to store the locale of the translation.Mobility adds associations, attribute accessors and other helpers to your models.
You can test this PR by starting the rails console and accessing attributes on a form.
To get the name of a form:
To get the Welsh name of a form:
You can also use the _cy suffix to get and set the Welsh name of a form:
To create a form document using the welsh translations:
Translated attributes
The following attributes are translated:
Forms
name
privacy_policy_url
support_email
support_phone
support_url
support_url_text
declaration_text
what_happens_next_markdown
payment_url
Pages
question_text
hint_text
answer_settings
page_heading
guidance_markdown
Conditions
answer_value
exit_page_markdown
exit_page_heading
We are adding translations for answer_type and answer_settings.
Answer_type is a string which represents the type of answer, for example "selection" or "text" or "number".
Including a translation means that we can have a different answer type for Welsh and English, although that may cause issues when filling in a form.
Answer_settings is a hash which contains the settings for the answer type.
For example, for a selection answer type, the answer settings will contain a list of options.
Have a translation for answer_settings means that we can have different options for selections in Welsh and English.
Things to consider when reviewing