183 lines
4.1 KiB
JavaScript
183 lines
4.1 KiB
JavaScript
// app/model/i18n_translation.js
|
|
'use strict';
|
|
|
|
module.exports = app => {
|
|
const { STRING, INTEGER, DATE, Op } = app.Sequelize;
|
|
|
|
// 直接在文件中定义常量
|
|
const CONSTANTS = {
|
|
TRANSLATION_STATUS: {
|
|
UNTRANSLATED: 0,
|
|
PENDING_REVIEW: 1,
|
|
APPROVED: 2,
|
|
},
|
|
LANGUAGES: [ 'zh-CN', 'en-US', 'ja-JP' ],
|
|
};
|
|
|
|
const I18nTranslation = app.model.define('wms_i18n_translation', {
|
|
id: {
|
|
type: INTEGER.UNSIGNED,
|
|
primaryKey: true,
|
|
autoIncrement: true,
|
|
},
|
|
key_id: {
|
|
type: INTEGER.UNSIGNED,
|
|
allowNull: false,
|
|
comment: '键ID',
|
|
},
|
|
language: {
|
|
type: STRING(10),
|
|
allowNull: false,
|
|
comment: '语言代码',
|
|
},
|
|
translation: {
|
|
type: STRING(500),
|
|
comment: '翻译内容',
|
|
},
|
|
translation_status: {
|
|
type: INTEGER,
|
|
allowNull: false,
|
|
defaultValue: CONSTANTS.TRANSLATION_STATUS.UNTRANSLATED,
|
|
comment: '翻译状态',
|
|
},
|
|
}, {
|
|
tableName: 'wms_i18n_translations',
|
|
timestamps: true,
|
|
createdAt: 'created_at',
|
|
updatedAt: 'updated_at',
|
|
underscored: true,
|
|
paranoid: false,
|
|
});
|
|
|
|
// 关联关系
|
|
I18nTranslation.associate = function() {
|
|
app.model.I18nTranslation.belongsTo(app.model.I18nKey, {
|
|
foreignKey: 'key_id',
|
|
as: 'key',
|
|
});
|
|
};
|
|
|
|
// 挂载常量到模型(可选)
|
|
I18nTranslation.CONSTANTS = CONSTANTS;
|
|
|
|
/**
|
|
* 获取翻译
|
|
* @param keyId
|
|
* @param language
|
|
*/
|
|
I18nTranslation.getTranslation = async function(keyId, language) {
|
|
return await this.findOne({
|
|
where: { key_id: keyId, language },
|
|
include: [{
|
|
model: app.model.I18nKey,
|
|
as: 'key',
|
|
}],
|
|
});
|
|
};
|
|
|
|
/**
|
|
* 批量获取翻译
|
|
* @param keyIds
|
|
* @param language
|
|
*/
|
|
I18nTranslation.batchGetTranslations = async function(keyIds, language) {
|
|
const records = await this.findAll({
|
|
where: {
|
|
key_id: keyIds,
|
|
language,
|
|
},
|
|
include: [{
|
|
model: app.model.I18nKey,
|
|
as: 'key',
|
|
attributes: [ 'key_name' ],
|
|
}],
|
|
});
|
|
|
|
// 返回键值对
|
|
const result = {};
|
|
records.forEach(record => {
|
|
if (record.key) {
|
|
result[record.key.key_name] = record.translation || record.key.key_name;
|
|
}
|
|
});
|
|
|
|
return result;
|
|
};
|
|
|
|
/**
|
|
* 获取项目翻译
|
|
* @param project
|
|
* @param language
|
|
*/
|
|
I18nTranslation.getProjectTranslations = async function(project, language) {
|
|
const records = await this.findAll({
|
|
include: [{
|
|
model: app.model.I18nKey,
|
|
as: 'key',
|
|
where: { project },
|
|
attributes: [ 'key_name' ],
|
|
}],
|
|
where: { language },
|
|
});
|
|
|
|
const result = {};
|
|
records.forEach(record => {
|
|
if (record.key && record.key.key_name) {
|
|
result[record.key.key_name] = record.translation || record.key.key_name;
|
|
}
|
|
});
|
|
|
|
return result;
|
|
};
|
|
|
|
/**
|
|
* 创建或更新翻译
|
|
* @param keyId
|
|
* @param language
|
|
* @param translation
|
|
*/
|
|
I18nTranslation.upsertTranslation = async function(keyId, language, translation) {
|
|
const [ record, created ] = await this.findOrCreate({
|
|
where: { key_id: keyId, language },
|
|
defaults: {
|
|
key_id: keyId,
|
|
language,
|
|
translation: translation || null,
|
|
translation_status: translation ? CONSTANTS.TRANSLATION_STATUS.PENDING_REVIEW : CONSTANTS.TRANSLATION_STATUS.UNTRANSLATED,
|
|
},
|
|
});
|
|
|
|
if (!created) {
|
|
record.translation = translation || null;
|
|
record.translation_status = translation ? CONSTANTS.TRANSLATION_STATUS.PENDING_REVIEW : CONSTANTS.TRANSLATION_STATUS.UNTRANSLATED;
|
|
await record.save();
|
|
}
|
|
|
|
return record;
|
|
};
|
|
|
|
/**
|
|
* 获取未翻译列表
|
|
* @param language
|
|
* @param project
|
|
*/
|
|
I18nTranslation.getUntranslated = async function(language, project) {
|
|
return await this.findAll({
|
|
include: [{
|
|
model: app.model.I18nKey,
|
|
as: 'key',
|
|
where: { project },
|
|
}],
|
|
where: {
|
|
language,
|
|
[Op.or]: [
|
|
{ translation: null },
|
|
{ translation: '' },
|
|
],
|
|
},
|
|
});
|
|
};
|
|
|
|
return I18nTranslation;
|
|
};
|