📜 ⬆️ ⬇️

Making your Chrome plugins multilanguage

image
Do you have a plugin for Chrome in Russian or in English, but the audience of the plugin is much wider and not only in your chosen language? Then please roll.

We will do support for Russian and English, but no one bothers you to do support for other languages.
In order to make your plugin multilingual from the beginning, we go to manifest.json
And add the following lines:
This line indicates which language will be used by default, in cases where the translation for the user's language is not available.
"default_locale": "en",
Now, in place of the standard description of the plugin we write
"description": "__MSG_chrome_extension_description__",
You can also do for the name of the plugin
"name": "__MSG_chrome_extension_name__",
After the manipulations, everything will look something like this.
image

Creating localization files


To do this, in the root of the plugin create a folder named _locales
In it we create folders en and ru
Create the file messages.json in folders
With the following content
{
"chrome_extension_description": {
"message": " ."
},
"chrome_extension_name": {
"message": " "
},
"some_string": {
"message": ""
},

"lang": {
"message": "ru"
}
}

In the folder en in the same file we write the same but in English
{
"chrome_extension_description": {
"message": "Extension description."
},
"chrome_extension_name": {
"message": "Plugin name"
},
"some_string": {
"message": "some text"
},

"lang": {
"message": "en"
}
}

If you want to make support for any other language, then create the appropriate folder and file in it, and be sure to change the value of the last lines
"lang": {
"message": "en"
}


Here are the languages ​​that Chrome supports.
am ar bg bn ca cs da de el en_GB en_US es es_419 et fiil fr gu gu hi hr hu id it ja kn ko lt lv ml mr nb nl or pl pt pt_BR pt_PT ro ru sk sl sr sw ta ta th r uk vi zh zh_CN zh_TW

Now let's go into the settings of the plugin namely in options.html, let's say you want to make a title in different languages ​​for this we write inside the tag

This operator communicates with Api chrome and takes the parameters we need from the files we created above.
')
And back to the multilanguage title.

Let before that in the plugin in the title it was written "some text"
What would this inscription be in several languages ​​replace it with

This line refers to the lang function and takes the parameter "some_string" from the files created earlier.

The parameter in the files is indicated as follows:

"some_string": {
"message": "some text"
},


And now, in order to translate all the inscriptions into multilanguage from the beginning, we add their meaning in the files, then we write the line to the place of source texts

That's all.
A live example of the work of the multi-language plugin
Auto HD for YouTube

Source: https://habr.com/ru/post/100628/


All Articles