res
│ resUtils.js
│
├─values
│ img.js
│ strings.js│
├─values_en
│ img.js
│ strings.js
│└─values_zh_CN
strings.js
module.exports = {
LOGIN_STATUS_INVALID: '登录失效',
LOGIN_LOG_AGAIN: '请重新登录',
}
module.exports = {
LOGIN_STATUS_INVALID: 'Login status invalid',
LOGIN_LOG_AGAIN: 'Log in again',
}
/**
* 默认环境图片配置
* @author Shirley.jiang
*/
const ICON_URL = 'https://***';
let env = 'zh_CN';
let getImg = (name) => {
return ICON_URL + '/' + env + '/' + name;
}
module.exports = {
IC_BTN_PHONE: getImg('btn_phone.png'), // 网络图片
ICON_LOCATION: '/imgs/icon_location.png', // 本地图片
}
/**
* en环境图片配置
* @author Shirley.jiang
*/
const ICON_URL = 'https://***';
let env = 'en';
let getImg = (name) => {
return ICON_URL + '/' + env + '/' + name;
}
module.exports = {
IC_CHANGE_LANGUAGE: getImg('ic_change_language.png')
};
const resUtils = require('../../res/resUtils.js'); // 引入
resUtils.strings.LOGIN_STATUS_INVALID; // 文字调用
resUtils.imgs.IC_CHANGE_LANGUAGE; // 图片调用
const localStorage = require('../utils/LocalStorage.js');
/**
* 国际化* @author Shirley.jiang
*/
class ResUtils {
static mInstance;
mStrings = {};
mImgs = {};
mEnv;
static getInstance() {
if (!ResUtils.mInstance) {
ResUtils.mInstance = new ResUtils();
}
return ResUtils.mInstance;
}
init(env) {
this.mEnv = env;
this.initStrings();
this.initImgs();
}
/**
* 引用字符配置
*/
initStrings() {
this.mStrings = {};
let strings;
let defaultStrings;
try {
strings = require('./values_' + this.mEnv + '/strings.js');
} catch (err) { }
try {
defaultStrings = require('./values/strings.js');
} catch (err) { }
// 初始化默认的数据
for (let key in defaultStrings) {
if (!defaultStrings.hasOwnProperty(key)) {
continue;
}
this.mStrings[key] = defaultStrings[key];
}
// 如果当前语言文件中定义的有,则直接覆盖
for (let key in strings) {
if (!this.mStrings.hasOwnProperty(key)) {
continue;
}
this.mStrings[key] = strings[key];
}
}
/**
* 引用图片配置
*/
initImgs() {
this.mImgs = {};
let imgs;
let defaultImgs;
try {
imgs = require('./values_' + this.mEnv + '/img.js');
} catch (err) { }
try {
defaultImgs = require('./values/img.js');
} catch (err) { }
// 初始化默认的数据
for (let key in defaultImgs) {
if (!defaultImgs.hasOwnProperty(key)) {
continue;
}
this.mImgs[key] = defaultImgs[key];
}
// 如果当前语言文件中定义的有,则直接覆盖
for (let key in imgs) {
if (!this.mImgs.hasOwnProperty(key)) {
continue;
}
this.mImgs[key] = imgs[key];
}
}
/**
* 切换语言
* @param {string} env 语言值
*/
changeLanguage(env) {
localStorage.setEnv(env);
this.init(env);
}
}
/**
* zh_CN 中文* zh_TW 中文繁体(中国台湾)
* en 英文环境*/
let env = localStorage.getEnv();
ResUtils.getInstance().init(env);
module.exports = ResUtils.getInstance();
感谢《小程序开发一群》的Shirley.jiang投稿。