所以我没有任何答案,我会用我目前发现的内容进行回复...虽然我已经提交了 suggestion in Breeze user voice here
要进行翻译,我必须在我的 EMFactory 中启动微风时首先翻译 Breeze Validators,我发现的方式是这样的:
function emFactory($cookies, breeze, fileService) {
var lang = $cookies.lang || "en";
// load the validator templates translation mapping (external files: validators.{lang}.json)
var translations = loadValidatorsTranslation();
breeze.Validator.messageTemplates = translations[lang];
// Identify the endpoint for the remote data service
var serviceRoot = window.location.protocol + '//' + window.location.host + '/';
var serviceName = serviceRoot + 'breeze/BreezeApi';
var factory = {
newManager: function () { return new breeze.EntityManager(serviceName); },
serviceName: serviceName,
language: lang
};
return factory;
}
function loadValidatorsTranslation() {
return {
en: {
// ...
required: "'%displayName%' is required",
string: "'%displayName%' must be a string",
stringLength: "'%displayName%' must be a string with between %minLength% and %maxLength% characters",
url: "The %displayName% '%value%' is not a valid url"
},
fr: {
// ...
required: "'%displayName%' est requis",
string: "'%displayName%' doit être une chaîne de caractère",
stringLength: "'%displayName%' doit être une chaîne de caractère entre %minLength% et %maxLength% caractères",
url: "%displayName% '%value%' n'est pas un URL valide"
}
};
}
然后我创建了一个TranslationService 来处理我的 Breeze 上下文的 displayNames 实体:
appDemo.factory('translationService', ['$q', '$timeout', translationService]);
function translationService($q, $timeout) {
// declare the displayNames translations of entities
var displayMapping = {
fr: {
City: {
Name: "Nom de Ville"
},
Speaker: {
Bio: "Bio",
Image: "Image",
Name: "Nom du Conférencier"
}
},
en: {
City: {
Name: "City Name"
},
Speaker: {
Bio: "Bio",
Image: "Image",
Name: "Speaker Name"
}
}
};
// reveal the public functions & return the service
return {
loadTranslationDisplayNames: loadTranslationDisplayNames
};
// -- public functions
// --------------------
function loadTranslationDisplayNames(manager, lang, entityTypes) {
for (var i = 0, ln = entityTypes.length; i < ln; i++) {
// get the specific context Entity
var custType = manager.metadataStore.getEntityType(entityTypes[i]);
var entityProperties = displayMapping[lang][entityTypes[i]];
// loop through all properties of this Entity and update their DisplayName
for (var name in entityProperties) {
custType.getProperty(name).displayName = entityProperties[name];
}
}
}
}
最后在我的 DataService 中,我用这个调用了我的 TranslationService
function dataService($rootScope, $q, breeze, entityManagerFactory, translationService) {
var service = this;
// reveal the public functions we want, any other functions will remain private
service.getSpeakers = getSpeakers;
var manager = entityManagerFactory.newManager();
var lang = entityManagerFactory.language;
return service;
// -- public/private functions declaration
function getSpeakers() {
var query = new breeze.EntityQuery.from("Speakers");
// load the translation of Breeze DisplayNames entities
translationService.loadTranslationDisplayNames(manager, lang, ["City", "Speaker"]);
startProcessingData();
var promise =
manager.executeQuery(query)
.catch(queryFailed)
.finally(processingDataComplete);
return promise;
}
}
...所以这项工作,我只需随时用我的新翻译更新我的 TranslationService。如果其他人有更好/更清洁的解决方案,我会很高兴看到它......