Оле*_*кий 3 javascript angularjs angular-translate
我是编程和角度的新手.我需要使用angular-translate及其'useUrlLoader,因为我的翻译存储在数据库中.$translateProvider.useUrlLoader('foo/bar.json');
$translateProvider.preferredLanguage('en');
虽然使用staticFilesLoader似乎对我来说足够简单,因为我只需要两个带有翻译数据的独立json文件,我无法获得useUrlLoader所期望的内容.据我所知,它期望json包括多种语言翻译(例如英语和德语).无法在任何地方找到此类文件的示例.
StaticFilesLoader期望您将不同语言的所有翻译存储在服务器上的单独文件中.它提出这样的请求:
/your/server/i18n/locale-en.json
/your/server/i18n/locale-de.json
/your/server/i18n/locale-fr.json
Run Code Online (Sandbox Code Playgroud)
where /your/server/i18n/locale-和.json是在配置期间传递的前缀和后缀(分别).
UrlLoader期望您有一个"聪明"的端点而不是一堆文件.它提出这样的请求:
/your/server/i18n/locale-endpoint?lang=en
/your/server/i18n/locale-endpoint?lang=de
/your/server/i18n/locale-endpoint?lang=fr
Run Code Online (Sandbox Code Playgroud)
where /your/server/i18n/locale-endpoint和lang是你在配置期间传递的url和queryParameter(分别).该URL是必需的,但queryParameter可以省略(默认为"郎").
您可以像这样设置UrlLoader:
$translateProvider.useUrlLoader('/path/to/your/endpoint', {
queryParameter : 'localeKey'
});
Run Code Online (Sandbox Code Playgroud)
或者像这样:
$translateProvider.useLoader('$translateUrlLoader', {
url : '/path/to/your/endpoint',
queryParameter : 'localeKey'
});
Run Code Online (Sandbox Code Playgroud)
两个加载器都希望将转换加载为JSON对象.它可能看起来像这样:
{
"translationId" : "Translation for this ID",
"anotherTranslationId": "Another translation for another id"
}
Run Code Online (Sandbox Code Playgroud)
您可以在官方指南中找到有关不同装载机的更多信息.
可以在angular-translate存储库中找到UrlLoader的源代码.
希望这可以帮助.