未加星标

Localization support in your Vue.js based Chrome extension

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

Localization is important when you’re looking to grow your app or website in other markets. Recently I added localization support to my startups’ product Helperbird. If you’re building a cross-platform Chrome app it is super straightforward.

Getting started

To get started, your going to make sure you have a folder called “_locales” in your projects directory. In this folder, you will have a folder en for English. To add more languages, you will create more folders with the languages shorthand, ES for Spanish and so on. In each of the language folders you will need to create a JSON file called “ messages.json”


Localization support in your Vue.js based Chrome extension

The contents of the messages.json file.

As you can see in the above screenshot, we have JSON objects with different message values. These are the English strings we will telling the Browser to inject into the app on render. So if the users browser and computer is set to English it will use these strings. The keys will have to unique.

Setting up yourApp

In Helperbird I am using Vue.js. We will be using this fantastic i18n Vue.js plugin called “ vue-plugin-webextension-i18n ” by ~ straybugs . This will allow us to have full support across all browsers, as it uses the Chrome native i18n functions and the webExtension functions which are used by Firefox, IE and Safari.

Install it npm install vue-plugin-webextension-i18n―save_ Include it in yourapp import i18n from ‘vue-plugin-webextension-i18n’; Vue.use(i18n);_ Use inapp

In your messages.json file. You have all the strings you want to inject into app.

{ "appName": { "message": "Mobile Dyslexic Tool - Helperbird for Chrome" }, "appTitle": { "message": "Helperbird" }, "appDescription": { "message": "OpenDyslexic font for Chrome. Google Docs Support. Text to speech. Background colors and more." } }

To access and inject “appTitle” into your app. For in the javascript functions or logic, will will use this.$i18n() from the plugin we installed, which will the add the cross platform support to the native function.

enableDyslexica: function () { this.showSnackbar = true; this.popupMessage = this.i18n('appTitle'); this.save('enabled', this.enable); this.reload(); }

In your template section, you will have to do the following.

<h3 class="md-title" style="flex: 1">{{ $i18n('appTitle') }}</h3>

Now thats it, you just need to do this for all the strings in your app. Now just build your project and it will work without anything else.

Finishing up

Once an extension or app is internationalized, translating it is simple. You copy messages.json , translate it, and put the copy into a new directory under _locales . For example, to support Spanish, just put a translated copy of messages.json under _locales/es . The following figure shows the previous extension with a new Spanish translation.


Localization support in your Vue.js based Chrome extension

Also important to note, you can use any of the supported locales . If you use an unsupported locale, the browser will ignoreit.

I would love feedback on this. You can follow me on Github and Twitter .

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:Localization support in your Vue.js based Chrome extension
本站链接:https://www.codesec.net/view/628251.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(28)