Skip to content

Commit e80e2c1

Browse files
Merge pull request #606 from SuffolkLITLab/updated-local-translation-explanation
Update translation docs in light of SuffolkLITLab/docassemble-AssemblyLine#1032
2 parents 7d1af46 + fd6d810 commit e80e2c1

3 files changed

Lines changed: 80 additions & 36 deletions

File tree

docs/components/AssemblyLine/language.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ given in the first part of the tuple.
105105

106106
<a id="AssemblyLine.language.get_language_list"></a>
107107

108-
#### get\_language\_list(languages: Optional[List[Tuple[str, str]]] = None, current="", lang\_codes: Optional[List[str]] = None, languages\_path: Optional[str] = None, event\_name="al\_change\_language")
108+
#### get\_language\_list(languages: Optional[List[Tuple[str, str]]] = None, current="", lang\_codes: Optional[List[str]] = None, languages\_path: Optional[str] = None, event\_name="al\_get\_language\_list\_change\_language")
109109

110110
```python
111111
def get_language_list(languages: Optional[List[Tuple[str, str]]] = None,
112112
current="",
113113
lang_codes: Optional[List[str]] = None,
114114
languages_path: Optional[str] = None,
115-
event_name="al_change_language") -> str
115+
event_name="al_get_language_list_change_language") -> str
116116
```
117117

118118
Given a list of language codes, returns
@@ -157,4 +157,3 @@ given in the first part of the tuple.
157157
**Returns**:
158158

159159
- `str` - A string containing the HTML for an unordered inline list item for language selection.
160-

docs/components/AssemblyLine/magic_variables.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -459,18 +459,33 @@ code: |
459459

460460
#### `al_user_language`
461461

462-
The currently selected language for the user. This is typically set automatically
463-
through the language selection interface or URL parameters, but can be set directly
464-
if needed.
462+
The interview-wide language choice. This is typically set automatically through
463+
URL parameters, an early language question, or the inline language list, but can
464+
be set directly if needed.
465+
466+
If `session_local.al_user_language` is set, Assembly Line will use that
467+
browser-session value first and only fall back to `al_user_language` when no
468+
session-local override exists.
465469

466470
#### `al_change_language` (event)
467471

468-
An event triggered when the user clicks a language selection link. The event
469-
handler automatically updates `al_user_language` based on the `lang` action
470-
argument and calls Docassemble's `set_language()` function.
472+
An event triggered when the user clicks a language selection link from the
473+
navigation dropdown. The event handler stores the selected language in
474+
`session_local.al_user_language` and calls Docassemble's `set_language()`
475+
function.
476+
477+
This event is used internally by `get_language_list_dropdown()` so one browser
478+
session can switch languages without changing the interview-wide language for
479+
other users.
480+
481+
#### `al_get_language_list_change_language` (event)
482+
483+
An event triggered by the inline `get_language_list()` helper. The event handler
484+
updates `al_user_language` from the `lang` action argument and calls
485+
Docassemble's `set_language()` function.
471486

472-
This event is used internally by the language switching functions like
473-
`get_language_list_dropdown()` and `get_language_list()`.
487+
Use this behavior when you want the language choice to become the shared,
488+
interview-wide default for the current run of the interview.
474489

475490
### Screen reader control
476491

docs/components/AssemblyLine/translation.md

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,17 @@ all Assembly Line special variables, see [Special variables](magic_variables.md)
8888
* `enable_al_language`: defaults to True, turning it off can partially ensure the language system in AssemblyLine doesn't interfere with existing language systems. It should be relatively easy for authors to migrate to this new system though.
8989
* `al_user_default_language`: can be controlled by interview author, this determines the language when the user makes no selection of their own. Defaults to "en".
9090
* `al_interview_languages`: a list of language codes, presumably ISO-639-1 (Alpha-2), like ["en","es"] etc. The Assembly Line also contains a translation of several common language codes into the native-language version of the language (e.g., `es` is translated as `Español`).
91-
* `al_user_language`: normally set via a dropdown menu or passed as a URL argument, this stores the user's current selected language
91+
* `al_user_language`: normally set via a URL argument, a language question, or the inline language list. This stores the interview-wide language choice.
92+
* `session_local.al_user_language`: an optional browser-session override used by the navigation dropdown. If it is set, the interview will use this value before falling back to `al_user_language`.
93+
94+
## Session-local and interview-wide language choices
95+
96+
Assembly Line now supports two layers of language selection:
97+
98+
1. `al_user_language` is the interview-wide language. Use it when you want the primary user's language choice to remain the default for the interview, such as from a landing page, an early language question, or the inline `get_language_list()` helper.
99+
1. `session_local.al_user_language` is a per-session override. The navigation dropdown uses it so one browser session can switch languages without changing the language another user sees in the same interview.
100+
101+
This is especially useful in multi-user interviews. For example, one signer can keep the interview in Spanish while an advocate or a second signer views the same interview in English.
92102

93103
## Make the translation file available in your interview
94104

@@ -119,14 +129,18 @@ When `al_interview_languages` is defined (which is the default) and has at least
119129
the Assembly Line will add a drop-down menu item in the top right that allows the user
120130
to switch languages at any time.
121131

132+
The drop-down menu now changes language only for the current browser session by setting
133+
`session_local.al_user_language`. This makes it a good fit for live help, advocate review,
134+
and multi-user interviews where different people may need different languages at the same time.
135+
122136
You can customize this drop-down menu with a block like this:
123137

124138
```yaml
125139
---
126140
default screen parts:
127141
navigation bar html: |
128142
% if enable_al_language and len(al_interview_languages) > 1:
129-
${ get_language_list_dropdown(al_interview_languages,current=al_user_language, extra_class="text-dark", icon="fa-solid fa-language fa-xl" ) }
143+
${ get_language_list_dropdown(al_interview_languages, current=session_local.attr("al_user_language") or al_user_language, extra_class="text-dark", icon="fa-solid fa-language fa-xl") }
130144
% endif
131145
```
132146

@@ -145,6 +159,9 @@ rather than directing them to the dropdown menu.
145159

146160
You can call `get_language_list()` to achieve this.
147161

162+
This helper uses the `al_get_language_list_change_language` event by default, so it updates
163+
the interview-wide `al_user_language` instead of the session-local override.
164+
148165
Example:
149166

150167
```yaml
@@ -155,7 +172,8 @@ subquestion: |
155172
### Ask the user a question
156173

157174
If you want to be absolutely sure that the user encounters the language
158-
question, you can directly ask the user a question:
175+
question, you can directly ask the user a question. Setting `al_user_language`
176+
this way updates the interview-wide language choice:
159177

160178
```yaml
161179
id: language
@@ -195,42 +213,54 @@ E.g.,
195213
`https://apps.suffolklitlab.org/start/uptocode/?lang=es`
196214

197215
The interview will launch in the language specified by
198-
the language code (if it is translated into that language)
216+
the language code (if it is translated into that language).
217+
218+
This sets the interview-wide `al_user_language`, so it is still the best choice
219+
for language-specific landing pages and other entry points where you want the
220+
language choice to be sticky.
199221

200222
:::note
201223
If the URL already has a `?` in it, replace the `?` with an `&`.
202224

203225
(this is a standard part of URL arguments).
204226
:::
205227

206-
## The `al_change_language` event
228+
## The `al_change_language` and `al_get_language_list_change_language` events
207229

208-
The Assembly Line language system uses a special event called `al_change_language`
209-
to handle language switching. This event is triggered automatically when a user
210-
clicks on a language selection link generated by functions like
211-
`get_language_list_dropdown()` or `get_language_list()`.
230+
Assembly Line now uses two language-switching events:
212231

213-
When the event is triggered, it:
232+
1. `al_change_language` is used by `get_language_list_dropdown()`. It stores the selected language in `session_local.al_user_language`, so the change only affects the current browser session.
233+
1. `al_get_language_list_change_language` is used by `get_language_list()`. It updates `al_user_language`, so the change becomes the interview-wide language choice.
214234

215-
1. Reads the `lang` parameter from the action arguments
216-
2. Sets `al_user_language` to the selected language code
217-
3. Calls Docassemble's `set_language()` function to apply the language change
235+
On each request, the built-in `initial: True` block checks for `session_local.attr("al_user_language")`
236+
first. If that value exists, Assembly Line applies it. Otherwise it falls back to `al_user_language`.
218237

219-
You normally don't need to handle this event directly, as it's managed automatically
220-
by the language switching functions. However, if you want to add custom behavior
221-
when languages are switched, you can define additional logic after the language
222-
change:
238+
You normally do not need to define either event yourself, but this is the current built-in logic:
223239

224240
```yaml
241+
---
242+
initial: True
243+
code: |
244+
if enable_al_language:
245+
if session_local.attr("al_user_language"):
246+
set_language(session_local.attr("al_user_language"))
247+
else:
248+
set_language(al_user_language)
249+
process_action()
250+
---
225251
event: al_change_language
226252
code: |
227-
# The built-in language switching happens first
228-
if 'lang' in action_arguments():
229-
al_user_language = action_argument('lang')
253+
# Set browser-specific language for this interview session
254+
if "lang" in action_arguments():
255+
session_local.al_user_language = action_argument("lang")
256+
set_language(session_local.al_user_language)
257+
---
258+
event: al_get_language_list_change_language
259+
code: |
260+
# Set the interview-wide language
261+
if "lang" in action_arguments():
262+
al_user_language = action_argument("lang")
230263
set_language(al_user_language)
231-
232-
# Add your custom logic here
233-
log(f"User switched to language: {al_user_language}")
234264
```
235265

236266
## A complete example
@@ -253,7 +283,7 @@ default screen parts:
253283
# Customizing this section isn't required. Here
254284
navigation bar html: |
255285
% if enable_al_language and len(al_interview_languages) > 1:
256-
${ get_language_list_dropdown(al_interview_languages,current=al_user_language, extra_class="text-dark", icon="fa-solid fa-language fa-xl" ) }
286+
${ get_language_list_dropdown(al_interview_languages, current=session_local.attr("al_user_language") or al_user_language, extra_class="text-dark", icon="fa-solid fa-language fa-xl") }
257287
% endif
258288
---
259289
sections:
@@ -316,4 +346,4 @@ Also, see the documentation for the [AL language module](language.md) for comple
316346

317347
* [MADE](https://gbls.org/MADE), ([GitHub source code](https://github.com/GBLS/docassemble-maevictiondefense))
318348
* [UpToCode](https://getuptocode.org), ([GitHub source code][https://github.com/LemmaLegalConsulting/docassemble-HousingCodeChecklist])
319-
* [Massachusetts 209A Abuse Prevention Petition](https://courtformsonline.org/dv/#209A), ([GitHub source code](https://github.com/suffolklitlab/docassemble-MA209AProtectiveOrder))
349+
* [Massachusetts 209A Abuse Prevention Petition](https://courtformsonline.org/dv/#209A), ([GitHub source code](https://github.com/suffolklitlab/docassemble-MA209AProtectiveOrder))

0 commit comments

Comments
 (0)