简体   繁体   English

点击 2 次语言后,语言切换工作

[英]Language switch works after 2 clicks on a language

I have a problem with the language switch.我的语言切换有问题。 I have 7 languages on my website.我的网站上有 7 种语言。

在此处输入图片说明

If I click on FR , the page is translated correctly.如果我点击FR ,页面将被正确翻译。 If I click on NL also, the page is translated correctly.如果我也单击NL ,则页面翻译正确。

However, If I click on EN , nothing is happening... I have to click 2 times to display the page in English .但是,如果我点击EN ,什么也没有发生......我必须点击 2 次才能以英文显示页面 I don't understand why?我不明白为什么?

Then...然后...

If I click on DE , the page is translated correctly.如果我点击DE ,页面被正确翻译。

But, If I click on ES , nothing is happening... I have the same probleme in English.但是,如果我点击ES ,什么也没有发生......我在英语中有同样的问题。 I have to click 2 times to display the page in Spanish.我必须单击 2 次才能以西班牙语显示页面。

In IT et PT , I don't have any problems.ITPT ,我没有任何问题。

Here is an idea of the file -> auth-layout.component.html这是文件的想法 - > auth-layout.component.html

<div class="languageSelect">
   <a href="#" *ngFor="let l of supportedLangs; let i = index" (click)="switchLanguage(l); false; ">
       {{ l | uppercase}}  <!-- FR | NL | EN | DE | ES | IT | PT -->
   </a>
</div>

In the file -> auth-layout.component.ts I have this in JS, I don't see where is the error?在文件 -> auth-layout.component.ts我在 JS 中有这个,我看不到错误在哪里?

if you have an idea, I am interested, because I am stuck.如果你有一个想法,我很感兴趣,因为我被卡住了。

@Component({
  selector: 'app-auth-layout',
  templateUrl: './auth-layout.component.html',
  styleUrls: ['./auth-layout.component.scss']
})
export class AuthLayoutComponent implements OnInit {
  
  supportedLangs;

  constructor(
    public _router: Router, 
    private translate: TranslateService,
    private LS: LocalStoreService,
    public _location: Location
  ) { }

  ngOnInit() {
    this.supportedLangs = ['fr', 'nl', 'en', 'de', 'es', 'it', 'pt'];
  }


  switchLanguage(lang){
    if(lang == 'uk'){
      lang = 'en';
    }
    this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
        this.LS.setItem('LX_Current_Language', event.lang);
        console.log("LELOG OK X : " + this.LS.getItem('LX_Current_Language'));
    });
    console.log("LELOG OK A : " + this.LS.getItem('LX_Current_Language'));
    this.translate.use(lang);
    this.refresh();
  }

  refresh(): void {
    this._router.navigateByUrl("/refresh", { skipLocationChange: true }).then(() => {
        this._router.navigate([decodeURI(this._location.path())]);
    });
  }

}

Thank you for your time.感谢您的时间。

If translate.onLangChange is a Subject that's updated when you call translate.use(lang) you may need to subscribe to only the first emitted value, otherwise the subscription function might continue being triggered for the languages you clicked before.如果translate.onLangChange是在您调用translate.use(lang)时更新的主题,您可能只需要订阅第一个发出的值,否则订阅功能可能会继续为您之前单击的语言触发。 Have you tried :你有没有尝试过 :

this.translate.onLangChange.pipe(take(1)).subscribe(...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM