Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

fix(config): init config check does not report Browse Videos errors (#7222)

Gianantonio Pini319da3b6a561
diff --git a/server/core/helpers/custom-validators/browse-videos.ts b/server/core/helpers/custom-validators/browse-videos.ts
index 8f23a88351..f329372333 100644
--- a/server/core/helpers/custom-validators/browse-videos.ts
+++ b/server/core/helpers/custom-validators/browse-videos.ts
@@ -1,10 +1,17 @@
import { t } from '../i18n.js'
-export function getBrowseVideosDefaultSortError (value: string, enabledTrendingAlgorithms: string[], language: string) {
+export function getBrowseVideosDefaultSortError (value: string, enabledTrendingAlgorithms: string[], language?: string) {
const availableOptions = [ '-publishedAt', '-originallyPublishedAt', 'name', '-trending', '-hot', '-likes', '-views' ]
if (availableOptions.includes(value) === false) {
- return t('Browse videos default sort should be \'' + availableOptions.join('\' or \'') + '\', instead of \'' + value + '\'', language)
+ const options = availableOptions.join('\' or \'')
+
+ // Only use `t` if language is provided, because i18n might not be initialized yet and can return `undefined`
+ if (language) {
+ return t(`Browse videos default sort should be '{options}', instead of '{value}'`, language, { options, value })
+ }
+
+ return `Browse videos default sort should be '${options}', instead of '${value}'`
}
const trendingSortAlgorithmMap = new Map<string, string>([
@@ -15,20 +22,31 @@ export function getBrowseVideosDefaultSortError (value: string, enabledTrendingA
const currentTrendingSortAlgorithm = trendingSortAlgorithmMap.get(value)
if (currentTrendingSortAlgorithm && enabledTrendingAlgorithms.includes(currentTrendingSortAlgorithm) === false) {
- return t(
- `Trending videos algorithm '${currentTrendingSortAlgorithm}' should be enabled if browse videos default sort is '${value}'`,
- language
- )
+ if (language) {
+ return t(
+ `Trending videos algorithm '{currentTrendingSortAlgorithm}' should be enabled if browse videos default sort is '{value}'`,
+ language,
+ { currentTrendingSortAlgorithm, value }
+ )
+ }
+
+ return `Trending videos algorithm '${currentTrendingSortAlgorithm}' should be enabled if browse videos default sort is '${value}'`
}
return null
}
-export function getBrowseVideosDefaultScopeError (value: string, language: string) {
+export function getBrowseVideosDefaultScopeError (value: string, language?: string) {
const availableOptions = [ 'local', 'federated' ]
if (availableOptions.includes(value) === false) {
- return t('Browse videos default scope should be \'' + availableOptions.join('\' or \'') + '\', instead of \'' + value + '\'', language)
+ const options = availableOptions.join('\' or \'')
+
+ if (language) {
+ return t(`Browse videos default scope should be '{options}', instead of '{value}'`, language, { options, value })
+ }
+
+ return `Browse videos default scope should be '${options}', instead of '${value}'`
}
return null
diff --git a/server/core/helpers/i18n.ts b/server/core/helpers/i18n.ts
index 9ed7aa53d6..33c31699c4 100644
--- a/server/core/helpers/i18n.ts
+++ b/server/core/helpers/i18n.ts
@@ -58,9 +58,19 @@ export function guessLanguageFromReq (req: express.Request, res: express.Respons
CONFIG.INSTANCE.DEFAULT_LANGUAGE
}
-export function t (key: string, language: string, context: Record<string, string | number> = {}) {
+export function t (
+ key: string,
+ language: string,
+ context: Record<string, string | number> = {}
+) {
if (!language) throw new Error('Language is required for translation')
+ if (!i18next.isInitialized) {
+ logger.warn('i18next is not initialized, translation will not work')
+
+ return key
+ }
+
return i18next.t(key, { lng: getCompleteLocale(language), ...context })
}
diff --git a/server/core/initializers/checker-after-init.ts b/server/core/initializers/checker-after-init.ts
index f3f8314f9e..97e169f990 100644
--- a/server/core/initializers/checker-after-init.ts
+++ b/server/core/initializers/checker-after-init.ts
@@ -15,7 +15,6 @@ import { OAuthClientModel } from '../models/oauth/oauth-client.js'
import { UserModel } from '../models/user/user.js'
import { CONFIG, getLocalConfigFilePath, isEmailEnabled, reloadConfig } from './config.js'
import { WEBSERVER } from './constants.js'
-import { englishLanguage } from '@server/helpers/i18n.js'
async function checkActivityPubUrls () {
const actor = await getServerActor()
@@ -390,14 +389,9 @@ function checkThumbnailsConfig () {
}
function checkBrowseVideosConfig () {
- const sortError = getBrowseVideosDefaultSortError(
- CONFIG.CLIENT.BROWSE_VIDEOS.DEFAULT_SORT,
- CONFIG.TRENDING.VIDEOS.ALGORITHMS.ENABLED,
- englishLanguage
- )
-
+ const sortError = getBrowseVideosDefaultSortError(CONFIG.CLIENT.BROWSE_VIDEOS.DEFAULT_SORT, CONFIG.TRENDING.VIDEOS.ALGORITHMS.ENABLED)
if (sortError) throw new Error(sortError)
- const scopeError = getBrowseVideosDefaultScopeError(CONFIG.CLIENT.BROWSE_VIDEOS.DEFAULT_SCOPE, englishLanguage)
+ const scopeError = getBrowseVideosDefaultScopeError(CONFIG.CLIENT.BROWSE_VIDEOS.DEFAULT_SCOPE)
if (scopeError) throw new Error(scopeError)
}