简体   繁体   English

在Angular中本地化路线

[英]Localizing routes in Angular

I am trying to create an Angular app in multiple languages but I have come across and issue with the routing. 我正在尝试创建多种语言的Angular应用程序,但是遇到了路由问题。 I found a workaround to make the necessary routes valid for 2 languages : 我找到了一种解决方法,以使必要的路由对2种语言均有效:

var app = angular.module("app", ["localization", "ngResource", "ngRoute"]).
config(function ($routeProvider, $locationProvider) {
    $routeProvider.
        when('/en-US/Gameplan/Admin/Fixtures/List', { controller: FixtureListController, templateUrl: '/Content/Templates/Fixtures.html' }).
        when('/da-DK/Gameplan/Admin/Fixtures/List', { controller: FixtureListController, templateUrl: '/Content/Templates/Fixtures.html' }).
        when('/en-US/Gameplan/Admin/Fixtures/Add', { controller: FixtureAddController, templateUrl: '/Content/Templates/FixtureAddEdit.html' }).
        when('/da-DK/Gameplan/Admin/Fixtures/Add', { controller: FixtureAddController, templateUrl: '/Content/Templates/FixtureAddEdit.html' }).
        when('/en-US/Gameplan/Admin/Fixtures/Edit/:fixtureId', { controller: FixtureEditController, templateUrl: '/Content/Templates/FixtureAddEdit.html' }).
        when('/da-DK/Gameplan/Admin/Fixtures/Edit/:fixtureId', { controller: FixtureEditController, templateUrl: '/Content/Templates/FixtureAddEdit.html' }).
        otherwise({ redirectTo: '/en-US/Gameplan/Admin/Fixtures/List' });
    $locationProvider.html5Mode(true); //will use html5 mode rather than hashbang where available
});

However, I still have an issue with links, currently my links look like this : 但是,我仍然对链接有疑问,目前我的链接如下所示:

<a href="/en-US/Gameplan/Admin/Fixtures/Add"><i class="glyphicon glyphicon-plus"></i></a>
<a href="/en-US/Gameplan/Admin/Fixtures/Edit/{{fixture.Id}}"><i class="glyphicon glyphicon-edit"></i></a>

I don't want to hard code the URL and I have a client side object that returns the locale (in this case either en-US or da-DK), but I have been unable to dynamically set the href values. 我不想对URL进行硬编码,并且有一个客户端对象返回语言环境(在这种情况下为en-US或da-DK),但是我无法动态设置href值。 Is there any way to do this in Angular, or a different approach altogether regarding localizing routes? 有没有办法在Angular中做到这一点,或者在本地化路线方面采用完全不同的方法?

This isn't AngularJS specific, but why not set a cookie as soon as you know what language the user wants for the UI? 这不是AngularJS特有的,但是为什么不知道用户想要的UI语言就立即设置cookie? Then both the client and server have ready access to it on every page and you don't have to shuttle it around in the URL. 这样,客户端和服务器都可以在每个页面上访问它,而不必在URL中穿梭它。

This might be too late, but in case you are using Angular 2x there is a library localize-router . 这可能为时已晚,但是如果您使用的是Angular 2x,则有一个库localize-router You can find it here . 你可以在这里找到它。

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

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