Просмотр исходного кода

Merge pull request #3074 from weseek/fix/migrate-deprecated-locale-id

Fix/migrate deprecated locale
Yuki Takei 5 лет назад
Родитель
Сommit
129feb7cb3
2 измененных файлов с 21 добавлено и 1 удалено
  1. 16 0
      src/lib/util/locale-utils.js
  2. 5 1
      src/server/models/user.js

+ 16 - 0
src/lib/util/locale-utils.js

@@ -2,6 +2,11 @@ const fs = require('fs');
 
 const helpers = require('./helpers');
 
+const MIGRATE_LOCALE_MAP = {
+  en: 'en_US',
+  ja: 'ja_JP',
+};
+
 /**
  * List locales dirents
  */
@@ -28,7 +33,18 @@ function listLocaleIds() {
     .map(meta => meta.id);
 }
 
+function migrateDeprecatedLocaleId(localeId) {
+  const toValue = MIGRATE_LOCALE_MAP[localeId];
+
+  if (toValue != null) {
+    return toValue;
+  }
+
+  return localeId;
+}
+
 module.exports = {
   listLocaleMetadatas,
   listLocaleIds,
+  migrateDeprecatedLocaleId,
 };

+ 5 - 1
src/server/models/user.js

@@ -11,7 +11,7 @@ const md5 = require('md5');
 const ObjectId = mongoose.Schema.Types.ObjectId;
 const crypto = require('crypto');
 
-const { listLocaleIds } = require('@commons/util/locale-utils');
+const { listLocaleIds, migrateDeprecatedLocaleId } = require('@commons/util/locale-utils');
 
 module.exports = function(crowi) {
   const STATUS_REGISTERED = 1;
@@ -75,6 +75,10 @@ module.exports = function(crowi) {
       },
     },
   });
+  // eslint-disable-next-line prefer-arrow-callback
+  userSchema.pre('validate', function() {
+    this.lang = migrateDeprecatedLocaleId(this.lang);
+  });
   userSchema.plugin(mongoosePaginate);
   userSchema.plugin(uniqueValidator);