question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to share a common library with its translation to all other libraries and apps with NX?

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[x] Documentation issue or request
[x] Support request
[ ] Other... Please describe:

Dear Ngneat team,

We are currently migrating from ngx-translate to transloco because we use NX for handling several Angular apps and libs. In addition, we would like to separate the translations by module. The following example demonstrates it quite well, how we can use transloco with different scopes for each app and lib:

Example with NX: https://github.com/NetanelBasal/transloco-with-nx-libs

Unfortunately, I am stuck with the use case when you add a third library. For example, let’s name it “common”. This library contains all shared “dumb” components and is imported in practically all other libraries as well as by the (lazy) modules in the app itself. The common library has its own translations.

Update: 15.11.2020 How the dependency could look like: image

What I am missing in the example above is exactly this use case. I am assuming, that this is a common use case and should be possible with Transloco but I cannot get it to work. Maybe I am missing anything? Please see more details in the minimal reproduction section.

Thank you very much for your help.

Current behavior

The translations are not loaded: image

In the logs, I got a missing error:

Missing translation for ‘common.validations.required’

Expected behavior

The translations are loaded and rendered.

Minimal reproduction of the problem with instructions

Clone the project above and then generate a new library as follows:

nx g library common

Under libs/common/src/lib I created a new folder i18n and added two translation files en.json and es.json.

image

Example en.json (same for es.json):

{
  "validations.required": "This field is required"
}

The CommonsModule will look as follows:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TranslocoModule, TRANSLOCO_SCOPE } from '@ngneat/transloco';
import { scopeLoader } from '../../../../scoped-translations';

@NgModule({
  imports: [CommonModule, TranslocoModule],
  exports: [TranslocoModule],
  providers: [
    {
      provide: TRANSLOCO_SCOPE,
      useValue: {
        scope: 'common',
        loader: scopeLoader((lang, root) => import(`./${root}/${lang}.json`))
      }
    }
  ]
})
export class CommonsModule {}

Now, I will import this module in any lib and app.

Example Library import in TranslocoLibBModule:

import { TRANSLOCO_SCOPE } from '@ngneat/transloco';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { scopeLoader } from '../../../../scoped-translations';
import { LocationBComponent } from './location-b.component';
import { CommonsModule } from '@transloco-with-libs/common';

@NgModule({
  imports: [
    CommonModule, 

    // newly added
    CommonsModule],
  providers: [
    {
      provide: TRANSLOCO_SCOPE,
      useValue: {
        scope: 'compB',
        loader: scopeLoader((lang, root) => import(`./${root}/${lang}.json`))
      }
    }
  ],
  declarations: [LocationBComponent],
  exports: [LocationBComponent]
})
export class TranslocoLibBModule {}

And the component:

@Component({
  selector: 'transloco-with-libs-b',
  template: `
    <ng-container *transloco="let t">
      <p>where am I? {{ t('compB.gpsb') }}</p>
    </ng-container>

    <!-- newly added -->
    <p>Inside lib-b: {{ 'common.validations.required' | transloco }}</p>
    <ng-container *transloco="let t">
      <p>Inside lib-b: {{ t('common.validations.required') }}</p>
    </ng-container>
  `
})
export class LocationBComponent implements OnInit {
  constructor() {}

  ngOnInit() {}
}

The same thing in the app:

app.module.ts

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    HttpClientModule,
    // newly added
    CommonsModule,
    TranslocoLibBModule,
    RouterModule.forRoot(routes)
  ],
  providers: [
    {
      provide: TRANSLOCO_CONFIG,
      useValue: {
        reRenderOnLangChange: true,
        availableLangs,
        defaultLang: 'en',
        prodMode: environment.production
      } as TranslocoConfig
    },
    translocoLoader
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

app.component.html

<section>
  <h1>This should tell me where I am</h1>
  <button (click)="change('en')">English</button>
  <button (click)="change('es')">Spanish</button>

  <ng-container *transloco="let t">
    <p>Root: {{ t('gps') }}</p>
  </ng-container>

  <!-- newly added-->
  <p>Inside app: {{ 'common.validations.required' | transloco }}</p>
  <ng-container *transloco="let t">
    <p>Inside app: {{ t('common.validations.required') }}</p>
  </ng-container>

  <div style="display: flex; flex-direction: column">
    <transloco-with-libs-b></transloco-with-libs-b>
  </div>

  <router-outlet></router-outlet>
</section>

Run and verify it: ng serve

Environment


Angular version: 8.2.9 (minimal reproduction from the cloned repo)

Its still an issue in our project with Angular version 10.0.0 and with Transloco version ^2.19.1.

Browser:
- [x ] Chrome (desktop) version 86.0.4240.183
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: v12.18.0  
- Platform:  Mac 

Others:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
JohanHeyvaertcommented, May 3, 2021

Would it be possible to make it more easy to achieve this (i.e. having translations in the module’s (or component’s) folder itself)? It’s a bit cumbersome having to configure the provider in each component (or module). The code is always the same so it’s not DRY. Ideally this should be configured just once, on the root level and then all modules would know where to get their scoped translations.

2reactions
strikeflarecommented, Dec 10, 2020

Sorry for being late. I uploaded the reproduction here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Environment sharing across libraries with Angular Injection ...
Create the applications and libraries. Let's first generate two applications that will share common libraries. $ nx g @nrwl/angular:application ...
Read more >
Grouping Libraries - Nx
This organization has two apps, booking and check-in . In the Nx workspace, libraries related to booking are grouped under a libs/booking folder,...
Read more >
Lazy translation assets with Angular, Transloco and Nx ...
Transloco as the translation library for handling your translations. JSON files to contain your translation key-value pairs (en.json). Tutorial.
Read more >
Taming Code Organization with Module Boundaries in Nx
A shared or core library should not be able to depend on a feature library ; A feature library can depend on another...
Read more >
Getting started with standalone components - Angular
You can use standalone components with existing NgModule -based libraries or dependencies in your template. Standalone components can take full advantage of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found