diff --git a/js/extensions/extension.js b/js/extensions/extension.js index 966a8336b..bcc518edd 100644 --- a/js/extensions/extension.js +++ b/js/extensions/extension.js @@ -1,7 +1,5 @@ import {ExtensionBase, GettextWrapper, setExtensionManager} from './sharedInternals.js'; -export {gettext, ngettext, pgettext} from './sharedInternals.js'; - const {extensionManager} = imports.ui.main; setExtensionManager(extensionManager); @@ -24,3 +22,7 @@ export class Extension extends ExtensionBase { extensionManager.openExtensionPrefs(this.uuid, '', {}); } } + +export const { + gettext, ngettext, pgettext, +} = Extension.defineTranslationFunctions(); diff --git a/js/extensions/prefs.js b/js/extensions/prefs.js index db285e247..5d2de3f98 100644 --- a/js/extensions/prefs.js +++ b/js/extensions/prefs.js @@ -5,8 +5,6 @@ import {extensionManager} from '../extensionsService.js'; setExtensionManager(extensionManager); -export {gettext, ngettext, pgettext} from './sharedInternals.js'; - export class ExtensionPreferences extends ExtensionBase { static defineTranslationFunctions(url) { const wrapper = new GettextWrapper(this, url); @@ -22,3 +20,7 @@ export class ExtensionPreferences extends ExtensionBase { throw new GObject.NotImplementedError(); } } + +export const { + gettext, ngettext, pgettext, +} = ExtensionPreferences.defineTranslationFunctions(); diff --git a/js/extensions/sharedInternals.js b/js/extensions/sharedInternals.js index 2d5afd0f6..dcac3c233 100644 --- a/js/extensions/sharedInternals.js +++ b/js/extensions/sharedInternals.js @@ -276,95 +276,3 @@ export class GettextWrapper { }; } } - -/** - * @private - * - * @returns {?object} - The current extension, or null if not called from - * an extension. - */ -function getCurrentExtension() { - const basePath = '/gnome-shell/extensions/'; - - // Search for an occurrence of an extension stack frame - // Start at 1 because 0 is the stack frame of this function - const [, ...stack] = new Error().stack.split('\n'); - const extensionLine = stack.find( - line => line.includes(basePath)); - - if (!extensionLine) - return null; - - // The exact stack line differs depending on where the function - // was called (function or module scope), and whether it's called - // from a module or legacy import (file:// URI vs. plain path). - // - // We don't have to care about the exact composition, all we need - // is a string that can be traversed as path and contains the UUID - let path = extensionLine.slice(extensionLine.indexOf(basePath)); - - // Walk up the directory tree, looking for an extension with - // the same UUID as a directory name. - do { - path = GLib.path_get_dirname(path); - - const dirName = GLib.path_get_basename(path); - const extension = _extensionManager.lookup(dirName); - if (extension !== undefined) - return extension.stateObj; - } while (path !== '/'); - - return null; -} - -/** - * Translate @str using the extension's gettext domain - * - * @param {string} str - the string to translate - * - * @returns {string} - the translated string - */ -export function gettext(str) { - return callExtensionGettextFunc('gettext', str); -} - -/** - * Translate @str and choose plural form using the extension's - * gettext domain - * - * @param {string} str - the string to translate - * @param {string} strPlural - the plural form of the string - * @param {number} n - the quantity for which translation is needed - * - * @returns {string} - the translated string - */ -export function ngettext(str, strPlural, n) { - return callExtensionGettextFunc('ngettext', str, strPlural, n); -} - -/** - * Translate @str in the context of @context using the extension's - * gettext domain - * - * @param {string} context - context to disambiguate @str - * @param {string} str - the string to translate - * - * @returns {string} - the translated string - */ -export function pgettext(context, str) { - return callExtensionGettextFunc('pgettext', context, str); -} - -/** - * @private - * @param {string} func - function name - * @param {*[]} args - function arguments - */ -function callExtensionGettextFunc(func, ...args) { - const extension = getCurrentExtension(); - - if (!extension) - throw new Error(`${func}() can only be called from extensions`); - - return extension[func](...args); -}