Bug 38630 - Make the REST API respect KohaOpacLanguage cookie
Summary: Make the REST API respect KohaOpacLanguage cookie
Status: Pushed to main
Alias: None
Product: Koha
Classification: Unclassified
Component: I18N/L10N (show other bugs)
Version: Main
Hardware: All All
: P5 - low normal
Assignee: Julian Maurice
QA Contact: Marcel de Rooy
URL:
Keywords:
Depends on: 32551
Blocks: 38903
  Show dependency treegraph
 
Reported: 2024-12-05 11:22 UTC by Julian Maurice
Modified: 2025-04-10 00:33 UTC (History)
5 users (show)

See Also:
Change sponsored?: ---
Patch complexity: Small patch
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
25.05.00
Circulation function:


Attachments
Bug 38630: Make the REST API respect KohaOpacLanguage cookie (15.52 KB, patch)
2024-12-05 11:23 UTC, Julian Maurice
Details | Diff | Splinter Review
Bug 38630: Make the REST API respect KohaOpacLanguage cookie (15.57 KB, patch)
2025-01-08 11:07 UTC, David Nind
Details | Diff | Splinter Review
Bug 38630: Make the REST API respect KohaOpacLanguage cookie (15.66 KB, patch)
2025-01-16 10:33 UTC, Marcel de Rooy
Details | Diff | Splinter Review
Bug 38630: (QA follow-up) Add copyright, license and/or POD (2.25 KB, patch)
2025-01-16 10:33 UTC, Marcel de Rooy
Details | Diff | Splinter Review
Bug 38630: (QA follow-up) Two typos (1.50 KB, patch)
2025-01-16 10:33 UTC, Marcel de Rooy
Details | Diff | Splinter Review
Bug 38630: (QA follow-up) Fixing few calls of getTranslatedLanguages (4.93 KB, patch)
2025-01-16 10:33 UTC, Marcel de Rooy
Details | Diff | Splinter Review
Bug 38630: Make the REST API respect KohaOpacLanguage cookie (15.70 KB, patch)
2025-01-28 11:48 UTC, Tomás Cohen Arazi (tcohen)
Details | Diff | Splinter Review
Bug 38630: (QA follow-up) Add copyright, license and/or POD (2.27 KB, patch)
2025-01-28 11:48 UTC, Tomás Cohen Arazi (tcohen)
Details | Diff | Splinter Review
Bug 38630: (QA follow-up) Two typos (1.52 KB, patch)
2025-01-28 11:48 UTC, Tomás Cohen Arazi (tcohen)
Details | Diff | Splinter Review
Bug 38630: (QA follow-up) Fixing few calls of getTranslatedLanguages (4.95 KB, patch)
2025-01-28 11:48 UTC, Tomás Cohen Arazi (tcohen)
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Julian Maurice 2024-12-05 11:22:51 UTC
Continuation of bug 32551
Comment 1 Julian Maurice 2024-12-05 11:23:44 UTC
Created attachment 175238 [details] [review]
Bug 38630: Make the REST API respect KohaOpacLanguage cookie

The REST API can return translated strings to the opac or staff
interface, so it should respect the KohaOpacLanguage cookie and return
translations in the same language than the interface.

Test plan:
1. Do not apply patch yet
2. Log in to koha staff interface
3. Use the browser's devtools to get the value of CGISESSID cookie
4. Make sure you have at least 2 languages installed ('en' + another
   one - I'll use 'fr-FR' as an example in this test plan)
5. Find an item and note its itemnumber and itemtype
6. Go translate this itemtype's description (admin » item types » edit
   item type » translate). Add a translation for 'en' and another one
   for your language.
7. Close your browser tab (do not logout) and restart Koha to flush the
   memory cache.
8. Execute the following commands (replace variables by the correct
   value):

    curl -sSL "$KOHA_URL/api/v1/items/$ITEMNUMBER" \
        -H 'x-koha-embed: +strings' \
        -H "Cookie: KohaOpacLanguage=en; CGISESSID=$CGISESSID" \
        | jq -r '._strings.item_type_id.str'

   If you do not have jq, it can be found in most distributions
   packages. Or you could inspect the JSON manually at the path
   /_strings/item_type_id/str

   Restart Koha again, and execute the same command but with your
   language set in KohaOpacLanguage cookie:

    curl -sSL "$KOHA_URL/api/v1/items/$ITEMNUMBER" \
        -H 'x-koha-embed: +strings' \
        -H "Cookie: KohaOpacLanguage=fr-FR; CGISESSID=$CGISESSID" \
        | jq -r '._strings.item_type_id.str'

   Depending on how Koha sort the languages (not sure how it's done),
   you should get either the 'en' one or the 'fr-FR' one, but the
   important thing is you should get the same translation, no matter
   what the value of KohaOpacLanguage is, which is the issue this patch
   is trying to fix.
9. Apply the patch and restart Koha
10. Repeat step 8. This time you should get the translation
    corresponding to the language set in KohaOpacLanguage cookie

Side note:
* The need to restart Koha before each request is due to a bug (see bug
  38454). If already pushed when you're testing this one, you don't have
  to restart.
Comment 2 David Nind 2025-01-03 19:03:09 UTC
I had a go at testing, but didn't have any luck with step 8.

1. Are these commands issued from within the KTD shell, or your local machine? I tried both (with a restart after each command).

2. I get null as the result:

curl -sSL "127.0.0.1:8081/api/v1/items/578" \
-H 'x-koha-embed: +strings' \
-H "Cookie: KohaOpacLanguage=en; CGISESSID=f44da5bb4f1805fced9004f8dab49160" \
| jq -r '._strings.item_type_id.str'
null

3. The session id keeps on changing. If I go to access the staff interface again, I get session time out, please log in again, and the session id is different (as expected ?).

Testing notes (using KTD):
1. Step 3: 
   - Chromium-based browsers: More tools > Developer tools > Application > Storage > Cookies > [Select website] > CGISESSID
2. Step 4:
   - Install a translation: koha-translate --install fr-FR --dev kohadev
   - Koha administration > System preferences > I18/L10N > OPACLanguages and StaffInterfaceLanguages [select languages to enable], opaclanguagedisplay - allow
3. Step 8, from within KTD (also tried from local machine):
   - sudo apt-get install jq
   - first execution - result = null:
curl -sSL "127.0.0.1:8081/api/v1/items/578" \
-H 'x-koha-embed: +strings' \
-H "Cookie: KohaOpacLanguage=en; CGISESSID=f44da5bb4f1805fced9004f8dab49160" \
| jq -r '._strings.item_type_id.str'
null
Comment 3 Julian Maurice 2025-01-06 07:27:37 UTC
(In reply to David Nind from comment #2)
> I had a go at testing, but didn't have any luck with step 8.
> 
> 1. Are these commands issued from within the KTD shell, or your local
> machine? I tried both (with a restart after each command).
They should be run from the same machine that is running your browser (so probably your local machine)

> 2. I get null as the result:
> 
> curl -sSL "127.0.0.1:8081/api/v1/items/578" \
> -H 'x-koha-embed: +strings' \
> -H "Cookie: KohaOpacLanguage=en; CGISESSID=f44da5bb4f1805fced9004f8dab49160"
> \
> | jq -r '._strings.item_type_id.str'
> null
Can you try to remove the '| jq ...' part to see the full JSON response ?

> 3. The session id keeps on changing. If I go to access the staff interface
> again, I get session time out, please log in again, and the session id is
> different (as expected ?).
Maybe you have SessionRestrictionByIP enabled and running the command from
within KTD logged you out
Comment 4 David Nind 2025-01-08 11:07:19 UTC
Created attachment 176226 [details] [review]
Bug 38630: Make the REST API respect KohaOpacLanguage cookie

The REST API can return translated strings to the opac or staff
interface, so it should respect the KohaOpacLanguage cookie and return
translations in the same language than the interface.

Test plan:
1. Do not apply patch yet
2. Log in to koha staff interface
3. Use the browser's devtools to get the value of CGISESSID cookie
4. Make sure you have at least 2 languages installed ('en' + another
   one - I'll use 'fr-FR' as an example in this test plan)
5. Find an item and note its itemnumber and itemtype
6. Go translate this itemtype's description (admin » item types » edit
   item type » translate). Add a translation for 'en' and another one
   for your language.
7. Close your browser tab (do not logout) and restart Koha to flush the
   memory cache.
8. Execute the following commands (replace variables by the correct
   value):

    curl -sSL "$KOHA_URL/api/v1/items/$ITEMNUMBER" \
        -H 'x-koha-embed: +strings' \
        -H "Cookie: KohaOpacLanguage=en; CGISESSID=$CGISESSID" \
        | jq -r '._strings.item_type_id.str'

   If you do not have jq, it can be found in most distributions
   packages. Or you could inspect the JSON manually at the path
   /_strings/item_type_id/str

   Restart Koha again, and execute the same command but with your
   language set in KohaOpacLanguage cookie:

    curl -sSL "$KOHA_URL/api/v1/items/$ITEMNUMBER" \
        -H 'x-koha-embed: +strings' \
        -H "Cookie: KohaOpacLanguage=fr-FR; CGISESSID=$CGISESSID" \
        | jq -r '._strings.item_type_id.str'

   Depending on how Koha sort the languages (not sure how it's done),
   you should get either the 'en' one or the 'fr-FR' one, but the
   important thing is you should get the same translation, no matter
   what the value of KohaOpacLanguage is, which is the issue this patch
   is trying to fix.
9. Apply the patch and restart Koha
10. Repeat step 8. This time you should get the translation
    corresponding to the language set in KohaOpacLanguage cookie

Side note:
* The need to restart Koha before each request is due to a bug (see bug
  38454). If already pushed when you're testing this one, you don't have
  to restart.

Signed-off-by: David Nind <david@davidnind.com>
Comment 5 David Nind 2025-01-08 11:09:03 UTC
(In reply to Julian Maurice from comment #3)
> (In reply to David Nind from comment #2)

> Maybe you have SessionRestrictionByIP enabled and running the command from
> within KTD logged you out

I turned off SessionRestrictionByIP and StaffLoginLibraryBasedOnIP (both enabled by default in KTD), and everything now works as per the test plan.

Thanks Julian!
Comment 6 Marcel de Rooy 2025-01-10 10:16:57 UTC
Looking here
Comment 7 Marcel de Rooy 2025-01-10 10:23:13 UTC
The tidy stuff and a POD failure:

 WARN   C4/Languages.pm
   WARN   tidiness
                The file is less tidy than before (bad/messy lines before: 210, now: 212)

 FAIL   Koha/Language.pm
   FAIL   pod coverage
                POD is missing for get_requested_language
                POD is missing for set_requested_language
   WARN   tidiness
                The file is less tidy than before (bad/messy lines before: 0, now: 2)

 WARN   t/Languages.t
   WARN   tidiness
                The file is less tidy than before (bad/messy lines before: 10, now: 16)

 WARN   t/db_dependent/api/v1/items.t
   WARN   tidiness
                The file is less tidy than before (bad/messy lines before: 180, now: 184)
Comment 8 Katrin Fischer 2025-01-10 10:26:54 UTC
(In reply to Marcel de Rooy from comment #7)
> The tidy stuff and a POD failure:
> 
>  WARN   C4/Languages.pm
>    WARN   tidiness
>                 The file is less tidy than before (bad/messy lines before:
> 210, now: 212)
> 
>  FAIL   Koha/Language.pm
>    FAIL   pod coverage
>                 POD is missing for get_requested_language
>                 POD is missing for set_requested_language
>    WARN   tidiness
>                 The file is less tidy than before (bad/messy lines before:
> 0, now: 2)
> 
>  WARN   t/Languages.t
>    WARN   tidiness
>                 The file is less tidy than before (bad/messy lines before:
> 10, now: 16)
> 
>  WARN   t/db_dependent/api/v1/items.t
>    WARN   tidiness
>                 The file is less tidy than before (bad/messy lines before:
> 180, now: 184)

As we plan to tidy the whole codebase quite soon (January/February), I think you can ignore the tidy warnings for now. POD should be checked of course.
Comment 9 Marcel de Rooy 2025-01-10 10:59:01 UTC
Will continue on this report early next week. No need to change status.
Comment 10 Marcel de Rooy 2025-01-16 08:16:51 UTC
This reminds me of abandoned work on bug 29245 (from 2021) btw
Comment 11 Marcel de Rooy 2025-01-16 08:20:38 UTC
Changing Component. Holidays does not cover the load well :)
Comment 12 Marcel de Rooy 2025-01-16 10:33:15 UTC
Created attachment 176617 [details] [review]
Bug 38630: Make the REST API respect KohaOpacLanguage cookie

The REST API can return translated strings to the opac or staff
interface, so it should respect the KohaOpacLanguage cookie and return
translations in the same language than the interface.

Test plan:
1. Do not apply patch yet
2. Log in to koha staff interface
3. Use the browser's devtools to get the value of CGISESSID cookie
4. Make sure you have at least 2 languages installed ('en' + another
   one - I'll use 'fr-FR' as an example in this test plan)
5. Find an item and note its itemnumber and itemtype
6. Go translate this itemtype's description (admin » item types » edit
   item type » translate). Add a translation for 'en' and another one
   for your language.
7. Close your browser tab (do not logout) and restart Koha to flush the
   memory cache.
8. Execute the following commands (replace variables by the correct
   value):

    curl -sSL "$KOHA_URL/api/v1/items/$ITEMNUMBER" \
        -H 'x-koha-embed: +strings' \
        -H "Cookie: KohaOpacLanguage=en; CGISESSID=$CGISESSID" \
        | jq -r '._strings.item_type_id.str'

   If you do not have jq, it can be found in most distributions
   packages. Or you could inspect the JSON manually at the path
   /_strings/item_type_id/str

   Restart Koha again, and execute the same command but with your
   language set in KohaOpacLanguage cookie:

    curl -sSL "$KOHA_URL/api/v1/items/$ITEMNUMBER" \
        -H 'x-koha-embed: +strings' \
        -H "Cookie: KohaOpacLanguage=fr-FR; CGISESSID=$CGISESSID" \
        | jq -r '._strings.item_type_id.str'

   Depending on how Koha sort the languages (not sure how it's done),
   you should get either the 'en' one or the 'fr-FR' one, but the
   important thing is you should get the same translation, no matter
   what the value of KohaOpacLanguage is, which is the issue this patch
   is trying to fix.
9. Apply the patch and restart Koha
10. Repeat step 8. This time you should get the translation
    corresponding to the language set in KohaOpacLanguage cookie

Side note:
* The need to restart Koha before each request is due to a bug (see bug
  38454). If already pushed when you're testing this one, you don't have
  to restart.

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Comment 13 Marcel de Rooy 2025-01-16 10:33:17 UTC
Created attachment 176618 [details] [review]
Bug 38630: (QA follow-up) Add copyright, license and/or POD

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Comment 14 Marcel de Rooy 2025-01-16 10:33:20 UTC
Created attachment 176619 [details] [review]
Bug 38630: (QA follow-up) Two typos

Prefered and chose.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Comment 15 Marcel de Rooy 2025-01-16 10:33:22 UTC
Created attachment 176620 [details] [review]
Bug 38630: (QA follow-up) Fixing few calls of getTranslatedLanguages

The fourth parameter is now removed and was never used.
Also replacing the 'both' interface by passing no interface to make
it consistent with call in additional-contents.
Resolving warn for empty interface or theme on Languages.pm L128.

Test plan:
Run t/db_dependent/Languages.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Comment 16 Julian Maurice 2025-01-20 09:25:31 UTC
Thanks for the followup patches Marcel !
Comment 17 Tomás Cohen Arazi (tcohen) 2025-01-28 11:48:16 UTC
Created attachment 177266 [details] [review]
Bug 38630: Make the REST API respect KohaOpacLanguage cookie

The REST API can return translated strings to the opac or staff
interface, so it should respect the KohaOpacLanguage cookie and return
translations in the same language than the interface.

Test plan:
1. Do not apply patch yet
2. Log in to koha staff interface
3. Use the browser's devtools to get the value of CGISESSID cookie
4. Make sure you have at least 2 languages installed ('en' + another
   one - I'll use 'fr-FR' as an example in this test plan)
5. Find an item and note its itemnumber and itemtype
6. Go translate this itemtype's description (admin » item types » edit
   item type » translate). Add a translation for 'en' and another one
   for your language.
7. Close your browser tab (do not logout) and restart Koha to flush the
   memory cache.
8. Execute the following commands (replace variables by the correct
   value):

    curl -sSL "$KOHA_URL/api/v1/items/$ITEMNUMBER" \
        -H 'x-koha-embed: +strings' \
        -H "Cookie: KohaOpacLanguage=en; CGISESSID=$CGISESSID" \
        | jq -r '._strings.item_type_id.str'

   If you do not have jq, it can be found in most distributions
   packages. Or you could inspect the JSON manually at the path
   /_strings/item_type_id/str

   Restart Koha again, and execute the same command but with your
   language set in KohaOpacLanguage cookie:

    curl -sSL "$KOHA_URL/api/v1/items/$ITEMNUMBER" \
        -H 'x-koha-embed: +strings' \
        -H "Cookie: KohaOpacLanguage=fr-FR; CGISESSID=$CGISESSID" \
        | jq -r '._strings.item_type_id.str'

   Depending on how Koha sort the languages (not sure how it's done),
   you should get either the 'en' one or the 'fr-FR' one, but the
   important thing is you should get the same translation, no matter
   what the value of KohaOpacLanguage is, which is the issue this patch
   is trying to fix.
9. Apply the patch and restart Koha
10. Repeat step 8. This time you should get the translation
    corresponding to the language set in KohaOpacLanguage cookie

Side note:
* The need to restart Koha before each request is due to a bug (see bug
  38454). If already pushed when you're testing this one, you don't have
  to restart.

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 18 Tomás Cohen Arazi (tcohen) 2025-01-28 11:48:19 UTC
Created attachment 177267 [details] [review]
Bug 38630: (QA follow-up) Add copyright, license and/or POD

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 19 Tomás Cohen Arazi (tcohen) 2025-01-28 11:48:22 UTC
Created attachment 177268 [details] [review]
Bug 38630: (QA follow-up) Two typos

Prefered and chose.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 20 Tomás Cohen Arazi (tcohen) 2025-01-28 11:48:24 UTC
Created attachment 177269 [details] [review]
Bug 38630: (QA follow-up) Fixing few calls of getTranslatedLanguages

The fourth parameter is now removed and was never used.
Also replacing the 'both' interface by passing no interface to make
it consistent with call in additional-contents.
Resolving warn for empty interface or theme on Languages.pm L128.

Test plan:
Run t/db_dependent/Languages.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 21 Katrin Fischer 2025-01-30 16:45:49 UTC
I had asked for support from topic expert Tomas on this one. Thanks! :)
Comment 22 Katrin Fischer 2025-01-30 17:07:15 UTC
Pushed for 25.05!

Well done everyone, thank you!
Comment 23 Katrin Fischer 2025-04-09 11:50:31 UTC
Can we please consider backport to 24.11?
Comment 24 David Cook 2025-04-10 00:33:30 UTC
This is an interesting one...

I was going to say "It's too bad the KohaOpacLanguage cookie is httponly. Otherwise, we could've used Javascript to read the cookie and then set Accept-Language on the API call."

But that would come with its own complications...