简体   繁体   English

如何读取AngularJs语言环境文件?

[英]How to read AngularJs locale file?

I am using Angular bootstrap datepicker popup and I want to set the date format of the textbox according to the locale, so my idea is reading the property "shortdate" from locale file and set it in the format property. 我正在使用Angular bootstrap datepicker弹出窗口,并且我想根据语言环境设置文本框的日期格式,所以我的想法是从语言环境文件中读取属性“ shortdate”并将其设置在format属性中。

So my question is, how can I read the locale file? 所以我的问题是, 如何读取语言环境文件? This file is present, localization is actually working and you can see the file in the network tab of the browser's developer tools 该文件存在,本地化实际上正在起作用,您可以在浏览器开发人员工具的“网络”标签中看到该文件

在此处输入图片说明

I once had the same requirement and end up using the 'tmh.dynamicLocale' module ( see here . 我曾经有过相同的要求,最终使用了'tmh.dynamicLocale'模块( 请参阅此处)

Using this module, you'll first have to configure a locale provider. 使用此模块,您首先必须配置区域设置提供程序。 In an angular config function, inject 'tmhDynamicLocaleProvider' and call the localeLocationPattern( ) function to define the pattern to use to access your locale files. 在角度配置函数中,注入“ tmhDynamicLocaleProvider”并调用localeLocationPattern()函数以定义用于访问区域设置文件的模式。

For instance: 例如:

 tmhDynamicLocaleProvider.localeLocationPattern('vendor/angular/locale/angular-locale_{{locale}}-{{locale}}.js');

Then, whenever the locale is supposed to changed, you simply have to inject 'tmhDynamicLocale' in your service/controller/... and call the set( ) function on in, passing as argument, the locale you want to switch to. 然后,只要应该更改语言环境,您只需在服务/控制器/ ...中插入“ tmhDynamicLocale”,然后在其中调用set()函数,并以参数形式传递要切换到的语言环境。

Hope that'll help you. 希望对您有帮助。

I realized there is no need to read anything, as the locale is already loaded by Angular you have all data available in the $locale service. 我意识到没有必要读取任何内容,因为Angular已经加载了语言环境,因此您可以在$ locale服务中获得所有可用数据。

this.format = $locale.DATETIME_FORMATS.shortDate;

In my case this format variable is being assigned to uib-datepicker-popup like this: 在我的情况下,此格式变量被分配给uib-datepicker-popup,如下所示:

<input type="text" uib-datepicker-popup="{{vm.format}}"
ng-model="vm.model" datepicker-options="vm.dateOptions"
ng-required="vm.isRequired" ng-disabled="vm.isDisabled"/>

Silly question, I hope it may help to some other clueless in future :) 愚蠢的问题,希望以后对其他无知的人有所帮助:)

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

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