only the JS file translation does not work
It’s most likely because WordPress couldn’t find your translation file, which should be in a valid JED format, like so:
{
"domain": "my-text-domain",
"locale_data": {
"my-text-domain": {
"": {
"domain": "my-text-domain",
"plural-forms": "n != 1",
"lang": "en-us"
},
"Shortcode Title": [
"Shortcode Title translated"
],
"Display it?": [
"Display it? translated"
],
"Another text 1": [
"Translated text - singular",
"Translated text - plural"
],
"Another text 2": [
"Translated text"
// No plural.
]
}
}
}
And saved with a name in this format: ${domain}-${locale}-${handle}.json
. For example, in your case, it could be my-text-domain-en_US-my-blocks-script.json
if the site’s language is English (United States)
(see “General Settings” → “Site Language”).
And when you call wp_set_script_translations()
, you can specify the third parameter ($path
) which is the full absolute file path to the directory containing the JS/JSON translation files. E.g.:
// $pluginurl was taken from your code
wp_set_script_translations( 'my-blocks-script', 'my-text-domain', $pluginurl . '/languages' );
Also, you can use po2json to convert PO/.po
files to a JED-compatible JavaScript object or JSON string.
Notes:
-
In the above JS/JSON translation data, the locale as in
"lang": "{locale}"
should be the GlotPress locale as you can see in the table on this page. For example,en-gb
forEnglish (UK)
. -
In your JS/JSON translation file name, the locale as in
${domain}-${locale}-${handle}.json
should be the WP locale as you can see in the table on this page. For example,en_GB
forEnglish (UK)
.