1- /// <reference types="@nextcloud/typings" />
21/*!
32 * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
43 * SPDX-License-Identifier: GPL-3.0-or-later
@@ -32,19 +31,6 @@ export type Translations = Record<string, string | string[] | undefined>
3231 */
3332export type PluralFunction = ( number : number ) => number
3433
35- /**
36- * Extended window interface with translation registry
37- * Exported just for internal testing purpose
38- *
39- * @private
40- */
41- export interface NextcloudWindowWithRegistry extends Nextcloud . v27 . WindowWithGlobals {
42- _oc_l10n_registry_translations ?: Record < string , Translations >
43- _oc_l10n_registry_plural_functions ?: Record < string , PluralFunction >
44- }
45-
46- declare const window : NextcloudWindowWithRegistry
47-
4834export interface AppTranslations {
4935 translations : Translations
5036 pluralFunction : PluralFunction
@@ -54,12 +40,11 @@ export interface AppTranslations {
5440 * Check if translations and plural function are set for given app
5541 *
5642 * @param appId - The app id
57- * @return
5843 */
59- export function hasAppTranslations ( appId : string ) {
44+ export function hasAppTranslations ( appId : string ) : boolean {
6045 return (
61- window . _oc_l10n_registry_translations ?. [ appId ] !== undefined
62- && window . _oc_l10n_registry_plural_functions ?. [ appId ] !== undefined
46+ appId in globalThis . _oc_l10n_registry_translations
47+ && appId in globalThis . _oc_l10n_registry_plural_functions
6348 )
6449}
6550
@@ -74,34 +59,27 @@ export function registerAppTranslations(
7459 appId : string ,
7560 translations : Translations ,
7661 pluralFunction : PluralFunction ,
77- ) {
62+ ) : void {
7863 if ( appId === '__proto__' || appId === 'constructor' || appId === 'prototype' ) {
7964 throw new Error ( 'Invalid appId' )
8065 }
8166
82- window . _oc_l10n_registry_translations = Object . assign (
83- window . _oc_l10n_registry_translations || { } ,
84- {
85- [ appId ] : Object . assign ( window . _oc_l10n_registry_translations ?. [ appId ] || { } , translations ) ,
86- } ,
87- )
67+ globalThis . _oc_l10n_registry_translations [ appId ] = {
68+ ...( globalThis . _oc_l10n_registry_translations [ appId ] || { } ) ,
69+ ...translations ,
70+ }
8871
89- window . _oc_l10n_registry_plural_functions = Object . assign (
90- window . _oc_l10n_registry_plural_functions || { } ,
91- {
92- [ appId ] : pluralFunction ,
93- } ,
94- )
72+ globalThis . _oc_l10n_registry_plural_functions [ appId ] = pluralFunction
9573}
9674
9775/**
9876 * Unregister all translations and plural function for given app
9977 *
10078 * @param appId - The app id
10179 */
102- export function unregisterAppTranslations ( appId : string ) {
103- delete window . _oc_l10n_registry_translations ?. [ appId ]
104- delete window . _oc_l10n_registry_plural_functions ?. [ appId ]
80+ export function unregisterAppTranslations ( appId : string ) : void {
81+ delete globalThis . _oc_l10n_registry_translations [ appId ]
82+ delete globalThis . _oc_l10n_registry_plural_functions [ appId ]
10583}
10684
10785/**
@@ -111,7 +89,11 @@ export function unregisterAppTranslations(appId: string) {
11189 */
11290export function getAppTranslations ( appId : string ) : AppTranslations {
11391 return {
114- translations : window . _oc_l10n_registry_translations ?. [ appId ] ?? { } ,
115- pluralFunction : window . _oc_l10n_registry_plural_functions ?. [ appId ] ?? ( ( number : number ) => number ) ,
92+ translations : globalThis . _oc_l10n_registry_translations [ appId ] ?? { } ,
93+ pluralFunction : globalThis . _oc_l10n_registry_plural_functions [ appId ] ?? ( ( number : number ) => number ) ,
11694 }
11795}
96+
97+ // Initialize global state if needed
98+ globalThis . _oc_l10n_registry_translations ??= { }
99+ globalThis . _oc_l10n_registry_plural_functions ??= { }
0 commit comments