-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Drop Cockpit dependency for web translations #1118
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,9 @@ const copy_files = [ | |
const plugins = [ | ||
new Copy({ patterns: copy_files }), | ||
new Extract({ filename: "[name].css" }), | ||
new CockpitPoPlugin(), | ||
// the wrapper sets the main code called in the po.js files, | ||
// the PO_DATA tag is replaced by the real translation data | ||
new CockpitPoPlugin({ wrapper: "agama.locale(PO_DATA);" }), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: The default wrapper is |
||
new CockpitRsyncPlugin({ dest: packageJson.name }), | ||
development && new ReactRefreshWebpackPlugin({ overlay: false }), | ||
// replace the "process.env.WEBPACK_SERVE" text in the source code by | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍