DEV Community

Cover image for Creating Search Engine-friendly Internationalized Apps with Angular Universal and Transloco 🌐
Shahar Kazaz
Shahar Kazaz

Posted on

2 1

Creating Search Engine-friendly Internationalized Apps with Angular Universal and Transloco 🌐

Originally published at shahar.kazaz

In this article, I will show you how easily we can add internalization (i18n) support to Angular SSR using the next generation Angular internalization library Transloco. Let's get started.

First, let's create a brand new Angular CLI project:

npx @angular/cli new transloco-ssr
Enter fullscreen mode Exit fullscreen mode

Now, add SSR support by using @nguniversal/express-engine schematics:

ng add @nguniversal/express-engine --clientProject <PROJECT-NAME>
Enter fullscreen mode Exit fullscreen mode

Add Transloco to your project by using schematics:

ng add @ngneat/transloco
Enter fullscreen mode Exit fullscreen mode

Choose your supported languages, and answer Yes for the SSR question. Now you can see that it created the translation files, the loader, and added everyhing it needs to the AppModule.

When using Angular SSR, we need to change our loader base path to be absolute instead of relative, for it to work. This is already done for you.

The only thing you need to do is modify the new baseUrl property in each one of the environment files based on your application:

export const environment = {
  production: false,
  baseUrl: 'http://localhost:4200' <====
};

// environment.prod.ts
export const environment = {
  production: true,
  baseUrl: 'http://localhost:4000' <====
};
Enter fullscreen mode Exit fullscreen mode

Now let's add a new key to our translation files:

{
  "title": "Angular SSR with Transloco english"
}
Enter fullscreen mode Exit fullscreen mode

And let's use it in our template:

<ng-template transloco let-t>
  {{ t.title }}
</ng-template>
Enter fullscreen mode Exit fullscreen mode

Let's build our application for production using the following commands:

npm run build:ssr
npm run serve:ssr
Enter fullscreen mode Exit fullscreen mode

Open your browser and navigate to http://localhost:4000 and Viola! We did it!

Let's see the source HTML file:
image

We have got a working application with SSR and internalization (i18n) support in 5 minutes.

Bonus

We can use the accept-language header to set the preferred user language automatically. The Accept-Language request HTTP header advertises which languages the client can understand, and which locale variant is preferred.

import { Component, Inject, Optional } from '@angular/core';
import { Request } from 'express';
import { REQUEST } from '@nguniversal/express-engine/tokens';
import { TranslocoService } from '@ngneat/transloco';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(@Optional() @Inject(REQUEST) private request: Request, private translocoService: TranslocoService) {
    if (request.headers) {
      translocoService.setActiveLang(normalize(request.headers['accept-language']));
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

The accept-language headers return the following string:

  'accept-language': 'en-US,en;q=0.9,he;q=0.8,ru;q=0.7,fr;q=0.6,pt;q=0.5',
Enter fullscreen mode Exit fullscreen mode

You need to extract the language and set it as active.


Here is some more great content about Transloco:

πŸš€ Introducing Transloco: Angular Internationalization Done Right

πŸŽ‰ Good Things Come to Those Who Wait: What’s new in Transloco

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here β†’

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay