Extensions Reference
Extensions ReferenceGoogle Translate

Google Translate

Directive @strTranslate to translate a field value to over 130 languages using the Google Translate API.

Description

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:

{
  posts {
    enTitle: title
    frTitle: title @strTranslate(from: "en", to: "fr")
 
    enExcerpt: excerpt    
    frExcerpt: excerpt @strTranslate(from: "en", to: "fr")
  }
}

...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."
      }
    ]
  }
}

List of languages

You can translate your content to any of the following languages:

CodeLanguage
afAfrikaans
sqAlbanian
amAmharic
arArabic
hyArmenian
asAssamese
ayAymara
azAzerbaijani
bmBambara
euBasque
beBelarusian
bnBengali
bhoBhojpuri
bsBosnian
bgBulgarian
caCatalan
cebCebuano
zh-CN or zh (BCP-47)Chinese (Simplified)
zh-TW (BCP-47)`Chinese (Traditional)
coCorsican
hrCroatian
csCzech
daDanish
dvDhivehi
doiDogri
nlDutch
enEnglish
eoEsperanto
etEstonian
eeEwe
filFilipino (Tagalog)
fiFinnish
frFrench
fyFrisian
glGalician
kaGeorgian
deGerman
elGreek
gnGuarani
guGujarati
htHaitian Creole
haHausa
hawHawaiian
he or iwHebrew
hiHindi
hmnHmong
huHungarian
isIcelandic
igIgbo
iloIlocano
idIndonesian
gaIrish
itItalian
jaJapanese
jv or jwJavanese
knKannada
kkKazakh
kmKhmer
rwKinyarwanda
gomKonkani
koKorean
kriKrio
kuKurdish
ckbKurdish (Sorani)
kyKyrgyz
loLao
laLatin
lvLatvian
lnLingala
ltLithuanian
lgLuganda
lbLuxembourgish
mkMacedonian
maiMaithili
mgMalagasy
msMalay
mlMalayalam
mtMaltese
miMaori
mrMarathi
mni-MteiMeiteilon (Manipuri)
lusMizo
mnMongolian
myMyanmar (Burmese)
neNepali
noNorwegian
nyNyanja (Chichewa)
orOdia (Oriya)
omOromo
psPashto
faPersian
plPolish
ptPortuguese (Portugal, Brazil)
paPunjabi
quQuechua
roRomanian
ruRussian
smSamoan
saSanskrit
gdScots Gaelic
nsoSepedi
srSerbian
stSesotho
snShona
sdSindhi
siSinhala (Sinhalese)
skSlovak
slSlovenian
soSomali
esSpanish
suSundanese
swSwahili
svSwedish
tlTagalog (Filipino)
tgTajik
taTamil
ttTatar
teTelugu
thThai
tiTigrinya
tsTsonga
trTurkish
tkTurkmen
akTwi (Akan)
ukUkrainian
urUrdu
ugUyghur
uzUzbek
viVietnamese
cyWelsh
xhXhosa
yiYiddish
yoYoruba
zuZulu

Google Translate API Configuration

In order to use Google Translate, it is mandatory to provide the API key, via tab "Plugin Management => Google Translate API" on the Settings page.

Create the API key in Google Cloud Platform, following the instructions here.

Then follow one of the methods below to input the value.

By Settings

Input the API key in the corresponding input in the Settings page, and click on "Save Changes (All)":

Setting Google Translate's API key
Setting Google Translate's API key

In wp-config.php

Add constant GATOGRAPHQL_GOOGLE_TRANSLATE_API_KEY in wp-config.php:

define( 'GATOGRAPHQL_GOOGLE_TRANSLATE_API_KEY', 'your API key' );

By environment variable

Define environment variable GOOGLE_TRANSLATE_API_KEY.

Schema Configuration

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 => Google Translate" on the Settings page. These values will be used whenever any of those arguments is not provided in the query:

{
  posts {
    title @strTranslate
  }
}

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.

By Settings

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
Setting the default 'from' and 'to' languages

In wp-config.php

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' );

By environment variable

Define environment variables:

  • TRANSLATION_DEFAULT_FROM_LANG_CODE
  • TRANSLATION_DEFAULT_TO_LANG_CODE