wp-graphql-polylang icon indicating copy to clipboard operation
wp-graphql-polylang copied to clipboard

Query string translations

Open maweo-mathis opened this issue 3 years ago • 5 comments

Hello,

is it possible to query string translations via GraphQL with that plugin? I didn't find a solution yet...

maweo-mathis avatar Jul 20 '21 12:07 maweo-mathis

Let's say the site tagline has been translated from English (default) to Spanish (secondary). To get the Spanish string:

query MyQuery {
  translateString(language: ES, string: "Just another WordPress site")
}

The result:

{
  "data": {
    "translateString": "Solo otro sitio de WordPress"
  }
}

If I understood right, if you have more secondary languages, you can't get the translations between them directly, leaving out the default one. In this example, trying to get Russian from Spanish will return Spanish again.

query MyQuery {
  translateString(language: RU, string: "Solo otro sitio de WordPress")
}
{
  "data": {
    "translateString": "Solo otro sitio de WordPress"
  }
}

maxyudin avatar Sep 25 '21 19:09 maxyudin

@maxyudin Thanks for the answer, but I get this error: "message": "Cannot query field \"translateString\" on type \"Query\".",

Query:

query MyQuery {
  translateString(language: ES, string: "Just another WordPress site")
}

maweo-mathis avatar Oct 29 '21 11:10 maweo-mathis

Unfortunately, I can't reproduce your issue. Are you sure WP GraphQL Polylang is enabled?

maxyudin avatar Oct 30 '21 08:10 maxyudin

I was having trouble using this feature and finally got it. You have to call the $string registered in pll_register_string function, not the $name. For example, I have this string registered pll_register_string("contact-footer", "Contato", "Theme") and have to call it like this translateString(language: PT, string: "Contato"). The problem now is how to get multiple strings with only one query.

assef avatar Oct 08 '23 22:10 assef

Solved the problem I mentioned above. To query multiple strings in only one query you have to give it multiple aliases like this: contactFooter:translateString(language: PT, string: "Contato") subscribeFooterTitle:translateString(language: PT, string: "Inscreva-se em nossa newsletter.")

assef avatar Oct 08 '23 23:10 assef