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

query field translations + richtext #716

Merged
merged 8 commits into from
Oct 10, 2024
Merged

Conversation

christinaroise
Copy link
Contributor

@christinaroise christinaroise commented Sep 26, 2024

Updated GROQ queries to take language into account. This way we only fetch the translation for the current language, and web components don't have to handle unpacking internationalized strings. The downside is added complexity in the GROQ queries. This is somewhat alleviated by defining reusable query "fragments".

Examples:

export const TRANSLATED_LINK_FRAGMENT = groq`
    ${LANGUAGE_FIELD_FRAGMENT},
    ${TRANSLATED_LINK_TITLE_FRAGMENT},
    ${TRANSLATED_INTERNAL_LINK_FRAGMENT}
`;

const SECTIONS_FRAGMENT = groq`
  sections[]{
    ...,
    _type == "hero" => {
      ...,
      callToActions[] {
        ...,
        ${TRANSLATED_LINK_FRAGMENT}
      }
    },
    _type == "article" => {
      ...,
      link {
        ...,
        ${TRANSLATED_LINK_FRAGMENT}
      }
    },
    _type == "callout" => {
      ...,
      link {
        ...,
        ${TRANSLATED_LINK_FRAGMENT}
      }
    },
    _type == "ctaSection" => {
      ...,
      callToActions[] {
        ...,
        ${TRANSLATED_LINK_FRAGMENT}
      }
    }
  }
`;
export function translatedFieldFragment(fieldName: string) {
  return groq`${fieldName}[_key == $language][0].value`;
}

// ...

export const COMPENSATIONS_PAGE_QUERY = groq`
  *[_type == "compensations" && ${translatedFieldFragment("slug")} == $slug][0] {
    ...,
    "language": $language,
    "slug": ${translatedFieldFragment("slug")},
    "basicTitle": ${translatedFieldFragment("basicTitle")},
    "benefitsByLocation": benefitsByLocation[] {
      ...,
      "benefits": benefits[] {
        ...,
        "basicTitle": ${translatedFieldFragment("basicTitle")},
        "richText": ${translatedFieldFragment("richText")}
      }
    },
    "seo": ${translatedFieldFragment("seo")}
  }
`;

As an example on how to add translations to richText, this PR adds richtext translations in benefit

Visual Overview (Image/Video)

Translated rich text in benefits:
Screenshot 2024-09-26 at 09 55 12

Checklist

Please ensure that you’ve completed the following checkpoints before submitting your pull request:

  • Documentation: Relevant documentation has been added or updated (if applicable).
  • Testing: Have you tested your changes thoroughly?

Copy link

vercel bot commented Sep 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
variant-no ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 10, 2024 7:07am
variant-se-2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 10, 2024 7:07am

@anemne
Copy link
Contributor

anemne commented Oct 7, 2024

You should add to PR description that you have added localized value for title as well.

@mathiazom mathiazom force-pushed the add-translations-to-richText branch from 7c9d77f to a4f8559 Compare October 8, 2024 11:09
@mathiazom mathiazom changed the title add richtext as field translations query field translations + richtext Oct 8, 2024
@mathiazom mathiazom requested a review from anemne October 8, 2024 11:14
@mathiazom
Copy link
Contributor

"Hijacked" this PR to also include handling of field translations in GROQ queries.

Copy link
Contributor Author

@christinaroise christinaroise left a comment

Choose a reason for hiding this comment

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

make sure you also update the PR description

studio/schemas/objects/link.ts Outdated Show resolved Hide resolved
studio/schemas/objects/link.ts Outdated Show resolved Hide resolved
studio/lib/interfaces/global.ts Show resolved Hide resolved
studio/lib/queries/pages.ts Outdated Show resolved Hide resolved
studio/lib/queries/pages.ts Outdated Show resolved Hide resolved
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.

3 participants