LogoLogo
❤️ SponserMore
  • 🚀Getting Started
    • Installation
    • Angular Compatibility
    • Config Options
  • 💡Core Concepts
    • Translation in the Template
    • Signals
    • Translation API
    • Language API
  • 🧠Advanced Topics
    • 🦥Lazy Load
      • Scope Configuration
      • Inline Loaders
    • The Transpiler
    • SSR Support
    • Unit Testing
    • Hack the Library
  • 🧪Sandbox & Examples
  • ⚙️Additional Functionality
    • Loading Template
    • Key Referencing
    • Utility Functions
    • Comments for Translators
    • Multiple Languages Simultaneously
  • 📦Migration Guides
    • Migrate from ngx-translate
    • Migrate from Angular's i18n
  • 🔧Tools
    • Keys Manager (TKM)
      • Keys Extractor
      • Keys Detective
      • Options
      • Debugging
      • Using with Nx 🐋
    • Validator
    • Optimize
    • Scoped Library Extractor
  • 🔌Plugins
    • Message Format
    • Persist Translations
    • Persist Lang
    • Preload Langs
    • Locale l10n
    • Community Plugins
  • 🧰Schematics
    • :ng-add
    • :scope
    • :join
    • :split
  • 🍳Recipies
    • Prefetch User Language
    • Using Xliff
    • Generate Locale Files using Google Translate
  • 📚Blog Posts
    • Transloco Team Posts
      • Transloco Goes Functional: A Guide to Transloco's Functional Transpiler
    • From the Community
  • ❓FAQs
Powered by GitBook

@ 2025 Transloco

On this page

Was this helpful?

  1. Recipies

Prefetch User Language

To enhance user experience, it's recommended to preload the user's data, including internationalization settings, before allowing interaction with components. This ensures that the necessary data is available upfront, preventing issues like jumpy content or flickering CSS.

You can achieve this by utilizing the APP_INITIALIZER token, which runs initialization logic before the app is bootstrapped.

Here’s how you can implement it:

import { provideAppInitializer } from '@angular/core';
import { UserService } from './user.service';
import { TranslocoService } from '@jsverse/transloco';

export function preloadUser(userService: UserService, transloco: TranslocoService) {
  return async () => {
    const config = await userService.getUser();
    // Set the language
    transloco.setActiveLang(config.lang);
    // Load the translation file
    await transloco.load(config.lang);
  };
}

export const preLoad = provideAppInitializer(preloadUser, [UserService, TranslocoService]);
import { APP_INITIALIZER } from '@angular/core';
import { UserService } from './user.service';
import { TranslocoService } from '@jsverse/transloco';

export function preloadUser(userService: UserService, transloco: TranslocoService) {
  return async function() {
     const config = await userService.getUser();
     // Set the language
     transloco.setActiveLang(config.lang);
     // Load the translation file
     await transloco.load(lang);
    };
  };
}

export const preLoad = {
  provide: APP_INITIALIZER,
  multi: true,
  useFactory: preloadUser,
  deps: [UserService, TranslocoService]
};

Explanation:

  • The preloadUser function ensures that the user's language settings are fetched from the server, and the corresponding translation file is loaded before the application starts.

  • This guarantees that the app doesn’t boot up until the necessary language file is ready, preventing content flickering or layout shifts caused by missing translations.

By using this approach, you ensure a smooth user experience from the moment the app loads.

PreviousRecipiesNextUsing Xliff

Last updated 4 months ago

Was this helpful?

🍳