public static LanguageDictionary New(string fileName) { var dictionary = new LanguageDictionary(1); dictionary.Filename = fileName; return(dictionary); }
private static LanguageDictionary FromText(string[] lines) { var dictionary = new LanguageDictionary(lines.Length); foreach (var line in lines) { int index = line.IndexOf(':'); dictionary.TryAdd(line.Substring(0, index), line.Substring(index + 1)); } return(dictionary); }
public async Task <string> Translate(string word, string sourceLang, string targetLang) { if (string.IsNullOrWhiteSpace(word)) { return(string.Empty); } word = word.ToLower(); sourceLang = sourceLang.ToLower(); targetLang = targetLang.ToLower(); var dictionaryName = sourceLang + targetLang; if (!_dictionaries.TryGetValue(dictionaryName, out var dictionary)) { dictionary = LanguageDictionary.New(_dictionariesPath + dictionaryName); _dictionaries.Add(dictionaryName, dictionary); } if (dictionary.TryGetValue(word, out var translation)) { return(translation); } //translation = await _googleTranslatorService.Translate(word, sourceLang, targetLang); if (string.IsNullOrEmpty(translation)) { return(string.Empty); } dictionary.Add(word, translation.ToLower()); dictionary.Save(); _dictionaries[dictionaryName] = dictionary; return(translation); }
public OfflineTranslatorService(GoogleTranslatorService googleTranslatorService) { _googleTranslatorService = googleTranslatorService; if (!Directory.Exists(_dictionariesPath)) { _dictionaries = new Dictionary <string, LanguageDictionary>(); return; } var fileNames = Directory.GetFiles(_dictionariesPath); _dictionaries = new Dictionary <string, LanguageDictionary>(fileNames.Length); foreach (var file in fileNames) { var dictionary = LanguageDictionary.FromFile(file); if (dictionary.IsSuccess) { _dictionaries.TryAdd(dictionary.Value.Language.ToLower(), dictionary.Value); } } }