agama/web/src/agama.test.ts
Ladislav Slezák 6375ae0345
Correctly handle the "zh-Hans" language (bsc#1238584) (#2314)
## Problem

- The storage page crashes after selecting Chinese language ("zh-Hans")
- https://bugzilla.suse.com/show_bug.cgi?id=1238584
- A similar problem exists also with the `pt_BR` language as it also
uses a country code. (All other languages use just a simple language
code without any separator.) But it is less serious as the `pt_BR`
locale is known by glibc so we do not need to do any additional
conversions besides using the correct language separator.
- The underlying problem is that Linux and the Web (browsers) use two
different standards for identifying locales and languages: POSIX locale
identifiers such as `zh_TW.UTF-8` and IETF language tags such as
`zh-Hant`. Agama sometimes uses a punctuation-only conversion which only
works for some cases.

## Solution

- Change the `zh_Hans` in Weblate to `zh_CN` so we do not need to do any
conversions
- Change the web frontend to consistently use the dash (`-`) separator
in all places, avoid unnecessary conversions, convert to underscore only
when sending to the backend
- Convert the underscore (`_`) separator to dash (`-`) when importing
the frontend translations from Weblate
- Handle exceptions in the `Intl.ListFormat` formatting function, in
case of exception fallback to a simple formatting function with comma
separator (", ")

## Notes

- It turned out that actually only the `C.UTF-8` and `en_US.UTF-8`
locales were present in the Live ISO. Unfortunately the filtering regexp
was not updated after renaming the PO translation files from
`po.<lang>.js` to `po-<lang>.js`. 😱
- I have changed the code to read the supported languages from the
`languages.json` file. That allows more precise filtering. In the past
we kept e.g. all `de_DE`, `de_AT`, `de_CH` for German language. Now we
know that after selecting German the `de_DE` locale will be used so we
can delete the `de_AT` and `de_CH` locales from the live ISO.

## Testing

- Added a new unit test
- Tested manually
- The web frontend does not crash a properly displays the formatted
text, the `Intl.ListFormat` function is used in the `New partitions will
be created for "/" <and> "swap"` text
  - The backend receives a valid Linux locale (`zh_CN.UTF-8`)
  - The Web UI translation files use the dash separator
  - The Live ISO contains all supported locales (`locale -a`)


![agama-zh-CN](https://github.com/user-attachments/assets/3c8876c6-c3de-41e6-ad35-e6aff3a499d5)


![image](https://github.com/user-attachments/assets/a9afd37a-243c-445c-8808-de5d39b49947)

---------

Co-authored-by: YaST Bot <yast-devel@opensuse.org>
Co-authored-by: Martin Vidner <mvidner@suse.com>
2025-05-07 10:59:23 +02:00

47 lines
1.6 KiB
TypeScript

/*
* Copyright (c) [2025] SUSE LLC
*
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, contact SUSE LLC.
*
* To contact SUSE LLC about this file by physical or electronic mail, you may
* find current contact information at www.suse.com.
*/
import agama from "~/agama";
describe("agama", () => {
describe("formatList", () => {
afterEach(() => {
// restore the default language
agama.language = "en";
});
it("returns localized list", () => {
agama.language = "zh-CN";
const list = agama.formatList(["1", "2", "3"], {});
expect(list).toEqual("1、2和3");
});
it("it fallbacks to a simple formatting when the localized function fails", () => {
agama.language = "invalid:language";
// disable the console logging in this test, a failure is expected so do
// not mess the output with a false alarm
jest.spyOn(console, "warn").mockImplementation();
const list = agama.formatList(["1", "2", "3"], {});
expect(list).toEqual("1, 2, 3");
});
});
});