Skip to content

Commit b36f8ca

Browse files
committed
add proposed return type to gettext functions, event if it is already present in the global types
1 parent ff64e4b commit b36f8ca

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

packages/gnome-shell/src/extensions/sharedInternals.d.ts

+29-23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import type { Extension } from './extension.js';
44
import type Gio from '@girs/gio-2.0';
55
import type GObject from '@girs/gobject-2.0';
66

7+
/**
8+
* @see https://gjs.guide/extensions/development/translations.html#marking-strings-for-translation
9+
* @notes in Gnome Shell every string has format, since String.prototype.format() is added by it, but the best practices state, that only gettext functions use that, we already have that types in "global.d.ts" but if you don't want to have that type on every string, it is the return type of all those functions without importing the global types
10+
*/
11+
export type FormattableString = string & { format: (...args: any[]) => string };
12+
713
export interface TranslationFunctions {
814
/**
915
* Translate `str` using the extension's gettext domain
@@ -12,7 +18,7 @@ export interface TranslationFunctions {
1218
*
1319
* @returns {string} the translated string
1420
*/
15-
gettext(str: string): string
21+
gettext(str: string): FormattableString;
1622
/**
1723
* Translate `str` and choose plural form using the extension's
1824
* gettext domain
@@ -23,7 +29,7 @@ export interface TranslationFunctions {
2329
*
2430
* @returns {string} the translated string
2531
*/
26-
ngettext(str: string, strPlural: string, n: number): string
32+
ngettext(str: string, strPlural: string, n: number): FormattableString;
2733

2834
/**
2935
* Translate `str` in the context of `context` using the extension's
@@ -34,38 +40,38 @@ export interface TranslationFunctions {
3440
*
3541
* @returns {string} the translated string
3642
*/
37-
pgettext(context: string, str: string): string
43+
pgettext(context: string, str: string): FormattableString;
3844
}
3945

4046
export class ExtensionBase {
4147
#gettextDomain: string | null;
4248

43-
readonly metadata: ExtensionMetadata
49+
readonly metadata: ExtensionMetadata;
4450

4551
/**
4652
* Look up an extension by URL (usually 'import.meta.url')
4753
*
4854
* @param url - a file:// URL
4955
*/
50-
static lookupByURL(url: string): Extension | null
56+
static lookupByURL(url: string): Extension | null;
5157

5258
/**
5359
* Look up an extension by UUID
5460
*
5561
* @param {string} _uuid
5662
*/
57-
static lookupByUUID(_uuid: string): Extension | null
63+
static lookupByUUID(_uuid: string): Extension | null;
5864

5965
/**
6066
* @param metadata - metadata passed in when loading the extension
6167
*/
62-
constructor(metadata: ExtensionMetadata)
68+
constructor(metadata: ExtensionMetadata);
6369

64-
get uuid(): string
70+
get uuid(): string;
6571

66-
get dir(): Gio.File
72+
get dir(): Gio.File;
6773

68-
get path(): string
74+
get path(): string;
6975

7076
/**
7177
* Get a GSettings object for schema, using schema files in
@@ -76,7 +82,7 @@ export class ExtensionBase {
7682
*
7783
* @returns {}
7884
*/
79-
getSettings(schema?: string): Gio.Settings
85+
getSettings(schema?: string): Gio.Settings;
8086

8187
/**
8288
* Initialize Gettext to load translations from extensionsdir/locale. If
@@ -85,7 +91,7 @@ export class ExtensionBase {
8591
*
8692
* @param {string=} domain - the gettext domain to use
8793
*/
88-
initTranslations(domain: string): void
94+
initTranslations(domain: string): void;
8995

9096
/**
9197
* Translate `str` using the extension's gettext domain
@@ -94,7 +100,7 @@ export class ExtensionBase {
94100
*
95101
* @returns the translated string
96102
*/
97-
gettext(str: string): string
103+
gettext(str: string): FormattableString;
98104

99105
/**
100106
* Translate `str` and choose plural form using the extension's
@@ -106,7 +112,7 @@ export class ExtensionBase {
106112
*
107113
* @returns {string} the translated string
108114
*/
109-
ngettext(str: string, strPlural: string, n: number): string
115+
ngettext(str: string, strPlural: string, n: number): FormattableString;
110116

111117
/**
112118
* Translate `str` in the context of `context` using the extension's
@@ -117,29 +123,29 @@ export class ExtensionBase {
117123
*
118124
* @returns {string} the translated string
119125
*/
120-
pgettext(context: string, str: string): string
126+
pgettext(context: string, str: string): FormattableString;
121127

122128
/**
123129
* @param {string} func
124130
*/
125-
#checkGettextDomain(func: string)
131+
#checkGettextDomain(func: string);
126132
}
127133

128134
export class GettextWrapper {
129135
#url: string | null;
130136
#extensionClass: ExtensionBase;
131137

132-
constructor(extensionClass: ExtensionBase, url: string)
138+
constructor(extensionClass: ExtensionBase, url: string);
133139

134-
#detectUrl(): string | null
140+
#detectUrl(): string | null;
135141

136-
#lookupExtension(funcName: string): ExtensionBase
142+
#lookupExtension(funcName: string): ExtensionBase;
137143

138-
#gettext(str: string): string
144+
#gettext(str: string): FormattableString;
139145

140-
#ngettext(str: string, strPlural: string, n: number): string
146+
#ngettext(str: string, strPlural: string, n: number): FormattableString;
141147

142-
#pgettext(context:any, str: string): string
148+
#pgettext(context: any, str: string): FormattableString;
143149

144-
defineTranslationFunctions(): TranslationFunctions
150+
defineTranslationFunctions(): TranslationFunctions;
145151
}

0 commit comments

Comments
 (0)