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

Using Xliff

If your project uses XLIFF for translation management, Transloco makes it easy to handle and load these files. In this section, we'll walk you through setting up a custom loader to work with XLIFF translation files, and configure it in your Angular application.

1

Install the XLIFF Package

pnpm add xliff
yarn add xliff
npm install xliff

2

Implement a Custom XLIFF Loader

Replace the default HTTP loader in Transloco with a custom loader that fetches and processes XLIFF translation files.

import {
  TRANSLOCO_LOADER,
  Translation,
  TranslocoLoader,
  TRANSLOCO_CONFIG,
  translocoConfig,
  TranslocoModule,
} from '@jsverse/transloco';
import { Injectable, isDevMode, NgModule } from '@angular/core';
import { from } from 'rxjs';
import { switchMap, map } from 'rxjs/operators';
import xliff from 'xliff/xliff2js';

// Utility function to convert XLIFF to the Transloco format
function toTranslationFormat(json: any): Translation {
  const obj = json.resources.transloco;
  return Object.keys(obj).reduce((acc, key) => {
    acc[key] = obj[key].target;
    return acc;
  }, {} as Translation);
}

@Injectable({ providedIn: 'root' })
export class TranslocoXliffHttpLoader implements TranslocoLoader {
  constructor(private http: HttpClient) {}

  getTranslation(lang: string) {
    return this.http
      .get<Translation>(`/assets/i18n/${lang}.xlf`, { responseType: 'text' })
      .pipe(
        switchMap((translation) => from(xliff(translation))),
        map(toTranslationFormat)
      );
  }
}

bootstrapApplication(AppComponent, {
  providers: [
    provideTransloco({
      ...,
      loader: TranslocoXliffHttpLoader
    }),
  ],
});
import { HttpClient } from '@angular/common/http';
import {
  TRANSLOCO_LOADER,
  Translation,
  TranslocoLoader,
  TRANSLOCO_CONFIG,
  translocoConfig,
  TranslocoModule,
} from '@jsverse/transloco';
import { Injectable, isDevMode, NgModule } from '@angular/core';
import { from } from 'rxjs';
import { switchMap, map } from 'rxjs/operators';
import xliff from 'xliff/xliff2js';

// Utility function to convert XLIFF to the Transloco format
function toTranslationFormat(json: any): Translation {
  const obj = json.resources.transloco;
  return Object.keys(obj).reduce((acc, key) => {
    acc[key] = obj[key].target;
    return acc;
  }, {} as Translation);
}

@Injectable({ providedIn: 'root' })
export class TranslocoXliffHttpLoader implements TranslocoLoader {
  constructor(private http: HttpClient) {}

  getTranslation(lang: string) {
    return this.http
      .get<Translation>(`/assets/i18n/${lang}.xlf`, { responseType: 'text' })
      .pipe(
        switchMap((translation) => from(xliff(translation))),
        map(toTranslationFormat)
      );
  }
}

@NgModule({
  exports: [TranslocoModule],
  providers: [
    provideTransloco({
      ...,
      loader: TranslocoXliffHttpLoader
    }),
  ],
})
export class TranslocoRootModule {}
3

Sample XLIFF Translation File (v1.2)

Create your XLIFF translation files in the assets/i18n folder. Here's an example of a v1.2 XLIFF file:

<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
  <file source-language="en" datatype="plaintext" original="transloco">
    <body>
      <trans-unit id="title">
        <source>Hello Transloco!</source>
        <target>Bonjour Transloco!</target>
      </trans-unit>
    </body>
  </file>
</xliff>
PreviousPrefetch User LanguageNextGenerate Locale Files using Google Translate

Last updated 5 months ago

Was this helpful?

๐Ÿณ