使用简单键盘的多语言支持
Mutli-Language support using Simple Keyboard
我是 vue js 的新手,正在尝试实现 https://virtual-keyboard.js.org/vuejs/
我已经实现了基本布局,但需要进行多语言https://github.com/simple-keyboard/simple-keyboard-layouts支持,如前所述,使用不同的语言布局。
但我不能在语言之间切换。我实际上找不到注入不同语言文件的方法。
<template>
<div :class="keyboardClass"></div>
</template>
<script>
import Keyboard from "simple-keyboard";
import "simple-keyboard/build/css/index.css";
// import SimpleKeyboardLayouts from "../lib/components/Layouts";
export default {
name: "SimpleKeyboard",
props: {
keyboardClass: {
default: "simple-keyboard",
type: String,
},
input: {
type: String,
},
},
data: () => ({
keyboard: null,
}),
mounted() {
this.keyboard = new Keyboard(this.keyboardClass, {
onChange: this.onChange,
onKeyPress: this.onKeyPress,
});
},
methods: {
onChange(input) {
this.$emit("onChange", input);
},
onKeyPress(button) {
this.$emit("onKeyPress", button);
/**
* If you want to handle the shift and caps lock buttons
*/
if (button === "{shift}" || button === "{lock}") this.handleShift();
},
handleShift() {
let currentLayout = this.keyboard.options.layoutName;
let shiftToggle = currentLayout === "default" ? "shift" : "default";
this.keyboard.setOptions({
layoutName: shiftToggle,
});
},
handleLanguages() {
let currentLayoutlang = this.keyboard.options.layoutName;
let languageToggle =
currentLayoutlang === "english" ? "german" : "english";
this.keyboard.setOptions({
layoutName: languageToggle,
});
},
},
watch: {
input(input) {
this.keyboard.setInput(input);
},
},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped></style>
我有一个示例布局语言文件是这样的
import { LayoutItem } from "../interfaces";
/**
* Layout: German
*/
export default <LayoutItem>{
layout: {
default: [
"^ 1 2 3 4 5 6 7 8 9 0 \u00DF \u00B4 {bksp}",
"{tab} q w e r t z u i o p \u00FC +",
"{lock} a s d f g h j k l \u00F6 \u00E4 # {enter}",
"{shift} < y x c v b n m , . - {shift}",
".com @ {space}",
],
shift: [
'\u00B0 ! " \u00A7 $ % & / ( ) = ? ` {bksp}',
"{tab} Q W E R T Z U I O P \u00DC *",
"{lock} A S D F G H J K L \u00D6 \u00C4 ' {enter}",
"{shift} > Y X C V B N M ; : _ {shift}",
".com @ {space}",
],
},
};
我建议使用 simple-keyboard-layouts
,然后在您要更改语言的 method
中执行此操作:
handleLanguages() {
let languageToggle =
this.keyboard.options.layoutName === "english" ? "german" : "english";
const layout = new this.SimpleKeyboardLayouts().get(languageToggle);
this.keyboard.setOptions({ layout });
}
这就是您使用此库切换语言的方式。如果它缺少您需要的语言,请在 simple-keyboard-layouts 中提出 PR,我发现作者对我添加的布局非常敏感并且乐于助人。
Here is a fiddle(您可能需要单击 fiddle 中的刷新图标才能显示按钮),请注意 simple-keyboard-layouts
的某些较新版本似乎无法正常工作有了这个功能,所以一定要检查 package.json
版本号是否兼容,或者更新你的代码以匹配这个库中最新最好的版本。这些版本肯定 做 为此工作:
"simple-keyboard": "^2.32.108",
"simple-keyboard-layouts": "^1.15.165",
我是 vue js 的新手,正在尝试实现 https://virtual-keyboard.js.org/vuejs/
我已经实现了基本布局,但需要进行多语言https://github.com/simple-keyboard/simple-keyboard-layouts支持,如前所述,使用不同的语言布局。
但我不能在语言之间切换。我实际上找不到注入不同语言文件的方法。
<template>
<div :class="keyboardClass"></div>
</template>
<script>
import Keyboard from "simple-keyboard";
import "simple-keyboard/build/css/index.css";
// import SimpleKeyboardLayouts from "../lib/components/Layouts";
export default {
name: "SimpleKeyboard",
props: {
keyboardClass: {
default: "simple-keyboard",
type: String,
},
input: {
type: String,
},
},
data: () => ({
keyboard: null,
}),
mounted() {
this.keyboard = new Keyboard(this.keyboardClass, {
onChange: this.onChange,
onKeyPress: this.onKeyPress,
});
},
methods: {
onChange(input) {
this.$emit("onChange", input);
},
onKeyPress(button) {
this.$emit("onKeyPress", button);
/**
* If you want to handle the shift and caps lock buttons
*/
if (button === "{shift}" || button === "{lock}") this.handleShift();
},
handleShift() {
let currentLayout = this.keyboard.options.layoutName;
let shiftToggle = currentLayout === "default" ? "shift" : "default";
this.keyboard.setOptions({
layoutName: shiftToggle,
});
},
handleLanguages() {
let currentLayoutlang = this.keyboard.options.layoutName;
let languageToggle =
currentLayoutlang === "english" ? "german" : "english";
this.keyboard.setOptions({
layoutName: languageToggle,
});
},
},
watch: {
input(input) {
this.keyboard.setInput(input);
},
},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped></style>
我有一个示例布局语言文件是这样的
import { LayoutItem } from "../interfaces";
/**
* Layout: German
*/
export default <LayoutItem>{
layout: {
default: [
"^ 1 2 3 4 5 6 7 8 9 0 \u00DF \u00B4 {bksp}",
"{tab} q w e r t z u i o p \u00FC +",
"{lock} a s d f g h j k l \u00F6 \u00E4 # {enter}",
"{shift} < y x c v b n m , . - {shift}",
".com @ {space}",
],
shift: [
'\u00B0 ! " \u00A7 $ % & / ( ) = ? ` {bksp}',
"{tab} Q W E R T Z U I O P \u00DC *",
"{lock} A S D F G H J K L \u00D6 \u00C4 ' {enter}",
"{shift} > Y X C V B N M ; : _ {shift}",
".com @ {space}",
],
},
};
我建议使用 simple-keyboard-layouts
,然后在您要更改语言的 method
中执行此操作:
handleLanguages() {
let languageToggle =
this.keyboard.options.layoutName === "english" ? "german" : "english";
const layout = new this.SimpleKeyboardLayouts().get(languageToggle);
this.keyboard.setOptions({ layout });
}
这就是您使用此库切换语言的方式。如果它缺少您需要的语言,请在 simple-keyboard-layouts 中提出 PR,我发现作者对我添加的布局非常敏感并且乐于助人。
Here is a fiddle(您可能需要单击 fiddle 中的刷新图标才能显示按钮),请注意 simple-keyboard-layouts
的某些较新版本似乎无法正常工作有了这个功能,所以一定要检查 package.json
版本号是否兼容,或者更新你的代码以匹配这个库中最新最好的版本。这些版本肯定 做 为此工作:
"simple-keyboard": "^2.32.108",
"simple-keyboard-layouts": "^1.15.165",