Skip to content

Commit acc465a

Browse files
committed
fix: make lib side effects free
Signed-off-by: Grigorii K. Shartsev <me@shgk.me>
1 parent 3d02991 commit acc465a

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

lib/globals.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import type { PluralFunction, Translations } from './registry.ts'
99

1010
declare global {
11-
var _nc_l10n_locale: string
12-
var _nc_l10n_language: string
13-
var _nc_l10n_timezone: string
11+
var _nc_l10n_locale: string | undefined
12+
var _nc_l10n_language: string | undefined
13+
var _nc_l10n_timezone: string | undefined
1414

15-
var _oc_l10n_registry_translations: Record<string, Translations>
16-
var _oc_l10n_registry_plural_functions: Record<string, PluralFunction>
15+
var _oc_l10n_registry_translations: Record<string, Translations> | undefined
16+
var _oc_l10n_registry_plural_functions: Record<string, PluralFunction> | undefined
1717
}
1818

1919
export {}

lib/locale.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { getCapabilities } from '@nextcloud/capabilities'
99
* Returns the user's locale
1010
*/
1111
export function getLocale(): string {
12+
globalThis._nc_l10n_locale ??= (typeof document !== 'undefined' && document.documentElement.dataset.locale)
13+
|| Intl.DateTimeFormat().resolvedOptions().locale.replaceAll(/-/g, '_')
14+
1215
return globalThis._nc_l10n_locale
1316
}
1417

@@ -40,6 +43,9 @@ export function setLocale(locale: string): void {
4043
* Returns the user's language
4144
*/
4245
export function getLanguage(): string {
46+
globalThis._nc_l10n_language ??= (typeof document !== 'undefined' && document.documentElement.lang)
47+
|| (globalThis.navigator?.language ?? 'en')
48+
4349
return globalThis._nc_l10n_language
4450
}
4551

@@ -136,11 +142,3 @@ export function isRTL(language?: string): boolean {
136142

137143
return rtlLanguages.includes(languageCode)
138144
}
139-
140-
// Initialize global state if needed (e.g. when not in DOM context like on WebWorker)
141-
142-
globalThis._nc_l10n_locale ??= (typeof document !== 'undefined' && document.documentElement.dataset.locale)
143-
|| Intl.DateTimeFormat().resolvedOptions().locale.replaceAll(/-/g, '_')
144-
145-
globalThis._nc_l10n_language ??= (typeof document !== 'undefined' && document.documentElement.lang)
146-
|| (globalThis.navigator?.language ?? 'en')

lib/registry.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ export interface AppTranslations {
4343
*/
4444
export function hasAppTranslations(appId: string): boolean {
4545
return (
46-
appId in globalThis._oc_l10n_registry_translations
46+
globalThis._oc_l10n_registry_translations !== undefined
47+
&& globalThis._oc_l10n_registry_plural_functions !== undefined
48+
&& appId in globalThis._oc_l10n_registry_translations
4749
&& appId in globalThis._oc_l10n_registry_plural_functions
4850
)
4951
}
@@ -64,6 +66,9 @@ export function registerAppTranslations(
6466
throw new Error('Invalid appId')
6567
}
6668

69+
globalThis._oc_l10n_registry_translations ??= {}
70+
globalThis._oc_l10n_registry_plural_functions ??= {}
71+
6772
globalThis._oc_l10n_registry_translations[appId] = {
6873
...(globalThis._oc_l10n_registry_translations[appId] || {}),
6974
...translations,
@@ -78,8 +83,8 @@ export function registerAppTranslations(
7883
* @param appId - The app id
7984
*/
8085
export function unregisterAppTranslations(appId: string): void {
81-
delete globalThis._oc_l10n_registry_translations[appId]
82-
delete globalThis._oc_l10n_registry_plural_functions[appId]
86+
delete globalThis._oc_l10n_registry_translations?.[appId]
87+
delete globalThis._oc_l10n_registry_plural_functions?.[appId]
8388
}
8489

8590
/**
@@ -89,11 +94,7 @@ export function unregisterAppTranslations(appId: string): void {
8994
*/
9095
export function getAppTranslations(appId: string): AppTranslations {
9196
return {
92-
translations: globalThis._oc_l10n_registry_translations[appId] ?? {},
93-
pluralFunction: globalThis._oc_l10n_registry_plural_functions[appId] ?? ((number: number) => number),
97+
translations: globalThis._oc_l10n_registry_translations?.[appId] ?? {},
98+
pluralFunction: globalThis._oc_l10n_registry_plural_functions?.[appId] ?? ((number: number) => number),
9499
}
95100
}
96-
97-
// Initialize global state if needed
98-
globalThis._oc_l10n_registry_translations ??= {}
99-
globalThis._oc_l10n_registry_plural_functions ??= {}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
},
1717
"license": "GPL-3.0-or-later",
1818
"author": "Nextcloud GmbH and Nextcloud contributors",
19+
"sideEffects": false,
1920
"type": "module",
2021
"exports": {
2122
".": {

0 commit comments

Comments
 (0)