Directive @strTranslate
to translate a field value using any provider's API.
Add directive @strTranslate
to any field of type String
, to translate it to the desired language.
For instance, this query translates the post's title
and excerpt
fields from English to French (using the default API provider):
{
posts {
enTitle : title
frTitle : title @strTranslate ( from : "en" , to : "fr" )
enExcerpt : excerpt
frExcerpt : excerpt @strTranslate ( from : "en" , to : "fr" )
}
}
Copy
...producing:
{
"data" : {
"posts" : [
{
"enTitle" : "Welcome to a single post full of blocks!" ,
"frTitle" : "Bienvenue dans un poste unique plein de blocs !" ,
"enExcerpt" : "When I look back on my past and think how much time I wasted on nothing, how much time has been lost in futilities, errors, laziness, incapacity to live; how little I appreciated it, how many times I sinned against my heart and soul-then my heart bleeds. Life is a gift, life is happiness, every…" ,
"frExcerpt" : "Quand je repense à mon passé et que je pense au temps que j'ai perdu pour rien, au temps perdu en futilités, en erreurs, en paresse, en incapacité de vivre ; combien je l'ai peu apprécié, combien de fois j'ai péché contre mon cœur et mon âme, alors mon cœur saigne. La vie est un cadeau, la vie est un bonheur, chaque…"
},
{
"enTitle" : "Explaining the privacy policy" ,
"frTitle" : "Expliquer la politique de confidentialité" ,
"enExcerpt" : "Our privacy policy is at https://gato-graphql-pro.lndo.site/privacy/, and we are based in Carimano." ,
"frExcerpt" : "Notre politique de confidentialité se trouve sur https://gato-graphql-pro.lndo.site/privacy/, et nous sommes basés à Carimano."
},
{
"enTitle" : "HTTP caching improves performance" ,
"frTitle" : "La mise en cache HTTP améliore les performances" ,
"enExcerpt" : "Categories Block Latest Posts Block Did you know? We are not rich by what we possess but by what we can do without. Patience is the strength of the weak, impatience is the weakness of the strong." ,
"frExcerpt" : "Catégories Bloquer les derniers messages Bloquer Le saviez-vous ? Nous ne sommes pas riches de ce que nous possédons mais de ce dont nous pouvons nous passer. La patience est la force du faible, l'impatience est la faiblesse du fort."
}
]
}
}
Copy
Directive @strTranslate
requires passing two arguments:
from
: the language code of the text
to
: the language code to translate to
We can define a default value for these properties in tab "Schema Configuration => Translation" on the Settings page. These values will be used whenever any of those arguments is not provided in the query:
{
posts {
title @strTranslate
}
}
Copy
In addition, when defining default values, the corresponding argument in the GraphQL schema becomes non-mandatory.
By default, the default from
value is the language used in WordPress.
Input the from
/to
fields in the corresponding input in the Settings page, and click on "Save Changes (All)":
Setting the default 'from' and 'to' languages
Add constants in wp-config.php
:
GATOGRAPHQL_TRANSLATION_DEFAULT_FROM_LANG_CODE
GATOGRAPHQL_TRANSLATION_DEFAULT_TO_LANG_CODE
For instance:
define ( 'GATOGRAPHQL_TRANSLATION_DEFAULT_TO_LANG_CODE' , 'fr' );
Copy
Define environment variables:
TRANSLATION_DEFAULT_FROM_LANG_CODE
TRANSLATION_DEFAULT_TO_LANG_CODE