从 angular-translate FileStorage 访问存储的语言环境

accessing stored locale from angular-translate FileStorage

我正在使用 angular-translate 和 LocalStorage,它运行良好。区域设置缓存或存储在本地磁盘上。当浏览器关闭并重新启动时,选择的语言环境会保留。

我的问题是如何访问我的一个控制器磁盘上的 cachedstored 语言环境?为了更好地解释这里的工作流程工作:

1. Launch browser to localhost and can see the site with previously chosen locale
2. Attempt to access the current locale from either the
    2.1 $translate.proposedLanguage()
    2.2 tmhDynamicLocale
3. Both are undefined -- even though the correct language is shown
4. Select language choice once more
5. current locale from both the above is now correct 

这是模块和区域设置服务的 JSFiddle。不是 runnable 代码。只是我使用的代码示例

我正在尝试访问 currentLocale,但它是 未定义,直到用户(再次)做出选择。我知道它存储在某个地方,因为原始选择在浏览器退出后仍然存在。

在整个 API 文档和 angular-translate-* 源文件中大约 2 小时后,我发现我可以将 $translateLocalStorage 传递给我的控制器,然后调用 $translateLocalStorage.get() 这将 return 缓存的 localId。

该方法从 `$window.localStorage.getItem 中提取 localId。该代码是

get: function (name) {
    if(!langKey) {
      langKey = $window.localStorage.getItem(name);
    }

    return langKey;
  }

所以您需要做的就是:

angular.module('myModule')
  .controller('QueryController', function($scope, es, $translateLocalStorage) {
     $scope.show_results = function(results) {
        var currentLocale = $translateLocalStorgae.get();
 }
});