private bool LoadMessagesIntoCache(string langtag) { lock (Sync) { // It is possible for multiple threads to race to this method. The first to // enter the above lock will insert the messages into the cache. // If we lost the race...no need to duplicate the work of the winning thread. if (System.Web.HttpRuntime.Cache[GetCacheKey(langtag)] != null) { return(true); } Translation t = _translationRepository.GetTranslation(langtag); // Cache messages. // · In order to facilitate dynamic refreshing of translations during runtime // (without restarting the server instance) we first establish something upon // which the cache entry will be dependent: that is a 'cache dependency' object // which essentially triggers an event if/when the entry is to be considered // as 'dirty' and whihc is listened to by the cache. // In our case, we want this 'dirty' event to be triggered if/when the // translation's source PO file is updated. // NB: it is possible for GetCacheDependencyForSingleLanguage to return null in the // case of the default language where it is added to AppLanguages yet there // doesn't actually exist an underlying PO file. This is perfectly valid when // LocalizedApplication.MessageKeyIsValueInDefaultLanguage == true (default setting). // In this case the cache entry is associated with the null CacheDependency instance // which means the cache entry is effectively permanent for this server instance. System.Web.Caching.CacheDependency cd = _translationRepository.GetCacheDependencyForSingleLanguage(langtag); // · Insert translation into cache associating it with any CacheDependency. System.Web.HttpRuntime.Cache.Insert(GetCacheKey(langtag), t.Items, cd); } return(true); }
private bool LoadMessagesIntoCache(string langtag) { lock (Sync) { // It is possible for multiple threads to race to this method. The first to // enter the above lock will insert the messages into the cache. // If we lost the race...no need to duplicate the work of the winning thread. if (System.Web.HttpRuntime.Cache[GetCacheKey(langtag)] != null) { return(true); } Translation t = _translationRepository.GetTranslation(langtag); // Cache messages. // NB: if the file changes we want to be able to rebuild the index without recompiling. // NB: it is possible for GetCacheDependencyForSingleLanguage to return null in the // case of the default language where it is added to AppLanguages yet doesn't actually exist. // See MessageKeyIsValueInDefaultLanguage. var cd = _translationRepository.GetCacheDependencyForSingleLanguage(langtag); if (cd != null) { System.Web.HttpRuntime.Cache.Insert(GetCacheKey(langtag), t.Items, cd); } } return(true); }
public void MergeAllTranslation(IDictionary <string, TemplateItem> items) { foreach (var language in _repository.GetAvailableLanguages()) { MergeTranslation(items, _repository.GetTranslation(language.LanguageShortTag)); } }
public void MergeAllTranslation(IDictionary <string, TemplateItem> items) { foreach (var language in _repository.GetAvailableLanguages()) { var filesNames = items.GroupBy(x => x.Value.FileName).Select(x => x.Key).ToList(); MergeTranslation(items, _repository.GetTranslation(language.LanguageShortTag, filesNames, false)); } }
public string GetTranslation(string text) { var shakespeareTranslation = _translationRepository.GetTranslation(text); if (shakespeareTranslation == null) { throw new ApiException(System.Net.HttpStatusCode.NotFound, "No translation available"); } if (shakespeareTranslation.success.total != 1) { throw new ApiException(System.Net.HttpStatusCode.NotFound, "No translation available"); } return(shakespeareTranslation.contents.translated); }
public async Task <IActionResult> GetTranslation([FromBody] SearchRequest searchRequest) { var translation_fro_db = _translationRepository.GetTranslation(searchRequest.TextRequest); if (translation_fro_db != null) { Translation translationFromDatabase = new Translation() { EnglishText = translation_fro_db.EnglishText }; return(Ok(translationFromDatabase)); } var client = new RestClient("https://simple-elegant-translation-service.p.rapidapi.com/translate"); var request = new RestRequest(Method.POST); request.AddHeader("x-rapidapi-host", "simple-elegant-translation-service.p.rapidapi.com"); request.AddHeader("x-rapidapi-key", "5807bfc0f7mshdb2779da1949d0cp12623fjsnf96ac77d14d4"); request.AddHeader("content-type", "application/json"); request.AddHeader("accept", "application/json"); request.AddParameter("application/json", "{ \"text\":" + "\"" + searchRequest.TextRequest + "\"}", ParameterType.RequestBody); IRestResponse response = client.Execute(request); if (response.StatusCode == HttpStatusCode.OK) { var english_text = response.Content.ToString(); english_text = english_text.Remove(0, 85).ToString(); do { english_text = english_text.Remove(0, 1); }while (english_text[1] != '3'); english_text = english_text.Remove(0, 20).ToString(); english_text = english_text.Remove(english_text.Length - 4, 4).ToString(); var source_language = response.Content.ToString(); source_language = source_language.Remove(0, 35).ToString(); do { source_language = source_language.Remove(source_language.Length - 1, 1); }while (source_language[source_language.Length - 1] != '2'); source_language = source_language.Remove(source_language.Length - 5, 5); var destination_language = response.Content.ToString(); destination_language = destination_language.Remove(0, 75).ToString(); do { destination_language = destination_language.Remove(destination_language.Length - 1, 1); }while (destination_language[destination_language.Length - 1] != '3'); destination_language = destination_language.Remove(destination_language.Length - 4, 4); TranslationResult tr = new TranslationResult() { FirstLanguageText = searchRequest.TextRequest, EnglishText = english_text, From = source_language, To = destination_language, Time = DateTime.Now }; _translationRepository.Add(tr); await _translationRepository.SaveAsync(); Translation translationToReturn = new Translation() { EnglishText = tr.EnglishText }; SaveToXml(); return(Ok(translationToReturn)); } else { return(BadRequest("Server je preopterecen pokusajte malo kasnije")); } }