Yuki Takei 5 лет назад
Родитель
Сommit
0770ddd2fa

+ 6 - 7
src/server/crowi/dev.js

@@ -1,10 +1,12 @@
 const logger = require('@alias/logger')('growi:crowi:dev');
 const logger = require('@alias/logger')('growi:crowi:dev');
-const fs = require('fs');
 const path = require('path');
 const path = require('path');
 
 
+const { listLocaleIds } = require('@commons/util/locale-utils');
+
 const swig = require('swig-templates');
 const swig = require('swig-templates');
 const onHeaders = require('on-headers');
 const onHeaders = require('on-headers');
 
 
+
 class CrowiDev {
 class CrowiDev {
 
 
   /**
   /**
@@ -38,12 +40,9 @@ class CrowiDev {
    */
    */
   requireForAutoReloadServer() {
   requireForAutoReloadServer() {
     // load all json files for live reloading
     // load all json files for live reloading
-    fs.readdirSync(this.crowi.localeDir)
-      .filter((filename) => {
-        return fs.statSync(path.join(this.crowi.localeDir, filename)).isDirectory();
-      })
-      .forEach((dirname) => {
-        require(path.join(this.crowi.localeDir, dirname, 'translation.json'));
+    listLocaleIds()
+      .forEach((localeId) => {
+        require(path.join(this.crowi.localeDir, localeId, 'translation.json'));
       });
       });
   }
   }
 
 

+ 5 - 4
src/server/crowi/express-init.js

@@ -18,6 +18,8 @@ module.exports = function(crowi, app) {
   const i18nSprintf = require('i18next-sprintf-postprocessor');
   const i18nSprintf = require('i18next-sprintf-postprocessor');
   const i18nMiddleware = require('i18next-express-middleware');
   const i18nMiddleware = require('i18next-express-middleware');
 
 
+  const { listLocaleIds } = require('@commons/util/locale-utils');
+
   const registerSafeRedirect = require('../middleware/safe-redirect')();
   const registerSafeRedirect = require('../middleware/safe-redirect')();
 
 
   const avoidSessionRoutes = require('../routes/avoid-session-routes');
   const avoidSessionRoutes = require('../routes/avoid-session-routes');
@@ -25,7 +27,6 @@ module.exports = function(crowi, app) {
 
 
   const env = crowi.node_env;
   const env = crowi.node_env;
 
 
-  const User = crowi.model('User');
   const lngDetector = new i18nMiddleware.LanguageDetector();
   const lngDetector = new i18nMiddleware.LanguageDetector();
   lngDetector.addDetector(i18nUserSettingDetector);
   lngDetector.addDetector(i18nUserSettingDetector);
 
 
@@ -35,8 +36,8 @@ module.exports = function(crowi, app) {
     .use(i18nSprintf)
     .use(i18nSprintf)
     .init({
     .init({
       // debug: true,
       // debug: true,
-      fallbackLng: [User.LANG_EN_US],
-      whitelist: Object.keys(User.getLanguageLabels()).map((k) => { return User[k] }),
+      fallbackLng: ['en_US'],
+      whitelist: listLocaleIds(),
       backend: {
       backend: {
         loadPath: `${crowi.localeDir}{{lng}}/translation.json`,
         loadPath: `${crowi.localeDir}{{lng}}/translation.json`,
       },
       },
@@ -69,7 +70,7 @@ module.exports = function(crowi, app) {
     res.locals.consts = {
     res.locals.consts = {
       pageGrants: Page.getGrantLabels(),
       pageGrants: Page.getGrantLabels(),
       userStatus: User.getUserStatusLabels(),
       userStatus: User.getUserStatusLabels(),
-      language:   User.getLanguageLabels(),
+      language:   listLocaleIds(),
       restrictGuestMode: crowi.aclService.getRestrictGuestModeLabels(),
       restrictGuestMode: crowi.aclService.getRestrictGuestModeLabels(),
       registrationMode: crowi.aclService.getRegistrationModeLabels(),
       registrationMode: crowi.aclService.getRegistrationModeLabels(),
     };
     };

+ 0 - 5
src/server/crowi/index.js

@@ -8,7 +8,6 @@ const CdnResourcesService = require('@commons/service/cdn-resources-service');
 const Xss = require('@commons/service/xss');
 const Xss = require('@commons/service/xss');
 const { getMongoUri } = require('@commons/util/mongoose-utils');
 const { getMongoUri } = require('@commons/util/mongoose-utils');
 
 
-const fs = require('fs');
 const path = require('path');
 const path = require('path');
 
 
 const sep = path.sep;
 const sep = path.sep;
@@ -34,10 +33,6 @@ function Crowi(rootdir) {
   this.viewsDir = path.join(this.libDir, 'views') + sep;
   this.viewsDir = path.join(this.libDir, 'views') + sep;
   this.resourceDir = path.join(this.rootDir, 'resource') + sep;
   this.resourceDir = path.join(this.rootDir, 'resource') + sep;
   this.localeDir = path.join(this.resourceDir, 'locales') + sep;
   this.localeDir = path.join(this.resourceDir, 'locales') + sep;
-  this.locales = fs.readdirSync(this.localeDir)
-    .filter((filename) => {
-      return fs.statSync(path.join(this.localeDir, filename)).isDirectory();
-    });
   this.tmpDir = path.join(this.rootDir, 'tmp') + sep;
   this.tmpDir = path.join(this.rootDir, 'tmp') + sep;
   this.cacheDir = path.join(this.tmpDir, 'cache');
   this.cacheDir = path.join(this.tmpDir, 'cache');
 
 

+ 4 - 23
src/server/models/user.js

@@ -11,6 +11,8 @@ const md5 = require('md5');
 const ObjectId = mongoose.Schema.Types.ObjectId;
 const ObjectId = mongoose.Schema.Types.ObjectId;
 const crypto = require('crypto');
 const crypto = require('crypto');
 
 
+const { listLocaleIds, listLocaleAliases } = require('@commons/util/locale-utils');
+
 module.exports = function(crowi) {
 module.exports = function(crowi) {
   const STATUS_REGISTERED = 1;
   const STATUS_REGISTERED = 1;
   const STATUS_ACTIVE = 2;
   const STATUS_ACTIVE = 2;
@@ -22,11 +24,6 @@ module.exports = function(crowi) {
   /* eslint-disable no-unused-vars */
   /* eslint-disable no-unused-vars */
   const IMAGE_POPULATION = { path: 'imageAttachment', select: 'filePathProxied' };
   const IMAGE_POPULATION = { path: 'imageAttachment', select: 'filePathProxied' };
 
 
-  const LANG_EN = 'en';
-  const LANG_EN_US = 'en_US';
-  const LANG_EN_GB = 'en_GB';
-  const LANG_JA = 'ja_JP';
-
   const PAGE_ITEMS = 50;
   const PAGE_ITEMS = 50;
 
 
   let userEvent;
   let userEvent;
@@ -58,8 +55,8 @@ module.exports = function(crowi) {
     apiToken: { type: String, index: true },
     apiToken: { type: String, index: true },
     lang: {
     lang: {
       type: String,
       type: String,
-      enum: crowi.locales,
-      default: LANG_EN_US,
+      enum: listLocaleIds().concat(listLocaleAliases()),
+      default: 'en_US',
     },
     },
     status: {
     status: {
       type: Number, required: true, default: STATUS_ACTIVE, index: true,
       type: Number, required: true, default: STATUS_ACTIVE, index: true,
@@ -146,16 +143,6 @@ module.exports = function(crowi) {
     return hasher.digest('base64');
     return hasher.digest('base64');
   }
   }
 
 
-  function getLanguageLabels() {
-    const lang = {};
-    lang.LANG_EN = LANG_EN;
-    lang.LANG_EN_US = LANG_EN_US;
-    lang.LANG_EN_GB = LANG_EN_GB;
-    lang.LANG_JA = LANG_JA;
-
-    return lang;
-  }
-
   userSchema.methods.isPasswordSet = function() {
   userSchema.methods.isPasswordSet = function() {
     if (this.password) {
     if (this.password) {
       return true;
       return true;
@@ -354,7 +341,6 @@ module.exports = function(crowi) {
     });
     });
   };
   };
 
 
-  userSchema.statics.getLanguageLabels = getLanguageLabels;
   userSchema.statics.getUserStatusLabels = function() {
   userSchema.statics.getUserStatusLabels = function() {
     const userStatus = {};
     const userStatus = {};
     userStatus[STATUS_REGISTERED] = 'Approval Pending';
     userStatus[STATUS_REGISTERED] = 'Approval Pending';
@@ -767,10 +753,5 @@ module.exports = function(crowi) {
   userSchema.statics.IMAGE_POPULATION = IMAGE_POPULATION;
   userSchema.statics.IMAGE_POPULATION = IMAGE_POPULATION;
   userSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
   userSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
 
 
-  userSchema.statics.LANG_EN = LANG_EN;
-  userSchema.statics.LANG_EN_US = LANG_EN_US;
-  userSchema.statics.LANG_EN_GB = LANG_EN_US;
-  userSchema.statics.LANG_JA = LANG_JA;
-
   return mongoose.model('User', userSchema);
   return mongoose.model('User', userSchema);
 };
 };

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

@@ -6,6 +6,8 @@ const debug = require('debug')('growi:routes:admin');
 
 
 const express = require('express');
 const express = require('express');
 
 
+const { listLocaleIds } = require('@commons/util/locale-utils');
+
 const router = express.Router();
 const router = express.Router();
 
 
 const { body } = require('express-validator/check');
 const { body } = require('express-validator/check');
@@ -108,7 +110,7 @@ module.exports = (crowi) => {
     appSetting: [
     appSetting: [
       body('title').trim(),
       body('title').trim(),
       body('confidential'),
       body('confidential'),
-      body('globalLang').isIn(crowi.locales),
+      body('globalLang').isIn(listLocaleIds()),
       body('fileUpload').isBoolean(),
       body('fileUpload').isBoolean(),
     ],
     ],
     siteUrlSetting: [
     siteUrlSetting: [

+ 3 - 1
src/server/routes/apiv3/personal-setting.js

@@ -7,6 +7,8 @@ const express = require('express');
 
 
 const passport = require('passport');
 const passport = require('passport');
 
 
+const { listLocaleIds } = require('@commons/util/locale-utils');
+
 const router = express.Router();
 const router = express.Router();
 
 
 const { body } = require('express-validator/check');
 const { body } = require('express-validator/check');
@@ -76,7 +78,7 @@ module.exports = (crowi) => {
     personal: [
     personal: [
       body('name').isString().not().isEmpty(),
       body('name').isString().not().isEmpty(),
       body('email').isEmail(),
       body('email').isEmail(),
-      body('lang').isString().isIn(crowi.locales),
+      body('lang').isString().isIn(listLocaleIds()),
       body('isEmailPublished').isBoolean(),
       body('isEmailPublished').isBoolean(),
     ],
     ],
     imageType: [
     imageType: [