【sgGoogleTranslate】自定义组件:基于Vue.js用谷歌Google Translate翻译插件实现网站多国语言开发

news/2024/11/3 4:17:43/

sgGoogleTranslate源码

<template><div :id="$options.name"> </div>
</template>
<script>
export default {name: "sgGoogleTranslate",props: ["languages", "currentLanguage"],data() {return {//语言列表,遵循 ISO 639-1 标准,俩位的code(参考:https://zh.wikipedia.org/wiki/ISO_639-1)defaultLanguages: [{ code: "en", name: "English", cname: "英语", ename: "English", },{ code: "af", name: "Afrikaans", cname: "南非语", ename: "Afrikaans", },{ code: "sq", name: "Gjuha shqipe", cname: "阿尔巴尼亚语", ename: "Albanian", },{ code: "ar", name: "العربية", cname: "阿拉伯语", ename: "Arabic", },{ code: "hy", name: "Հայերեն", cname: "亚美尼亚语", ename: "Armenian", },{ code: "az", name: "Азәрбајҹан дили", cname: "阿塞拜疆语", ename: "Azerbaijani", },{ code: "eu", name: "Euskara", cname: "巴斯克语", ename: "Basque", },{ code: "be", name: "беларуская мова", cname: "白俄罗斯语", ename: "Belarusian", },{ code: "bg", name: "български език", cname: "保加利亚语", ename: "Bulgarian", },{ code: "ca", name: "Català", cname: "加泰罗尼亚语", ename: "Catalan", },{ code: "zh-CN", name: "Chinese (Simplified)", cname: "中文 (简体)", ename: "Chinese (Simplified)", },{ code: "zh-TW", name: "Chinese (Traditional)", cname: "中文 (繁体)", ename: "Chinese (Traditional)", },{ code: "hr", name: "Српскохрватски језик", cname: "克罗地亚语", ename: "Croatian", },{ code: "cs", name: "čeština", cname: "捷克语", ename: "Czech", },{ code: "da", name: "Danmark", cname: "丹麦语", ename: "Danish", },{ code: "nl", name: "Nederlands", cname: "荷兰语", ename: "Dutch", },{ code: "et", name: "eesti keel", cname: "爱沙尼亚语", ename: "Estonian", },{ code: "tl", name: "Filipino", cname: "菲律宾语", ename: "Filipino", },{ code: "fi", name: "Finnish", cname: "芬兰语", ename: "Finnish", },{ code: "fr", name: "Français", cname: "法语", ename: "French", },{ code: "de", name: "Deutsch", cname: "德语", ename: "German", },{ code: "el", name: "Ελληνικά", cname: "希腊语", ename: "Greek", },{ code: "hu", name: "magyar", cname: "匈牙利语", ename: "Hungarian", },{ code: "id", name: "Indonesia", cname: "印度尼西亚语", ename: "Indonesian", },{ code: "ga", name: "Irish", cname: "爱尔兰语", ename: "Irish", },{ code: "it", name: "Italiano", cname: "意大利语", ename: "Italian", },{ code: "ja", name: "にほんご", cname: "日语", ename: "Japanese", },{ code: "ko", name: "한국어", cname: "韩语", ename: "Korean", },{ code: "lt", name: "lietuvių kalba", cname: "立陶宛语", ename: "Lithuanian", },{ code: "ms", name: "Malay", cname: "马来西亚语", ename: "Malay", },{ code: "no", name: "norsk", cname: "挪威语", ename: "Norwegian", },{ code: "pl", name: "Polski", cname: "波兰语", ename: "Polish", },{ code: "pt", name: "Português", cname: "葡萄牙语", ename: "Portuguese", },{ code: "ro", name: "limba română", cname: "罗马尼亚语", ename: "Romanian", },{ code: "ru", name: "Русский", cname: "俄语", ename: "Russian", },{ code: "es", name: "Español", cname: "西班牙语", ename: "Spanish", },{ code: "sv", name: "Swedish", cname: "瑞典语", ename: "Swedish", },{ code: "th", name: "ภาษาไทย", cname: "泰语", ename: "Thai", },{ code: "tr", name: "Turkish", cname: "土耳其语", ename: "Turkish", },{ code: "uk", name: "українська мова", cname: "乌克兰语", ename: "Ukrainian", },],};},mounted() {//google翻译插件初始化window[this.$options.name] = () => {new window.google.translate.TranslateElement({pageLanguage: this.currentLanguage || "auto",//默认页面源语言code:zh-CN 简体中文includedLanguages: (this.languages || this.defaultLanguages).map(v => v.code).join(','),autoDisplay: true,layout: google.translate.TranslateElement.InlineLayout.horizontal},this.$options.name);};this.loadJS(`https://translate.google.com/translate_a/element.js?cb=${this.$options.name}`);//如果该网址无法在国内访问,将无法使用google翻译插件},methods: {loadJS(url, callback) {let script = document.createElement("script");script.type = "text/javascript";script.src = url;script.onload = script.onreadystatechange = function () {if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {callback && callback();script.onload = script.onreadystatechange = null;}};document.querySelector("html").appendChild(script);},},
};
</script>
<style lang="scss" >
body {top: revert !important;
}.skiptranslate iframe {display: none;
}.goog-te-gadget {height: 50px;overflow: hidden;pointer-events: none;.goog-te-combo {pointer-events: auto;box-sizing: border-box;padding: 10px;background-color: white;box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);border-radius: 4px;margin-bottom: 10px !important; //把“由Google强力驱动”几个字隐藏掉}
}
</style>

用例

<template><div><sgGoogleTranslate /><br><br><p> 我一路向北,离开有你的季节,你说你好累,已无法再爱上谁。风在山路吹,过往的画面全都是不对,细数惭愧,我伤你几回。 </p><br><p> 我想我是太过依赖,在挂电话的刚才,坚持学单纯的小孩,静静看守这份爱,知道不能太依赖,怕你会把我宠坏,你的香味一直徘徊,我舍不得离开。 </p><br><p> 缓缓飘落的枫叶像思念,为何挽回要赶在冬天来之前,爱你穿越时间,两行来自秋末的眼泪,让爱渗透了地面我要的只是你在我身边。 </p></div>
</template><script>
import sgGoogleTranslate from "./sgGoogleTranslate";
export default { components: { sgGoogleTranslate, }, }; 
</script>

最原始的多国语言方法一分钟上手Vue VueI18n Internationalization(i18n)多国语言系统开发、国际化、中英文语言切换!_你挚爱的强哥的博客-CSDN博客这里以Vue2为例子。https://blog.csdn.net/qq_37860634/article/details/132250859


http://www.ppmy.cn/news/1116473.html

相关文章

[QT]day3

1.一个闹钟 widget.cpp: #include "widget.h" #include "ui_widget.h"#include <QWidget> #include <QTimerEvent> //定时器事件处理类 #include <QTime>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {//给播…

laravel框架 - 事件与监听器

一&#xff0c;绑定事件与监听器 在app\Providers下的EventServiceProvider.php中添加我们定义的事件与监听器 protected $listen [Registered::class > [SendEmailVerificationNotification::class,],App\ebvent\RegisterMessage>[//事件App\listeners\SendMessage//监…

QT--day3

2> 完成文本编辑器的保存工作 widget.cpp #include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this); }Widget::~Widget() {delete ui; }void Widget::on_fontbtn_cl…

EM算法和VAE的学习笔记

文章目录 摘要EM算法流程EM算法对GMM的参数估计EM算法的证明EM算法的另一种理解VAE参考文献 摘要 这是我学习EM算法&#xff08;Expectation-Maximization Algorithm&#xff09;和VAE&#xff08;Variational Auto-Encoder&#xff09;的学习笔记&#xff0c;首先总结了EM算法…

删除文件夹提示已在另一程序打开,如何强制删除

该文件里的应用程序可能在占用端口&#xff0c;所以无法直接删除。 解决办法&#xff1a; 打开要删除的文件夹复制上面的路径 同时按CtrlShiftEsc打开任务管理器 点击性能找到下方的资源监视器 点击CPU 在搜索句柄里粘贴上面复制的路径 找到搜索出来的文件右键结束进程&a…

蓝桥杯嵌入式创建第一个工程(点亮led灯)

蓝桥杯嵌入式创建第一个工程&#xff08;点亮led灯&#xff09; 一.keil导入stm32G431RX板级芯片包1.1 下载板级芯片包1.2 导入芯片包1.2.1 我们首先打开keil 点击Pack installer,如图**1.2.2 然后选中file→import 如图**1.2.3 选择我们下好的芯片包即可1.2.4 确定安装成功 二…

Java手写Trie树和Trie树应用拓展案例

Java手写Trie树和Trie树应用拓展案例 1. 算法思维导图 以下是使用mermaid代码表示的Trie树的实现原理&#xff1a; #mermaid-svg-5twy24X7Wqbhyulb {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-5twy24X7Wqbhyul…

JUC并发工具使用与详情(CountDownLatch、CyclicBarrier、Semaphore、Exchanger)

JUC并发工具 一、CountDownLatch应用&源码分析 1.1 CountDownLatch介绍 CountDownLatch就是JUC包下的一个工具&#xff0c;整个工具最核心的功能就是计数器 如果有三个业务需要并行处理&#xff0c;并且需要知道三个业务全部都处理完毕了 需要一个并发安全的计数器来操…