Skip to content

Commit

Permalink
refactor(web): migrate src/agama to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed Dec 26, 2024
1 parent 760a780 commit dc0d491
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 106 deletions.
99 changes: 0 additions & 99 deletions web/src/agama.js

This file was deleted.

99 changes: 99 additions & 0 deletions web/src/agama.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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 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.
*/

/**
* This module provides a global "agama" object which can be use from other
* scripts like "po.js".
*/

// mapping with the current translations
let translations = {};
// function used for computing the plural form index
let plural_fn: (n: number) => boolean;

const agama = {
// the current language
language: "en",

// set the current translations, called from po.<lang>.js
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 = {};
plural_fn = undefined;
agama.language = "en";
}
},

/**
* Get a translation for a singular text
* @param str input text
* @return translated text or the original text if the translation is not found
*/
gettext: (str: string): string => {
if (translations) {
const translated = translations[str];
if (translated?.[0]) return translated[0];
}

// fallback, return the original text
return str;
},

/**
* get a translation for a plural text
* @param str1 input singular text
* @param strN input plural text
* @param 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
*/
ngettext: (str1: string, strN: string, n: number) => {
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)
const index = plural_index === true ? 1 : plural_index || 0;

if (translation[index]) return translation[index];
}
}

// fallback, return the original text
return n === 1 ? str1 : strN;
},
};

export default agama;
14 changes: 7 additions & 7 deletions web/src/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import { _, n_, N_, Nn_ } from "~/i18n";
import agama from "~/agama";

// mock the cockpit gettext functions
jest.mock("~/agama");
const gettextFn = jest.fn();
agama.gettext.mockImplementation(gettextFn);
const ngettextFn = jest.fn();
agama.ngettext.mockImplementation(ngettextFn);
jest.mock("~/agama", () => ({
...jest.requireActual("~/agama"),
gettext: jest.fn(),
ngettext: jest.fn(),
}));

// some testing texts
const text = "text to translate";
Expand All @@ -40,15 +40,15 @@ describe("i18n", () => {
it("calls the agama.gettext() implementation", () => {
_(text);

Check failure on line 41 in web/src/i18n.test.ts

View workflow job for this annotation

GitHub Actions / frontend_build (18.x)

Use a string literal argument in the translation functions

expect(gettextFn).toHaveBeenCalledWith(text);
expect(agama.gettext).toHaveBeenCalledWith(text);
});
});

describe("n_", () => {
it("calls the agama.ngettext() implementation", () => {
n_(singularText, pluralText, 1);

Check failure on line 49 in web/src/i18n.test.ts

View workflow job for this annotation

GitHub Actions / frontend_build (18.x)

Use a string literal argument in the translation functions

Check failure on line 49 in web/src/i18n.test.ts

View workflow job for this annotation

GitHub Actions / frontend_build (18.x)

Use a string literal argument in the translation functions

expect(ngettextFn).toHaveBeenCalledWith(singularText, pluralText, 1);
expect(agama.ngettext).toHaveBeenCalledWith(singularText, pluralText, 1);
});
});

Expand Down

0 comments on commit dc0d491

Please sign in to comment.