Translate webpage

Mimicks chrome's built-in page translator. Can be used as a userscript (from the context menu or always on) or as a bookmarklet (clicking the bookmark translates the page). For Firefox and chrome

نووسەر
Achernar
Daily installs
2
Total installs
739
Ratings
3 0 0
وەشان
0.5.1
Created
2023-07-15
Updated
2023-07-15
مۆڵەت
نییە
Applies to
هەموو سایتەکان

This userscript mimicks chrome's built-in page translator. It can be used as a userscript (from the context menu, or, always on) or as a bookmarklet (clicking the bookmark translates the page). Works in Firefox and in chrome. It has been tested with Tampermonkey and Violentmonkey.


Since June 2023, the built-in translation in old versions of chrome has stopped working. This sparked the idea to bring the feature back to those users who are still sticking to their old programs, and make it more flexible and browser independant. It should works in all versions of Firefox and chromium-based browsers. If you experience problem in older versions, feel free to contact me.
The only cave-at is that it uses google translate. You are warned.

Webmasters have always been able to add a widget to integrate the service to their site to allow their foreign visitors to translate the page on the fly.
The userscript is using this script on all pages, which allows complete inline translation of pages and frames.


When used as a bookmarklet, it integrates the translation toolbar with the default capabilities. Except for a few css tunings to fix some issues:
  • The toolbar is hidden. It appears when the mouse pointer is moved in the top 4 pixels of the viewport.
  • The boxes displaying the list of languages is scrollable to accommodate lower resolutions.

The target language is saved site by site in a session cookies. A default language can be set. See below in the section on how to install as a bookmarklet.

When no target language


When used as a userscript, buttons and menu items are added:
  • The button "Set as default language" saves the current default target language for your translation.
  • A button with the current default language serves as a shortcut to set the current target language when you've changed it (see the screenshot below).

The menu has other commands: (items are usually hidden and are all displayed here as a convenience)

  • The first one now also save the choice in the script config
  • The second allows to revert your choice
  • You can turn off translation on all sites. A trigger will be added to the webpages to start the translation process, but no connection to the service is made if you don't click it.
  • You can turn off globally while allowing for the current site. You can still allow/disallow individual sites with the first 2 commands
  • You can revert translation on for all site
  • The last item at the bottom ¿? , is there if the language used in the toolbar is incorrect. When clicked it selects the language used in your browser's interface and displays a list of country codes to choose from. You'll have to reload the page to take effect.
    Selecting the first item (empty) sets it back to autodetection.


How to use

Note that the userscript has the advantage to update automatically when a new version is released. For the bookmarklet you'll have to do it yourself.

As a userscript (context menu)
By default when installed in Tampermonkey, the userscript is accessible through the browser's context-menu of the page. Right-click over the section of a page (eg. a frame), click on "Tampermonkey" and then on the userstyle's name in the submenu. This is the prefered method if you don't need to translate too often.
This functionality is not available in Violentmonkey where the other method is used.

As a userscript
In this configuration, all the websites will be subjected to translation. You can disable this for each site separately, but the default option is to translate. You can configure the script to work the opposite way: never translate unless called from the trigger, or if the site is set to auto-translate.

This appears (hidden by default) in the top right corner of a page where auto-translation is disabled. Move the mouse pointer there where an area 3 by 3 pixels triggers the display of the symbol. Click it to start the translation. The translation will start, or the "Select language" dialog appear if the script doesn't know the language to use.


Switching between the 2 configurations (Tampermonkey only)

In Tampermonkey, open the userscript, click the "Settings" tab. In the 2nd section, "General" "Run at", change between Default (or context-menu) and document-idle. When document-idle is set the context menu entry disappears.


As a bookmarklet
  • Create a new bookmark that you place for easy access in the bookmarks toolbar or in a submenu of the bookmarks toolbar.
  • Open the bookmarks manager and edit that bookmark.
  • Visit the page with the userscript source code. Select everything below ==/UserScript==, starting with javascript: up until the end.
  • Go to the bookmark manager and paste in the url field of the bookmark. If you want to set a default target language, save the bookmark without closing the manager.
  • Edit the bookmark. Go to the url field. Hit the "home" key to go to the beginning of the code
  • Type your language code (country code, en for english, fr for french, etc.) between the "" after var defLang=
    If you don't know the country code, type a ? . The userscript will try to use the language of the browser.

  • If the language used to display the toolbar isn't what you want, you can do the same thing but between the "" after toolbarLang=

Notes

If a cookie is set, it has precedence over other configurations.

  • The current target language is ignored. If you change the default to another language, and then go back to a site recently visited, it will use the previous language. (the cookie was set at that time).
  • If a site is set to never translate (global or site-specific), it will translate as set by the cookie.
  • Cookies can be discarded either by clicking the cross icon in the toolbar ; by clicking "Show Original" ; when the browser is restarted and the session cookies are erased.

The session cookie is always set when a translation is attempted. Except for sites where cookies are blocked by configuring your browser.


The userscript contains several translations for the added buttons and menu items. Except for languages that I know, or poorly know, they are taken as-is from google translate. Forgive me for the errors.
The translations included are: french, dutch, spanish, italian, german, portuguese, japanese, chinese (simplified and traditionnal).

If the website that you want to translate is configured to block inline script, the translation won't work. Parts of google translate scripts will be blocked.

If you encounter any problem when running it in older browsers, feel free to contact me.

For more userscripts, styles, extensions, tips, ..., visit my programming page.