如何在 angular 根中引入常量 (Angular-Translate)

How to introduce constants (Angular-Translate) in angular roots

我刚刚使用本指南在我的网站上实现了异步翻译;

https://scotch.io/tutorials/internationalization-of-angularjs-applications

他们使用此代码在页面加载时加载首选语言;

.constant('LOCALES', {
    'locales': {
      'en_EN': 'English',
      'fr_FR': 'Français'
    },
    'preferredLocale': 'fr_FR'
  })

我想知道是否可以在路由中执行此操作,这样我就可以修复链接以发送给人们。所以;

 .when('/en', {
        templateUrl: 'views/static/home.html',
        needConnection: false,
        .constant ('LOCALES', {
          'prefferedLocale': 'en_EN'
        }),
      })

谢谢

我建议你有一个 value 只要 constant,具有 prefferedLocale 的值。然后你在$rootScope注册监听$routeChangeStart,检查新路由,如果它以en/开头 您在您的值中使用 en_EN preferedLocale 并使用该值进行翻译。

此外,如果您确实需要常量,它可以是一个函数,returns 具有在 routeChange 中配置的 prefferedLocale 的对象。