public INccTranslator GetTranslator(string cultureCode) { _cultureCode = cultureCode; if (string.IsNullOrEmpty(cultureCode) || cultureCode.ToLower().Equals("en")) { return(this); } if (string.IsNullOrEmpty(_resourceFilePath)) { var path = Path.Combine(GlobalContext.ContentRootPath, "Resources", "Language"); if (GlobalContext.HostingEnvironment.EnvironmentName.Contains("Development")) { path = Path.Combine(GlobalContext.ContentRootPath, "bin", "Debug", "netcoreapp2.0", "Resources"); } _fileName = "NccTranslationFile." + cultureCode + ".lang"; _resourceFilePath = Path.Combine(path, _fileName); if (!File.Exists(_resourceFilePath)) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } File.Create(_resourceFilePath).Dispose(); } } if (_translationFile == null) { var translationFileData = File.ReadAllText(_resourceFilePath); if (string.IsNullOrWhiteSpace(translationFileData)) { var assembly = typeof(SharedResource).GetTypeInfo().Assembly; using (Stream resource = assembly.GetManifestResourceStream("NetCoreCMS.Framework.Resources.SharedResource.lang")) { var sr = new StreamReader(resource); translationFileData = sr.ReadToEndAsync().Result; File.WriteAllText(_resourceFilePath, translationFileData); } } _translationFile = JsonConvert.DeserializeObject <NccTranslationFile>(translationFileData); } return(this); }
public NccTranslator <T> GetTranslator(Type resourceType, string cultureCode) { _cultureCode = cultureCode; if (string.IsNullOrEmpty(cultureCode) || cultureCode.ToLower().Equals("en")) { return(this); } var assembly = resourceType.Assembly; var fileInfo = new FileInfo(assembly.Location); var path = Path.Combine(fileInfo.DirectoryName, "Resources"); _fileName = resourceType.Namespace + "." + resourceType.Name + "." + cultureCode + ".lang"; _resourceFilePath = Path.Combine(path, _fileName); if (!File.Exists(_resourceFilePath)) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } File.Create(_resourceFilePath).Dispose(); } var translationFileData = File.ReadAllText(_resourceFilePath); if (string.IsNullOrEmpty(translationFileData)) { translationFileData = JsonConvert.SerializeObject(new NccTranslationFile() { Translations = new Dictionary <string, string>() { { "FileName", _fileName }, { "Type", resourceType.Name } } }, Formatting.Indented); File.WriteAllText(_resourceFilePath, translationFileData); } _translationFile = JsonConvert.DeserializeObject <NccTranslationFile>(translationFileData); return(this); }