-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop Cockpit dependency for web translations (#1118)
## Problem - Drop Cockpit dependency for loading the web translations ## Solution - Reimplement translation loading without Cockpit ## Notes - The code still works with the Cockpit backend and can be merged to `master` without breaking anything. - It uses the same process and same translation loading mechanism as Cockpit just without using the Cockpit library. This makes the transition easy and simple. - In the future we can optionally change that (and even use a different framework like i18n-next). - There are some comments marking the code for removal when the Cockpit dependency is fully dropped. ## Testing - Updated unit tests - Tested manually
- Loading branch information
Showing
9 changed files
with
155 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright (c) [2024] SUSE LLC | ||
* | ||
* All Rights Reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of version 2 of the GNU General Public License as published | ||
* by the Free Software Foundation. | ||
* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* This module provides a global "agama" object which can be use from other | ||
* scripts like "po.js". | ||
*/ | ||
|
||
const agama = { | ||
// the current language | ||
language: "en", | ||
}; | ||
|
||
// mapping with the current translations | ||
let translations = {}; | ||
// function used for computing the plural form index | ||
let plural_fn; | ||
|
||
// set the current translations, called from po.js | ||
agama.locale = function locale(po) { | ||
if (po) { | ||
Object.assign(translations, po); | ||
|
||
const header = po[""]; | ||
if (header) { | ||
if (header["plural-forms"]) | ||
plural_fn = header["plural-forms"]; | ||
if (header.language) | ||
agama.language = header.language; | ||
} | ||
} else if (po === null) { | ||
translations = {}; | ||
} | ||
}; | ||
|
||
/** | ||
* get a translation for a singular text | ||
* @param {string} str input text | ||
* @return translated text or the original text if the translation is not found | ||
*/ | ||
agama.gettext = function gettext(str) { | ||
if (translations) { | ||
const translated = translations[str]; | ||
// skip the `null` item in the list generated by cockpit-po-plugin | ||
// TODO: get rid of that later | ||
if (translated?.[1]) return translated[1]; | ||
} | ||
|
||
// fallback, return the original text | ||
return str; | ||
}; | ||
|
||
/** | ||
* get a translation for a plural text | ||
* @param {string} str1 input singular text | ||
* @param {string} strN input plural text | ||
* @param {number} n the actual number which decides whether to use the | ||
* singular or plural form (of which plural form if there are several of them) | ||
* @return translated text or the original text if the translation is not found | ||
*/ | ||
agama.ngettext = function ngettext(str1, strN, n) { | ||
if (translations && plural_fn) { | ||
// plural form translations are indexed by the singular variant | ||
const translation = translations[str1]; | ||
|
||
if (translation) { | ||
const plural_index = plural_fn(n); | ||
|
||
// the plural function either returns direct index (integer) in the plural | ||
// translations or a boolean indicating simple plural form which | ||
// needs to be converted to index 0 (singular) or 1 (plural) | ||
let index = (plural_index === true ? 1 : plural_index || 0); | ||
|
||
// skip the `null` item in the list generated by cockpit-po-plugin | ||
// TODO: get rid of that later | ||
index += 1; | ||
|
||
if (translation[index]) return translation[index]; | ||
} | ||
} | ||
|
||
// fallback, return the original text | ||
return (n === 1) ? str1 : strN; | ||
}; | ||
|
||
// register a global object so it can be accessed from a separate po.js script | ||
window.agama = agama; | ||
|
||
export default agama; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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