- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Angular Cordova 和 Ionic 开发移动应用程序。我想在我的应用程序中使用多个翻译文件。喜欢:
我正在使用这段代码:
$translateProvider.useStaticFilesLoader({
prefix: 'main/i18n/local-',
suffix: '.json'
});
我的问题是如何加载多个文件?我搜索了很多,但一无所获。
提前致谢
最佳答案
您还可以找到 angular-translate-loader-pluggable包有用,部分加载器仍然取代加载器提供程序,而不是允许任何给定模块使用多个加载器。
然后可插拔加载器成为一种合并的翻译提供程序。它将合并所有已配置加载器的结果。
我使用可插入加载器允许每个模块的语言文件,以及数据库驱动的源。
The idea is that over time, most static string translations will be published into files, however some the user can configure directly at runtime, hence the dbStringProvider. In this example you can see examples of using the factory or provider method for defining the translate function
var module = angular.module('app', [
'pascalprecht.translate',
'LocalStorageModule',
'angular-translate-loader-pluggable'
]);
module.provider('$dbStringProvider', $dbStringProvider);
function $dbStringProvider() {
function isStringValid(str) {
return angular.isString(str) && str !== '';
}
this.$get = ['$rootScope', '$injector', '$q', '$http',
function ($rootScope, $injector, $q, $http) {
var service = function (options) {
// options.key is the requested language identifier
if (!isStringValid(options.key)) {
throw new TypeError('Unable to load data, a key is not a non-empty string.');
}
var errorHandler = options.loadFailureHandler;
if (errorHandler !== undefined) {
if (!angular.isString(errorHandler)) {
throw new Error('Unable to load data, a loadFailureHandler is not a string.');
} else {
errorHandler = $injector.get(errorHandler);
}
}
var deferred = $q.defer(), translations;
translations = {
'TEXT': 'TEST Provider',
'TEXT1': 'This is a test (1: Provider)'
};
deferred.resolve(translations);
return deferred.promise;
};
return service;
}];
}
module.factory('dbStringFactory', ['$q', '$http',
function ($q, $http) {
var loader = function (options) {
var deferred = $q.defer();
var translations = {};
if (options.key == "en") {
// Normally we call the database to get the strings
// The following demonstrates how multiple concurrent providers will provide translations
translations = {
'TEXT': 'TEST Factory',
'TEXT2': 'This is a test (2: factory)'
};
}
deferred.resolve(translations);
return deferred.promise;
};
return loader;
}]);
module.config(translateConfig);
/* @ngInject */
function translateConfig($translateProvider, $translatePartialLoaderProvider, triSettingsProvider, translatePluggableLoaderProvider) {
/**
* $translateProvider only allows a single loader at a time
* Use pluggable loader to add and remove loaders whenever you
* feel the need to, meaning some modules can use entirely different
* translation loaders without having to destroy any previous configuration
*/
$translateProvider.useLoader('translatePluggableLoader');
/**
* each module loads its own translation file - making it easier to create translations
* also translations are not loaded when they aren't needed
* each module will have a i18n folder that will contain its translations
* So still use partial loader as a loader for the pluggable loader.
*/
translatePluggableLoaderProvider.useLoader('$translatePartialLoader', {
urlTemplate: '{part}/i18n/{lang}.json'
});
$translatePartialLoaderProvider.addPart('app');
/**
* Pluggable allows us to register multiple providers
* at the same time!
*/
translatePluggableLoaderProvider.useLoader('dbStringFactory');
translatePluggableLoaderProvider.useLoader('$dbStringProvider');
// make sure all values used in translate are sanitized for security
$translateProvider.useSanitizeValueStrategy('sanitize');
// cache translation files to save load on server
$translateProvider.useLoaderCache(true);
/**
* Map the locale variants of en_US and en_UK to 'en'
* CS: Just an example of how to do it, remove this if you want to use full region codes (locales)
*/
$translateProvider
.registerAvailableLanguageKeys(angularTranslateLanguageKeys, {
'en_US': 'en',
'en_UK': 'en'
})
.use('en');
// store the users language preference in a cookie
$translateProvider.useLocalStorage();
}
现在在应用程序的一个页面上,使用以下代码测试字符串是否已加载
NOTE: The LAST loader added will overwrite previous entries in the translation, in the config we loaded the partial provider, then the
dbStringFactory
then thedbStringProvider
So the provider should overwrite any keys provided by the factory implementation:English is a tag in a partial file loaded from
$translatePartialLoaderProvider.addPart('app');
<h3 translate>English</h3>
<h1 translate>TEXT</h1>
<h2 translate>TEXT1</h2>
<h2 translate>TEXT2</h2>
产生:
翻译成法语:
Notice Provider still shows the static string results, because if is NOT changing the result based on the passed on
options.key
where as the factory implementation does filter on the passed in language but is only configured to show the english translation.
如果未找到匹配项,angular-translate 返回原始 key 的正常行为仍然存在。
我最初遇到了与 OP 相同的问题,我希望你觉得这有用。
关于javascript - Angular 翻译多个 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35272213/
翻译自官方wiki: https://github.com/facebook/rocksdb/wiki/Write-Stalls 转载请注明出处: https://www.cnblogs.c
译者注:在微服务架构设计,构建API和服务间通信技术选型时,对 REST 和 gRPC 的理解和应用还存在知识盲区,近期看到国外的这篇文章: A detailed comparison of
rocksdb调试指引 翻译自官方wiki: https://github.com/facebook/rocksdb/wiki/RocksDB-Tuning-Guide 转载请注明出处: h
传统的ASP.NET Web Forms是一个非常好的主意,但现实需求非常复杂。随着时间的推移,现实世界的项目暴露出Web Forms的一些不足之处: “沉重的”视图状态:现实中在http请求之间
翻译自:Top 10 questions of Java Strings 简单地说,”==”测试两个字符串的引用是否相同,equals()测试两个字符串的值是否相同。除非你希望检
你好,今天我要和大家分享一些东西,举例来说这个在JavaScript中用的很多。我要讲讲回调(callbacks)。你知道什么时候用,怎么用这个吗?你真的理解了它在java环境中的用法了吗?当我也问
Java多线程面试问题 1. 进程和线程之间有什么不同? 一个进程是一个独立(self contained)的运行环境,它可以被看作一个程序或者一个应用。而线程是在进程中执行的一个
原文: [A Dive into .Net 8 Native AOT and Efficient Web Development] 作者: [sharmila subbiah] 引言 随着 .NE
这是Fiddle 是否可以在 angular-translate 中检查其他语言的键值是否可用,然后它可以从其他语言中提取该键值? 就像在示例中,我有英语和西类牙语。并且一个键值(例如“CONFIRM
我希望能够使用 $this->__('String to translate')在外部脚本中。我该怎么做呢? Magento 版本 1.5.1.0 . 最佳答案 我认为设置语言环境的正确方法是: Ma
我有一个开关小部件,它使用自定义数据属性值来标记自己。 .switch.switch-text .switch-label::before { right: 1px; color: #c2cf
是否有人遇到过这样的情况:用 Java 编写并由(例如)法国程序员编写的现有代码库必须转换为英语程序员可以理解的代码?这里的问题是变量/方法/类名称、注释等都将采用该特定语言。 现在有可用的自动化解决
维基百科和其他一些网站将解释器描述为将代码从某种高级语言翻译成某种低级语言的翻译器。然而,有很多解释,包括在 stackoverflow 中,它说解释器直接执行作为输入的指令,而无需事先转换。那么解释
我想将基本动画应用于自定义单元格中的某些元素,例如标签、图像:特别是,我想让这些动画在我触摸单元格内部时也启动。我是初学者,我只学会了使用 animateWithDuration 和 transiti
这个问题在这里已经有了答案: NSDateFormatter and current language in iOS11 (5 个回答) 已关闭 3 年前。 当使用这样的 DateComponentF
我想在点击 var about 时移动 div.willshow。但我单击那个 btn,只有它获得类 active。然后我再次单击那个 btn 它失去了类。如果我再点击一次,每项任务都无法正常工作。
我想要一个按钮在悬停时向下移动几个像素,但它又回来了。当您还在上面徘徊时,它不应该留在原处吗? Email Me .btn {background: #2ecc71; padding: .5em 1e
在我的应用程序中,我想添加功能将页面翻译为用户在浏览器中设置的所有语言,如果没有可用的语言,则翻译为默认英语...问题是浏览器与语言支持不一致。我找到了一个解决方法,我对一些返回用户语言的 Web 服
我的应用程序有一个 Help.htm 文件,用谷歌翻译翻译得相当好。我想将菜单项标记为“请勿翻译”,但我发现并尝试过的 HTML 标签都不起作用。对于以下内容,我使用了谷歌翻译网站 - 它翻译了我没想
我有以下代码: span { width:200px; height:100px; background-color:red; border:1px solid black; } span.c2 {
我是一名优秀的程序员,十分优秀!