Translations Hooks
WordPress Filters relating to detecting & translating words
Weglot plugin will detect all the words in your HTML. To do this, it will parse the DOM in PHP and detect the inner text of HTML nodes but also some attributes we have defined, like the "placeholder" attribute in a <input> node or the "alt" attribute of an img node.
This usually ensure all the text in your page is detected and get translated. However, in some case, your text can be located in other location in your HTML. Like it could be in a JavaScript variable like this
1
<script>
2
var myAwesomeVar = 'This is a text I would like to translate';
3
</script>
Copied!
In that case, your text will not be detected and not be translated.
This is where you will use filters to extend the definitions of the Weglot Parser and instruct it to detect other text

weglot_get_dom_checkers

This filter extend the list of HTML nodes and attribute that are being translated.
Argument
Type
Description
$dom_checkers
String
Names of used DomChecker (extends Weglot\Parser\Check\Dom\AbstractDomChecker)

Use case

It can happen in your HTML that you use data-attributes that are not translated by default. Example with data-slide-title
1
<div data-slide-title="Title slide">
2
New project !
3
</div>
Copied!
With this the weglot_get_dom_checkers filter, you will extend the list of "Dom checkers" by adding a class in the list like on the following example. Y
1
<?php
2
3
add_filter( 'weglot_get_dom_checkers', 'custom_weglot_dom_check' );
4
function custom_weglot_dom_check( $dom_checkers ) { //$dom_checkers contains the list of all the class we are checking by default
5
6
class Div_Slide_Title extends Weglot\Parser\Check\Dom\AbstractDomChecker {
7
const DOM = 'div'; //Type of tag you want to detect // CSS Selector
8
const PROPERTY = 'data-slide-title'; //Name of the attribute in that tag uou want to detect
9
const WORD_TYPE = Weglot\Client\Api\Enum\WordType::TEXT; //Do not change unless it's not text but a media URL like a .pdf file for example.
10
}
11
$dom_checkers[] = '\Div_Slide_Title'; //You add your class to the list because you want the parser to also detect it
12
return $dom_checkers ;
13
}
14
Copied!

weglot_get_regex_checkers

This filter is a bit more tricky to understand but also very powerful. It allows you to give a Regex to the parser in order for it to detect the text you want in your DOM.
Argument
Type
Description
$regex_checkers
array
Used RegexChecker
$regex_checkers is an array that contains the RegexChecker objects, which determine what elements to treat as well as the type of data it is (TEXT, HTML or JSON).
1
\Weglot\Parser\Check\Regex\RegexChecker( $regex = '' , $type = '' , $var_number = 0 , $keys = array() );
Copied!
The RegexChecker constructor settings are as follows:
    $regex: Regex that target the element you want to be parsed
    $type: String variable that determines the type of the targeted element ('TEXT', 'HTML' or 'JSON')
    $var_number: Denotes the number of variable targeted by the regex that you want to parse
    $key: If the element being treated is a ‘JSON’ element, this variable allows you to specify keys to translate
    $decode_function: Function callback applied to intercepted content
    $encode_function: Function callback applied to returned content

Use case

Below are several examples of content that will not be translated by default by Weglot, but that you can translate using this filter
1
<script type="text/javascript">
2
jQuery(document).ready(function ($) {
3
4
// Untranslate text
5
alert("My untranslate text");
6
console.log("My untranslate text", "My untranslate text");
7
8
// Untranslate HTML
9
$("body").append("<p>My <strong>untranslate</strong> text</p>");
10
11
// Untranslate JSON values
12
var myJson = {
13
"MyKey1": {
14
"MyKey1-1": "My untranslate text",
15
"MyKey1-2": "My untranslate text"
16
},
17
"MyKey2": "My untranslate text",
18
"some_array" : [ "Hello" , "Translate this"]
19
};
20
21
});
22
</script>
23
<div data-escapedjson="{&quot;schema&quot;:{&quot;content&quot;:{&quot;desktop&quot;:&quot;&lt;h1&gt;Translate it&lt;\/h1&gt;&quot;}}}"><h1>This, no problem</h1></div>
Copied!
In order for the content in this example to be interpreted, and then translated, we will use the weglot_get_regex_checkers filter as followed.
1
<?php
2
3
add_filter( 'weglot_get_regex_checkers', 'custom_weglot_add_regex_checkers' );
4
5
function custom_weglot_add_regex_checkers( $regex_checkers ) {
6
7
// Text
8
$regex_checkers[] = new \Weglot\Parser\Check\Regex\RegexChecker( '#alert\(\"(.*)\"\);#', 'TEXT', 1 );
9
$regex_checkers[] = new \Weglot\Parser\Check\Regex\RegexChecker( '#console\.log\(\"(.*?)\",.*?\"(.*?)\"\);#', 'TEXT', 2 );
10
11
// HTML
12
$regex_checkers[] = new \Weglot\Parser\Check\Regex\RegexChecker( '#\$\(\"body\"\)\.append\(\"(.*)\"\);#', 'HTML', 1 );
13
14
// JSON
15
$regex_checkers[] = new \Weglot\Parser\Check\Regex\RegexChecker( '#var myJson = ((.|\s)+?);#', 'JSON', 1, array('MyKey1-1', 'MyKey1-2', 'MyKey2' , 'some_array') );
16
17
//More advanced : JSON after a callback
18
$regex_checkers[] = new \Weglot\Parser\Check\Regex\RegexChecker( '#data-escapedjson="((.|\s)+?)"#', 'JSON', 1, array(), "html_entity_decode" , "htmlentities" );
19
20
//More advanced : JSON after a callback, version with another regex
21
$regex_checkers[] = new \Weglot\Parser\Check\Regex\RegexChecker( '#data-escapedjson="(.*)"#', 'JSON', 1, array(), "html_entity_decode" , "htmlentities" );
22
23
return $regex_checkers;
24
}
Copied!

weglot_add_json_keys

Weglot also translates JSON response but not all values. Use this filter to target specific values.
Argument
Type
Description
$keys
array
Array of string
The JSON values translated by default are:
    Value with key: "name"
    Value with key: "description"
    Value in HTML format

Use case

You may come across values used in your JSON that are not translated by default.Here’s an example using the message value:
1
{
2
"name":"My name value, already translated", //Will be translated by default because key is "name"
3
"description":"My description value, already translated", //Will be translated by default because key is "description"
4
"my_custom_key":"<p>HTML content, already translated</p>", //Will be translated by default because we detect this is HTML
5
"message":"My message value to translate!" //This will not be translated and you will need to use the filter
6
}
Copied!
To do this, we use weglot_add_json_keys filter.
1
<?php
2
3
add_filter( 'weglot_add_json_keys', 'custom_weglot_add_json_keys' );
4
function custom_weglot_add_json_keys( $keys ){ //$keys already contains "name" and "description"
5
$keys[] = 'message'; //This tells Weglot to also look for key "message" when detecting content to translated
6
return $keys;
7
}
Copied!
Also, note that if a URL is detected in a value of the JSON, it will be replaced by the URL with the language code if and only if it is one of the redirecturl , url , link . For example, if you original JSON is
1
{
2
"url" : "https://mysite.com/contact",
3
"redirectURL" : "https://mysite.com/contact",
4
"niceURL" : "https://mysite.com/contact",
5
"name" : "This is my name"
6
}
Copied!
The translated response would be
1
{
2
"url" : "https://mysite.com/fr/contact",
3
"redirectURL" : "https://mysite.com/fr/contact",
4
"niceURL" : "https://mysite.com/contact",
5
"name" : "C'est mon nom"
6
}
Copied!
Adding keys to check when replacing URL will be done weglot_ajax_replace_urls

weglot_words_translate

Use this filter to target a specific word literally in your source code
Argument
Type
Description
$words
array
Array of string
You can add words that are present in your HTML page but not translated. It's useful when a word is not being translated by Weglot because it's inside a JavaScript for example and you can't really use other filters.

Use case

1
<?php
2
3
add_filter( 'weglot_words_translate', 'custom_weglot_words_translate' );
4
function custom_weglot_words_translate( $words ){
5
$words[] = "Monday";
6
$words[] = "Tuesday";
7
$words[] = "Nice to meet you";
8
return $words;
9
}
Copied!
This filter will tell Weglot to look literally for words in your source code, then translate it and literally replace these words in your source code. Be very careful to not enter keywords as "head" "body" for example as it could break your page.

weglot_html_treat_page

Use this filter when there is no other solution : It's a very powerful filter that allows you to make manual edition on the final translated DOM in PHP.
Argument
Type
Description
$html
string
HTML content of translated page

Use case 1

The following code replaces all of the “https://codex.wordpress.org/” links with “https://codex.wordpress.org/fr:Accueil” in the translated versions.
1
<?php
2
3
add_filter( 'weglot_html_treat_page', 'custom_weglot_html_treat_page_1' );
4
function custom_weglot_html_treat_page_1( $html ) {
5
6
$s = 'https://codex.wordpress.org/';
7
$r = 'https://codex.wordpress.org/fr:Accueil';
8
9
$html = str_replace( $s, $r, $html );
10
return $html;
11
}
Copied!

Use case 2

In this example, the replacement occurs according to the chosen language.
1
<?php
2
3
add_filter( 'weglot_html_treat_page', 'custom_weglot_html_treat_page_2' );
4
function custom_weglot_html_treat_page_2( $html ) {
5
6
$search = 'https://codex.wordpress.org/';
7
8
switch ( weglot_get_current_language() ) {
9
case 'fr':
10
$html = str_replace( $search, 'https://codex.wordpress.org/fr:Accueil', $html );
11
break;
12
case 'pt':
13
$html = str_replace( $search, 'https://codex.wordpress.org/pt:Página_Inicial', $html );
14
break;
15
}
16
17
return $html;
18
}
19
Copied!
Note, you can use weglot_render_dom instead, which is the same except there is the HTML of the button and the links are already translated.
Last modified 2mo ago