HomeSandboxShowcaseAppDocBlog
    • EnglishEnglish
      EN
    • русскийRussian
      RU
    • 日本語Japanese
      JA
    • françaisFrench
      FR
    • 한국어Korean
      KO
    • 中文Chinese
      ZH
    • españolSpanish
      ES
    • DeutschGerman
      DE
    • العربيةArabic
      AR
    • italianoItalian
      IT
    • British EnglishBritish English
      EN-GB
    • portuguêsPortuguese
      PT
    • हिन्दीHindi
      HI
    • TürkçeTurkish
      TR
    • polskiPolish
      PL
    • IndonesiaIndonesian
      ID
    • Tiếng ViệtVietnamese
      VI
    • українськаUkrainian
      UK
    /
    Filter docs by framework
    Alt+←
    Why Intlayer ?
    Get Started
    Concept
    • How Intlayer Works
    • Configuration
    • TestFillBuildWatchExtractLoginPushPullConfigurationListVersionEditorLiveDebugDoc ReviewDoc TranslateSDK
    • Visual Editor
    • CMS
    • CI/CD Integration
    • TranslationPluralEnumerationConditionGenderInsertionFileNestingMarkdownHTMLFunction Fetching
    • Per Locale File
    • Compiler
    • Auto Fill
    • Testing
    • Bundle Optimization
    Environment
    • Next.js 14 and App Router
      Next.js 15
      Next.js no locale path
      Next.js and Page Router
      Compiler
    • Tanstack Start Solid
    • Astro and React
      Astro and Svelte
      Astro and Vue
      Astro and Solid
      Astro and Preact
      Astro and Lit
      Astro and Vanilla JS
    • React Router v7
      React Router v7 (fs-routes)
      Compiler
    • Nuxt and Vue
    • Vite and Solid
    • SvelteKit
    • Vite and Preact
    • Vite and Vanilla JS
    • Vite and Lit
    • Angular 19 (Webpack)
      Analog
    • React CRA
    • React Native and Expo
    • Express.js
      NestJS
      Fastify
      Hono
      Adonis
    • Lynx and React
    Plugins
    • JSON
    • gettext (.po)
    VS Code Extension
    Agent
    • MCP Server
    • Agent skills
    Releases
    • v8
    • v7
    • v6
    Benchmark
    • Next.js
    • TanStack
    • Vue
    • Solid
    • Svelte
    Blog
    Ask a question
    1. Documentation
    2. Packages
    3. Intlayer
    4. GetLocalizedUrl
    Creation:2024-08-11Last update:2025-06-29
    Reference this doc to your favorite AI assistant
    ChatGPT
    Claude
    DeepSeek
    Google AI mode
    Gemini
    Perplexity
    Mistral
    Grok

    Ask your question and get a summary of the document by referencing this page and the AI provider of your choice

    This doc is out of date, the base version has been updated on 16 November 2025.

    Go to English doc

    Version History

    1. "Init history"
      v5.5.1029/06/2025

    The content of this page was translated using an AI.

    See the last version of the original content in English
    Edit this doc

    If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.

    GitHub link to the documentation
    Copy

    Copy doc Markdown to clipboard

    Documentation: getLocalizedUrl Function in intlayer

    Description

    The getLocalizedUrl function generates a localised URL by prefixing the given URL with the specified locale. It handles both absolute and relative URLs, ensuring that the correct locale prefix is applied based on the configuration.


    Parameters

    • url: string

      • Description: The original URL string to be prefixed with a locale.
      • Type: string
    • currentLocale: Locales

      • Description: The current locale for which the URL is being localised.
      • Type: Locales
    • locales: Locales[]

      • Description: Optional array of supported locales. By default, the configured locales in the project are provided.
      • Type: Locales[]
      • Default: Project Configuration
    • defaultLocale: Locales

      • Description: The default locale for the application. By default, the configured default locale in the project is provided.
      • Type: Locales
      • Default: Project Configuration
    • prefixDefault: boolean

      • Description: Whether to prefix the URL for the default locale. By default, the configured value in the project is provided.
      • Type: boolean
      • Default: Project Configuration

    Returns

    • Type: string
    • Description: The localised URL for the specified locale.

    Example Usage

    Relative URLs

    typescript
    Copy code

    Copy the code to the clipboard

    import { getLocalizedUrl, Locales } from "intlayer";
    
    getLocalizedUrl(
      "/about",
      Locales.FRENCH,
      [Locales.ENGLISH, Locales.FRENCH],
      Locales.ENGLISH,
      false
    );
    
    // Output: "/fr/about" for the French locale
    // Output: "/about" for the default (English) locale

    Absolute URLs

    typescript
    Copy code

    Copy the code to the clipboard

    getLocalizedUrl(  "https://example.com/about",  Locales.FRENCH, // Current Locale  [Locales.ENGLISH, Locales.FRENCH], // Supported Locales  Locales.ENGLISH, // Default Locale  false // Prefix Default Locale); // Output: "https://example.com/fr/about" for the FrenchgetLocalizedUrl(  "https://example.com/about",  Locales.ENGLISH, // Current Locale  [Locales.ENGLISH, Locales.FRENCH], // Supported Locales  Locales.ENGLISH, // Default Locale  false // Prefix Default Locale); // Output: "https://example.com/about" for the EnglishgetLocalizedUrl(  "https://example.com/about",  Locales.ENGLISH, // Current Locale  [Locales.ENGLISH, Locales.FRENCH], // Supported Locales  Locales.ENGLISH, // Default Locale  true // Prefix Default Locale); // Output: "https://example.com/en/about" for the English

    Unsupported Locale

    typescript
    Copy code

    Copy the code to the clipboard

    getLocalizedUrl(  "/about",  Locales.ITALIAN, // Current Locale  [Locales.ENGLISH, Locales.FRENCH], // Supported Locales  Locales.ENGLISH // Default Locale); // Output: "/about" (no prefix applied for unsupported locale)

    Edge Cases

    • No Locale Segment:

      • If the URL does not contain any locale segment, the function safely prefixes the appropriate locale.
    • Default Locale:

      • When prefixDefault is false, the function does not prefix the URL for the default locale.
    • Unsupported Locales:

      • For locales not listed in locales, the function does not apply any prefix.

    Usage in Applications

    In a multilingual application, configuring the internationalisation settings with locales and defaultLocale is critical for ensuring the correct language is displayed. Below is an example of how getLocalizedUrl can be used in an application setup:

    tsx
    Copy code

    Copy the code to the clipboard

    import { Locales, type IntlayerConfig } from "intlayer";
    
    // Configuration for supported locales and default locale
    export default {
      internationalization: {
        locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
        defaultLocale: Locales.ENGLISH,
      },
    } satisfies IntlayerConfig;
    
    export default config;

    The above configuration ensures that the application recognises ENGLISH, FRENCH, and SPANISH as supported languages and uses ENGLISH as the fallback language.

    Using this configuration, the getLocalizedUrl function can dynamically generate localised URLs based on the user's language preference:

    typescript
    Copy code

    Copy the code to the clipboard

    getLocalizedUrl("/about", Locales.FRENCH); // Output: "/fr/about"getLocalizedUrl("/about", Locales.SPANISH); // Output: "/es/about"getLocalizedUrl("/about", Locales.ENGLISH); // Output: "/about"

    By integrating getLocalizedUrl, developers can maintain consistent URL structures across multiple languages, enhancing both user experience and SEO.

    Why Intlayer ?
    Alt+→

    On this page

      Discussions are anonymous and regularly reviewed to address common issues. Feel free to share feature ideas, feedback on the documentation, or anything related to Intlayer, we use this input to shape our roadmap and improve the product.

      getLocalizedUrl(  "https://example.com/about",  Locales.FRENCH, // Current Locale  [Locales.ENGLISH, Locales.FRENCH], // Supported Locales  Locales.ENGLISH, // Default Locale  false // Prefix Default Locale); // Output: "https://example.com/fr/about" for the FrenchgetLocalizedUrl(  "https://example.com/about",  Locales.ENGLISH, // Current Locale  [Locales.ENGLISH, Locales.FRENCH], // Supported Locales  Locales.ENGLISH, // Default Locale  false // Prefix Default Locale); // Output: "https://example.com/about" for the EnglishgetLocalizedUrl(  "https://example.com/about",  Locales.ENGLISH, // Current Locale  [Locales.ENGLISH, Locales.FRENCH], // Supported Locales  Locales.ENGLISH, // Default Locale  true // Prefix Default Locale); // Output: "https://example.com/en/about" for the English
      getLocalizedUrl(  "/about",  Locales.ITALIAN, // Current Locale  [Locales.ENGLISH, Locales.FRENCH], // Supported Locales  Locales.ENGLISH // Default Locale); // Output: "/about" (no prefix applied for unsupported locale)
      getLocalizedUrl("/about", Locales.FRENCH); // Output: "/fr/about"getLocalizedUrl("/about", Locales.SPANISH); // Output: "/es/about"getLocalizedUrl("/about", Locales.ENGLISH); // Output: "/about"