gpt4 book ai didi

javascript - 不同的 URL 取决于语言 - Angular 2

转载 作者:行者123 更新时间:2023-11-30 20:49:49 24 4
gpt4 key购买 nike

我有一个 translation.service.ts 可以用不同的语言显示我的网站。这样可行。现在,我想根据显示的语言向 URL 添加类似 /es//en/ 的内容。

我的服务是:

import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Injectable()
export class TranslationService {
constructor(
//Used for the internationalization
private translate: TranslateService,
) { }

switchLanguage(language: string) {
//If spanish
if (
language == "es" || language == "es-ar" || language == "es-bo" || language == "es-cl" ||
language == "es-co" || language == "es-cr" || language == "es-do" || language == "es-ec" ||
language == "es-sv" || language == "es-gt" || language == "es-hn" || language == "es-mx" ||
language == "es-ni" || language == "es-pa" || language == "es-py" || language == "es-pe" ||
language == "es-er" || language == "es-es" || language == "es-uy" || language == "es-ve") this.translate.use("es");
//If german
else if (language == "de" || language == "de-at" || language == "de-de" || language == "de-li" || language == "de-lu" || language == "de-ch" ) this.translate.use("de");
//if not, english
else this.translate.use("en");
}
}

更新:13/02/2018

我试过:

app-routing.module.ts

var language = navigator.language;
const routes: Routes = [
{ path: '', redirectTo: '/dashboard/'+language, pathMatch: 'full'},
{ path: 'dashboard/'+language, component: DashboardComponent },
}

但是我得到一个错误:

ERROR Error: Uncaught (in promise): Error: Cannot redirect to '/dashboard/:language'. Cannot find ':language'. Error: Cannot redirect to '/dashboard/:language'. Cannot find ':language'.

当我尝试时:

const routes: Routes = [
{ path: '', redirectTo: '/dasboard/:language', pathMatch: 'full'},
{ path: 'dashboard:language', redirectTo: '/dasboard/:language', component: DashboardComponent }
]

我得到:

Error: Invalid configuration of route 'dashboard:language': redirectTo and component cannot be used together

有什么建议吗?

最佳答案

设置路由参数并导航到url

this.router.navigate(['../', { language: language}]

你应该有路由配置包含language作为如下所示的参数

 { path: '.../:language', redirectTo: '/..../:language' },

关于javascript - 不同的 URL 取决于语言 - Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48300949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com