[improvement] rename docs/src to docs/site (#3660)
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
// component mixin
|
||||
import { get } from '../../../src/utils';
|
||||
import { camelize } from '../../../src/utils/format/string';
|
||||
|
||||
export default {
|
||||
computed: {
|
||||
$t() {
|
||||
const { name } = this.$options;
|
||||
const prefix = name ? camelize(name) + '.' : '';
|
||||
const messages = this.$vantMessages[this.$vantLang];
|
||||
|
||||
return (path, ...args) => {
|
||||
const message = get(messages, prefix + path) || get(messages, path);
|
||||
return typeof message === 'function' ? message(...args) : message;
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* 同步父窗口和 iframe 的 vue-router 状态
|
||||
*/
|
||||
|
||||
import { setLang } from './lang';
|
||||
import { iframeReady, isMobile } from '.';
|
||||
|
||||
export function initIframeRouter() {
|
||||
window.syncPath = function () {
|
||||
const router = window.vueRouter;
|
||||
const isInIframe = window !== window.top;
|
||||
const currentDir = router.history.current.path;
|
||||
const pathParts = currentDir.split('/');
|
||||
let lang = pathParts[0];
|
||||
if (currentDir[0] === '/') {
|
||||
lang = pathParts[1];
|
||||
}
|
||||
|
||||
if (!isInIframe && !isMobile) {
|
||||
const iframe = document.querySelector('iframe');
|
||||
if (iframe) {
|
||||
iframeReady(iframe, () => {
|
||||
iframe.contentWindow.changePath(lang, currentDir);
|
||||
});
|
||||
}
|
||||
setLang(lang);
|
||||
} else if (isInIframe) {
|
||||
window.top.changePath(lang, currentDir);
|
||||
}
|
||||
};
|
||||
|
||||
window.changePath = function (lang, path = '') {
|
||||
setLang(lang);
|
||||
window.vueRouter.replace(path);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
function iframeReady(iframe, callback) {
|
||||
const doc = iframe.contentDocument || iframe.contentWindow.document;
|
||||
const interval = () => {
|
||||
if (iframe.contentWindow.changePath) {
|
||||
callback();
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
interval();
|
||||
}, 50);
|
||||
}
|
||||
};
|
||||
|
||||
if (doc.readyState === 'complete') {
|
||||
interval();
|
||||
} else {
|
||||
iframe.onload = interval;
|
||||
}
|
||||
}
|
||||
|
||||
const ua = navigator.userAgent.toLowerCase();
|
||||
const isMobile = /ios|iphone|ipod|ipad|android/.test(ua);
|
||||
|
||||
function importAll(map, r) {
|
||||
r.keys().forEach(key => {
|
||||
map[key] = r(key);
|
||||
});
|
||||
}
|
||||
|
||||
export {
|
||||
isMobile,
|
||||
importAll,
|
||||
iframeReady
|
||||
};
|
||||
@@ -0,0 +1,45 @@
|
||||
import Locale from '../../../src/locale';
|
||||
import zhCN from '../../../src/locale/lang/zh-CN';
|
||||
import enUS from '../../../src/locale/lang/en-US';
|
||||
|
||||
const langMap = {
|
||||
'en-US': {
|
||||
title: 'Vant - Mobile UI Components built on Vue',
|
||||
messages: enUS
|
||||
},
|
||||
'zh-CN': {
|
||||
title: 'Vant - 轻量、可靠的移动端 Vue 组件库',
|
||||
messages: zhCN
|
||||
}
|
||||
};
|
||||
|
||||
let currentLang = '';
|
||||
|
||||
function getDefaultLang() {
|
||||
const langs = Object.keys(langMap);
|
||||
const { hash } = location;
|
||||
|
||||
for (let i = 0; i < langs.length; i++) {
|
||||
if (hash.indexOf(langs[i]) !== -1) {
|
||||
return langs[i];
|
||||
}
|
||||
}
|
||||
|
||||
const userLang = localStorage.getItem('VANT_LANGUAGE') || navigator.language || 'en-US';
|
||||
return userLang.indexOf('zh-') !== -1 ? 'zh-CN' : 'en-US';
|
||||
}
|
||||
|
||||
export function setLang(lang) {
|
||||
if (currentLang === lang) {
|
||||
return;
|
||||
}
|
||||
|
||||
currentLang = lang;
|
||||
if (window.localStorage) {
|
||||
localStorage.setItem('VANT_LANGUAGE', lang);
|
||||
}
|
||||
Locale.use(lang, langMap[lang].messages);
|
||||
document.title = langMap[lang].title;
|
||||
}
|
||||
|
||||
setLang(getDefaultLang());
|
||||
Reference in New Issue
Block a user