jlarmstrongiv / astro-i18n-aut

The i18n integration for Astro 🧑‍🚀
https://www.npmjs.com/package/astro-i18n-aut
MIT License
125 stars 12 forks source link

How to generate posts based on the current language request? #41

Closed tiwka19 closed 10 months ago

tiwka19 commented 10 months ago

I request posts via the API and create them via the getStaticPath function

const { slug } = Astro.params;
export async function getStaticPaths() {
  const directus = await getDirectusClient();
  const data = await directus.query(`
    query { partners (filter: {status: { _eq: "published"}}) { slug  } }
  `);
  return data.partners.map((item) => ({
    params: { slug: item.slug.replace("en" + "/", "") },
  }));
}

but I have a different number of posts for each language.

then I create a second query and pull the full post information for the current slug

const data = await directus.query(`
    query {
        partners (filter: {status: { _eq: "published"} slug: { _eq: "${slug}" }
        translations: { 
          title: {_nempty: true}
          content: {_nempty: true}
        }
      })  {
          name
          link
          promocode
          logo { filename_disk }
          css_background
          thumbnail { filename_disk }
          translations(filter: { languages_code: { code: { _eq: "${locale}" } } }) {
            title
            description
            content
            offer 
          }
        }
      }
`);

but when I build, posts appear in the other language that shouldn't be there, and the build breaks because I get undefined and try to output it to markup