@tongziyang/uni-calendar-plugin
Version:
A comprehensive calendar plugin for uniapp with support for Gregorian and Lunar calendars, date selection, hotel booking, check-in functionality, and more.
138 lines (126 loc) • 4.28 kB
text/typescript
import { ref, computed } from 'vue';
import type { Ref } from 'vue';
// 支持的语言
export type Locale = 'zh-CN' | 'en-US' | 'ja-JP' | 'ko-KR';
// 语言包类型
export interface Messages {
[key: string]: string | string[] | Messages;
}
// 语言包
const messages: Record<Locale, Messages> = {
'zh-CN': {
calendar: {
year: '年',
month: '月',
week: '周',
day: '日',
today: '今天',
weekdays: ['日', '一', '二', '三', '四', '五', '六'],
weekdaysFull: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
months: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
monthsFull: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
noEvent: '无事件',
allDay: '全天',
price: '价格',
checkIn: '打卡',
checkedIn: '已打卡',
notCheckedIn: '未打卡'
}
},
'en-US': {
calendar: {
year: 'Year',
month: 'Month',
week: 'Week',
day: 'Day',
today: 'Today',
weekdays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
weekdaysFull: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
monthsFull: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
noEvent: 'No Event',
allDay: 'All Day',
price: 'Price',
checkIn: 'Check In',
checkedIn: 'Checked In',
notCheckedIn: 'Not Checked In'
}
},
'ja-JP': {
calendar: {
year: '年',
month: '月',
week: '週',
day: '日',
today: '今日',
weekdays: ['日', '月', '火', '水', '木', '金', '土'],
weekdaysFull: ['日曜日', '月曜日', '火曜日', '水曜日', '木曜日', '金曜日', '土曜日'],
months: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
monthsFull: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
noEvent: 'イベントなし',
allDay: '終日',
price: '価格',
checkIn: 'チェックイン',
checkedIn: 'チェックイン済み',
notCheckedIn: '未チェックイン'
}
},
'ko-KR': {
calendar: {
year: '년',
month: '월',
week: '주',
day: '일',
today: '오늘',
weekdays: ['일', '월', '화', '수', '목', '금', '토'],
weekdaysFull: ['일요일', '월요일', '화요일', '수요일', '목요일', '금요일', '토요일'],
months: ['1월', '2월', '3월', '4월', '5월', '6월', '7월', '8월', '9월', '10월', '11월', '12월'],
monthsFull: ['1월', '2월', '3월', '4월', '5월', '6월', '7월', '8월', '9월', '10월', '11월', '12월'],
noEvent: '이벤트 없음',
allDay: '종일',
price: '가격',
checkIn: '체크인',
checkedIn: '체크인 완료',
notCheckedIn: '체크인 안됨'
}
}
};
// 当前语言
const currentLocale: Ref<Locale> = ref('zh-CN');
// 设置语言
export function setLocale(locale: Locale): void {
if (messages[locale]) {
currentLocale.value = locale;
} else {
console.warn(`Locale ${locale} not found, fallback to zh-CN`);
currentLocale.value = 'zh-CN';
}
}
// 获取当前语言
export function getLocale(): Locale {
return currentLocale.value;
}
// 获取翻译
export function useI18n() {
const t = (key: string): string => {
const keys = key.split('.');
let result: any = messages[currentLocale.value];
for (const k of keys) {
if (result && result[k]) {
result = result[k];
} else {
console.warn(`Translation key "${key}" not found in locale ${currentLocale.value}`);
return key;
}
}
return result as string;
};
const locale = computed(() => currentLocale.value);
return {
t,
locale,
setLocale
};
}
// 导出默认实例
export default useI18n();