Yuki Takei 1 год назад
Родитель
Сommit
7064d3e3a5

+ 1 - 1
apps/app/src/features/openai/server/services/replace-annotation-with-page-link.ts

@@ -17,7 +17,7 @@ export const replaceAnnotationWithPageLink = async(messageContentDelta: MessageC
           .populate<{page: Pick<IPageHasId, 'path' | '_id'>}>('page', 'path');
 
         if (vectorStoreFileRelation != null) {
-          const { t } = await getTranslation(lang);
+          const { t } = await getTranslation({ lang });
           messageContentDelta.text.value = messageContentDelta.text.value?.replace(
             annotation.text,
             ` [${t('source')}: [${vectorStoreFileRelation.page.path}](/${vectorStoreFileRelation.page._id})]`,

+ 1 - 1
apps/app/src/server/routes/apiv3/app-settings.js

@@ -569,7 +569,7 @@ module.exports = (crowi) => {
    *            description: Succeeded to send test mail for smtp
    */
   router.post('/smtp-test', loginRequiredStrictly, adminRequired, addActivity, async(req, res) => {
-    const { t } = await getTranslation(req.user.lang);
+    const { t } = await getTranslation({ lang: req.user.lang });
 
     try {
       await sendTestEmail(req.user.email);

+ 1 - 1
apps/app/src/server/routes/apiv3/security-settings/index.js

@@ -932,7 +932,7 @@ module.exports = (crowi) => {
    *                  $ref: '#/components/schemas/SamlAuthSetting'
    */
   router.put('/saml', loginRequiredStrictly, adminRequired, addActivity, validator.samlAuth, apiV3FormValidator, async(req, res) => {
-    const { t } = await getTranslation(req.user.lang);
+    const { t } = await getTranslation({ lang: req.user.lang, ns: ['translation', 'admin'] });
 
     //  For the value of each mandatory items,
     //  check whether it from the environment variables is empty and form value to update it is empty

+ 1 - 1
apps/app/src/server/routes/login-passport.js

@@ -241,7 +241,7 @@ module.exports = function(crowi, app) {
    * @param {*} res
    */
   const testLdapCredentials = async(req, res) => {
-    const { t } = await getTranslation(req.user.lang);
+    const { t } = await getTranslation({ lang: req.user.lang });
 
     if (!passportService.isLdapStrategySetup) {
       logger.debug('LdapStrategy has not been set up');

+ 16 - 8
apps/app/src/server/service/i18next.ts

@@ -1,7 +1,7 @@
 import path from 'path';
 
-import type { Lang } from '@growi/core';
-import type { TFunction, i18n } from 'i18next';
+import type { Lang } from '@growi/core/dist/interfaces';
+import type { InitOptions, TFunction, i18n } from 'i18next';
 import { createInstance } from 'i18next';
 import resourcesToBackend from 'i18next-resources-to-backend';
 
@@ -14,7 +14,7 @@ import { configManager } from './config-manager';
 
 const relativePathToLocalesRoot = path.relative(__dirname, resolveFromRoot('public/static/locales'));
 
-const initI18next = async(fallbackLng: Lang[] = [defaultLang]) => {
+const initI18next = async(overwriteOpts: InitOptions) => {
   const i18nInstance = createInstance();
   await i18nInstance
     .use(
@@ -26,7 +26,7 @@ const initI18next = async(fallbackLng: Lang[] = [defaultLang]) => {
     )
     .init({
       ...initOptions,
-      fallbackLng,
+      ...overwriteOpts,
     });
   return i18nInstance;
 };
@@ -36,13 +36,21 @@ type Translation = {
   i18n: i18n
 }
 
-export async function getTranslation(lang?: Lang): Promise<Translation> {
+type Opts = {
+  lang?: Lang,
+  ns?: string | readonly string[],
+}
+
+export async function getTranslation(opts?: Opts): Promise<Translation> {
   const globalLang = configManager.getConfig('crowi', 'app:globalLang') as Lang;
-  const fixedLang = lang ?? globalLang;
-  const i18nextInstance = await initI18next([fixedLang, defaultLang]);
+  const fixedLang = opts?.lang ?? globalLang;
+  const i18nextInstance = await initI18next({
+    fallbackLng: [fixedLang, defaultLang],
+    ns: opts?.ns,
+  });
 
   return {
-    t: i18nextInstance.getFixedT(fixedLang),
+    t: i18nextInstance.getFixedT(fixedLang, opts?.ns),
     i18n: i18nextInstance,
   };
 }