Composables

useAttributeTranslations

Description

The useAttributeTranslations composable is responsible for fetching and managing product attribute translations based on the current locale in a Nuxt-based e-commerce application. It ensures that the correct translations are loaded either from a local cache or by fetching them from a CDN. The composable also manages language change events by watching the locale and reloading the translations when the language changes. It helps in providing localized product attribute information for a seamless user experience across different languages.

FEATURE GOALS

  • Fetch and manage product attribute translations based on the current locale.
  • Utilize a local cache (product store) to minimize unnecessary requests.
  • Load translations from a CDN when necessary.
  • Ensure translations are automatically updated when the locale changes.
  • Provide error handling for any failures in the translation fetching process.

FEATURE EXPECTATIONS

  • Translations are fetched from either a local cache or a CDN based on the current locale.
  • Locale changes trigger a re-fetch of translations.
  • The composable gracefully handles errors during the fetch process and logs them for debugging.
  • Caching is used to prevent repeated fetching of the same data for the same locale.

DEV STRATEGIES

  1. Composables Logic
const { translations, fetchTranslations } = useAttributeTranslations();

// Fetch translations manually
await fetchTranslations();

// Translations will be available as `translations.value`
console.log(translations.value);
  • Translations are fetched based on the current locale, and the fetched data is stored in the translations reference.
  • The translations are cached using the productStore to prevent unnecessary network requests.
  • The locale is watched, and if it changes, the translations are automatically re-fetched for the new language.
  1. COMPOSABLE STRUCTURE
useAttributeTranslations(): {
  translations: Ref<Record<string, string>>; 
  fetchTranslations: () => Promise<void>;
}
  1. DEFAULT BEHAVIOR
OptionDefaultDescription
localeuseI18n().localeThe current locale, used to determine the appropriate translation set.
translations{}A reference to the translations object, populated after a successful fetch.
productStoreuseProductStore()A store that holds cached translations to reduce redundant API calls.

  1. ERROR RESPONSE STRUCTURE
  • If there is an error fetching the translations, the composable handles it and calls a custom error handler:
{
  success: false,
  error: string // Describes the error that occurred during the translation fetch
}
  • The error details are logged with contextual information (e.g., cacheKey, locale).

FLOW SUMMARY

  • The composable first checks if translations for the current locale are available in the local cache (productStore).
  • If not found, it fetches the translations from a CDN.
  • When the locale changes, translations are automatically re-fetched for the new language.
  • If fetching fails, the error is logged using a custom error handler.
  • The composable uses a reactive reference (translations) to store the fetched translations and make them available for use within the application.

Copyright © 2026