Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable taxonomy term translations #17

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

elskwid
Copy link
Contributor

@elskwid elskwid commented Apr 28, 2021

Overview

Enables support for taxonomy term translations that return the correct translated IDs and objects for terms in non-default locales.

get-tags

Details

Adds fields to Taxonomy types to be visible on the terms:

  • locale - the locale of the current term
  • translations - list of TermTranslation objects
  • translated - list of the translated Term objects

Adds two hooks to resolve the issues with WPML term ID adjustment. Basically, WPML adjusts term ids for every request, shifting request terms to the "current language". The WPGraphQL requests don't typically use a current language and that's especially the case with Gatsby. We want to retrieve all of the terms at once and let Gatsby handle the language switching. These two hooks ensure that term IDs are untouched during GraphQL requests allowing retrieval of non-default locale terms.

Adds a slug field to the term translation response to help in the situation where the same slug is used for terms in different locales. The example in the commit message shows what it would look like for the tech tag in two locales:

  • English

    • slug: tech
    • href: https://.../tag/tech
  • Spanish

    • slug: tech
    • href: https://.../es/tag/tech-2

Having the slug allows the client to do what it needs - in our case, it was building out tag pages with the correct tag in the route.

I've also included all of this context and more in the commit messages.

Don Morrison added 5 commits April 27, 2021 19:52
Adds the `TermTranslation` type during type registration to provide
term translation fields since these fields are slightly different
than those for posts.
Adds fields to Taxonomies types, visible on the terms, in the same way
these fields are added to posts.

Adds the following fields:

* `locale` - the locale of the current term
* `translations` - list of `TermTranslation` objects
* `translated` - list of the translated `Term` objects
Adds a hook to remove the WPML `get_term_adjust_id` filter that is
attached to the WP `get_term` function at a very high priority. This
filter causes WPML to adjust term ids for every request - shifting
request terms to the "current language". The problem is that the
WPGraphQL requests don't typically use the current language (and
especially not for use cases like Gatsby). This results in term IDs only
ever being returned for the default language and with single term
queries always returning the default language's term.

The function is added to the `init_graphql_request` action that runs
at the beginning of the GraphQL request cycle. Using this action
ensures that we're making adjustments for only "graphql" requests and
that those changes are early enough in the cycle to get in front of
the WPML filters.
Adds a hook to set the `$icl_adjust_id_url_filter_off` global to
`true`. This global is used in the `WPML_Term_Adjust_Id->filter()` to
indicate if term ids should be adjusted. Setting this global protects
against another hook or filter turning this feature back on.

The function is added to the `init_graphql_request` action that runs
at the beginning of the GraphQL request cycle. Using this action
ensures that we're making adjustments for only "graphql" requests and
that those changes are early enough in the cycle to get in front of
the WPML filters.

Setting this to `true` is another guard against WPML id adjustment that
results in only ever seeing the terms for the default language.
Adds the `slug` field to the term translations response (type and
resolver) to help when a term may have the same slug between different
locales. When the same slug is used, WordPress will add a numerical
suffix to the href/link even though the same slug may be desired.

Adding the slug to the response allows the client to use the slug when
generating routes. For example, imagine the situation with two locales:
English and Spanish. Creating a `tech` tag in both locales would look
like this:

  * English
    - slug: `tech`
    - href: `https://.../tag/tech`

  * Spanish
    - slug: `tech`
    - href: `https://.../es/tag/tech-2`

With the slug and the href, the client can use whatever works best in
that context.
* run. There is a WPML setting named `auto_adjust_ids` that will turn
* off this feature, but it should never be on for GraphQL queries.
*/
function wpgraphqlwpml__remove_term_adjust_id_filter() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to double check this, I am currently working on filtering menus and menu items and had a case where I actually need to get the adjusted ids.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rburgst no problem! We could probably set it conditionally if needed. We were lucky enough to not need any menu data for our project but the adjusted ids caused some serious havoc with Gatsby pulling translated terms.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, we could use this or the constant below and set them around the resolver functions if that would work better for you.

* should be adjusted. Setting this global to `true` ensures that
* term ids are not adjusted during GraphQL queries.
*/
function wpgraphqlwpml__set_global_adjust_id_filter_off() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused here, doesnt do this the same as line 773?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rburgst yes, but they're used in different places in WPML. I added both to be sure we were covering our bases. Like I said here, we could set these around the resolver functions if that helps isolate the impact.

@rburgst
Copy link
Owner

rburgst commented May 2, 2021

Thanks a lot for your contribution, however, one of my queries produces worse results with your patch than before, could you please check why this is the case?

query MenuLocationQuery {
  menus(first: 40) {
    nodes {
      id
      databaseId
      slug
      name
      locations
      language
    }
  }
}

previously returned

{
  "data": {
    "menus": {
      "nodes": [
        {
          "id": "dGVybTo2Mw==",
          "databaseId": 63,
          "slug": "be-part-of-it",
          "name": "Be Part Of It",
          "locations": [
            "BE_PART_MENU"
          ],
          "language": "en"
        },
        {
          "id": "dGVybToxOTg0",
          "databaseId": 1984,
          "slug": "concerts-events",
          "name": "Concerts & Events",
          "locations": [
            "CONCERTS_MENU"
          ],
          "language": "en"
        },

after your change, the locations for the non-default language menu elements seems to be gone

{
  "data": {
    "menus": {
      "nodes": [
        {
          "id": "dGVybTo2Mw==",
          "databaseId": 63,
          "slug": "be-part-of-it",
          "name": "Be Part Of It",
          "locations": null,
          "language": "en"
        },
        {
          "id": "dGVybToxOTg0",
          "databaseId": 1984,
          "slug": "concerts-events",
          "name": "Concerts & Events",
          "locations": null,
          "language": "en"
        },
        {

I am pretty certain this is due to the fact that now all getTerm calls are now no longer translated and therefore, you would probably need to resolve the location programmatically in the case where you are checking the location for a menu that is in a different language. Note that the menu location ids are different per language.

@elskwid
Copy link
Contributor Author

elskwid commented May 3, 2021

@rburgst We don't use menus anywhere so I'll need to take a look at that. Thank you for the thorough response.

@janmei
Copy link

janmei commented Jul 21, 2021

Is there any progress on this?
It is a feature i am really missing in this extension and my php knowledge is to weak to develop this further.

@Ririshi
Copy link

Ririshi commented May 2, 2023

Sorry to ping an old thread like this, but since the PR is still open: have you been able to do any further research on this, @elskwid? Figuring out this PR might solve #27, #34 and #44.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants