从角度翻译文件存储访问存储的区域设置

accessing stored locale from angular-translate FileStorage

本文关键字:存储 访问 区域 设置 文件 翻译      更新时间:2023-09-26

我在LocalStorage中使用了角度翻译,它工作得很好。 区域设置缓存或存储在本地磁盘上。 关闭浏览器并重新启动时,将保留所选区域设置。

我的问题是,如何访问其中一个控制器中磁盘上的缓存存储的区域设置? 为了更好地解释,以下是不起作用的工作流程:

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。 不可运行的代码。 只是我正在使用的代码的一个例子

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

在兔子整个 API 文档和角度翻译 * 源文件中大约 2 小时后,我发现我可以将$translateLocalStorage传递给我的控制器,然后调用$translateLocalStorage.get()它将返回缓存的 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();
 }
});