Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

L10n: Display changed language without page reload #25

Closed
StegSchreck opened this issue May 20, 2019 · 3 comments
Closed

L10n: Display changed language without page reload #25

StegSchreck opened this issue May 20, 2019 · 3 comments

Comments

@StegSchreck
Copy link
Owner

No description provided.

@tobyharding
Copy link

tobyharding commented Apr 11, 2020

localisation service
new variable:
languageChanged = new Subject();

with...
import { Subject } from 'rxjs';

new method:

  public changeLanguage():void {
    this.languageChanged.next();
  }

l10n component

    if (current_language !== language) {
      this.localizationService.changeLanguage();
    }

each constructor now subscribes...

  constructor(
    private localizationService: LocalizationService,
    private cvItemService: CvItemService,
  ) {
    this.localizationService.languageChanged.subscribe(() => { this.getLocalization() });
  }

header component has to nuke the existing navLinks

  private populateNavLinks(): void {
    this.navLinks.splice(0, this.navLinks.length);
(etc)

...and constructor has a slightly different method
this.localizationService.languageChanged.subscribe(() => { this.getAllItems(); }

getAllItems replaces ngOnInit, which calls it:

  private getAllItems(): void {
    this.getLocalization();
    this.getFeatureToggles();
    this.getItems();
    this.populateNavLinks();
  }

  ngOnInit() {
    this.getAllItems();
  }

@tobyharding
Copy link

Realised that it's more efficient to not call getLocalization() each time, but just pass the data back.

this.localizationService.changeLanguage(language);

then

  public changeLanguage(language):void {
    this.languageChanged.next(L10N[language]);
  }

with, for each ...component.ts

  constructor(
    private localizationService: LocalizationService,
    private cvItemService: CvItemService
  ) {
    this.localizationService.languageChanged.subscribe((data) => { this.l10n = data });
  }

header.component.ts can be simplified, with the original ngOnInit(), with constructor

  constructor(
    private localizationService: LocalizationService,
    private featureToggleService: FeatureToggleService,
    private cvItemService: CvItemService,
    ) {
      this.localizationService.languageChanged.subscribe(
          (data) => {
            this.l10n = data;
            this.populateNavLinks();
          });
    }

@StegSchreck
Copy link
Owner Author

@tobyharding Thank you for your guide how to achieve the language change without reload, that was very helpful!

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants