Exemple #1
0
        public void SaveTranslations()
        {
            OrderTranslations();
            //var fileData = JsonConvert.SerializeObject(_translationFile[_cultureCode], Formatting.Indented);

            lock (_lockObject)
            {
                //using (var resFile = File.Create(_resourceFilePath))
                //{
                //    var data = Encoding.UTF8.GetBytes(fileData);
                //    resFile.Write(data, 0, data.Length);
                //    resFile.Flush();
                //}

                foreach (var item in SupportedCultures.Cultures)
                {
                    if (item.TwoLetterISOLanguageName == "en")
                    {
                        continue;
                    }

                    var file = TranslationFile.GetTranslationFilePath(item.TwoLetterISOLanguageName);
                    NccFileHelper.WriteObject(file.FullName, _translationFile[item.TwoLetterISOLanguageName]);
                }
            }
        }
        public bool InactivateTheme(string themeId)
        {
            try
            {
                var infoFileLocation = Path.Combine(GlobalContext.ContentRootPath, NccInfo.ThemeFolder, themeId, ThemeInfoFileName);
                if (File.Exists(infoFileLocation))
                {
                    var theme = NccFileHelper.LoadObject <Theme>(infoFileLocation);

                    if (theme.IsActive == true)
                    {
                        theme.IsActive = false;
                        NccFileHelper.WriteObject <Theme>(infoFileLocation, theme);
                    }

                    return(true);
                }
                else
                {
                    RegisterErrorMessage("Theme config file Theme.json not found");
                }
            }
            catch (Exception ex)
            {
                return(false);
            }
            return(false);
        }
Exemple #3
0
        public static void LoadTranslationFile(string cultureCode)
        {
            if (cultureCode == "en")
            {
                return;
            }

            FileInfo translationFile = TranslationFile.GetTranslationFilePath(cultureCode);

            if (File.Exists(translationFile.FullName) == false)
            {
                if (Directory.Exists(translationFile.DirectoryName) == false)
                {
                    Directory.CreateDirectory(translationFile.DirectoryName);
                }
                File.Create(translationFile.FullName).Dispose();
            }

            var translationFileData = NccFileHelper.ReadAllText(translationFile.FullName);

            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;
                    NccFileHelper.WriteAllText(translationFile.FullName, translationFileData);
                }
            }

            _translationFile[cultureCode] = JsonConvert.DeserializeObject <TranslationFileData>(translationFileData);
        }
 public bool ActivateDefaultTheme()
 {
     try
     {
         var infoFileLocation = Path.Combine(GlobalContext.ContentRootPath, NccInfo.ThemeFolder, Constants.DefaultThemeId, ThemeInfoFileName);
         if (File.Exists(infoFileLocation))
         {
             var theme = NccFileHelper.LoadObject <Theme>(infoFileLocation);
             theme.ThemeId  = Constants.DefaultThemeId;
             theme.IsActive = true;
             NccFileHelper.WriteObject <Theme>(infoFileLocation, theme);
             var defaultTheme = GlobalContext.Themes.Where(x => x.ThemeId == Constants.DefaultThemeId).FirstOrDefault();
             if (defaultTheme != null)
             {
                 defaultTheme.IsActive = true;
             }
             ThemeHelper.ActiveTheme = theme;
             return(true);
         }
         else
         {
             RegisterErrorMessage("Theme config file Theme.json not found");
         }
     }
     catch (Exception ex)
     {
         RegisterErrorMessage(ex.Message);
         throw ex;
     }
     return(false);
 }
Exemple #5
0
 public void Save()
 {
     if (File.Exists(FullPath))
     {
         NccFileHelper.WriteAllText(FullPath, Content);
     }
 }
 public static void LoadMessagesFromStorage()
 {
     try
     {
         var messageDbFilePath = GetMessageDbFilePath();
         if (File.Exists(messageDbFilePath))
         {
             var msgs = NccFileHelper.LoadObject <Dictionary <string, GlobalMessageEntry> >(messageDbFilePath);
             if (msgs != null)
             {
                 foreach (var item in msgs)
                 {
                     _messageCache.AddOrUpdate(item.Key, item.Value, (key, val) =>
                     {
                         return(item.Value);
                     });
                 }
             }
         }
     }
     catch (Exception ex)
     {
         //TODO: have to chose better approch.
         using (var originalFileStream = File.Open(GetMessageDbFilePath(), FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite))
         {
         }
     }
 }
        private static void WriteMessagesToStorage()
        {
            var task = new Task(() => {
                try
                {
                    var messageDbFilePath = GetMessageDbFilePath();

                    if (File.Exists(messageDbFilePath) == false)
                    {
                        using (File.Open(messageDbFilePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)) { }
                    }

                    if (_messageCache.Count > 0)
                    {
                        NccFileHelper.WriteObject(messageDbFilePath, _messageCache);
                    }
                    else
                    {
                        using (var originalFileStream = File.Open(messageDbFilePath, FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite)) { }
                    }
                }
                catch (Exception ex)
                {
                    //TODO: have to improve this.
                }
            });

            task.Start();
            task.Wait();
        }
Exemple #8
0
 public string Load()
 {
     if (File.Exists(FullPath))
     {
         var fi = new FileInfo(FullPath);
         FileName = fi.Name;
         Culture  = GetCultureFromFileName(FileName);
         Content  = NccFileHelper.ReadAllText(FullPath);
         Id       = Cryptography.GetHash(FullPath);
     }
     return(Content);
 }
Exemple #9
0
        public static List <TranslationFile> GetTranslationFiles()
        {
            var translationFileList = new List <TranslationFile>();
            var languageFolder      = Path.Combine(GlobalContext.GetResourceFolder(), "Language");

            if (Directory.Exists(languageFolder))
            {
                var files = Directory.GetFiles(languageFolder, "*.lang");
                foreach (var item in files)
                {
                    var fi = new FileInfo(item);
                    var tf = new TranslationFile();
                    tf.Content  = NccFileHelper.ReadAllText(item);
                    tf.FileName = fi.Name;
                    tf.FullPath = item;
                    tf.Group    = "Website";
                    tf.Culture  = GetCultureFromFileName(fi.Name);
                    tf.Id       = Cryptography.GetHash(item);
                    translationFileList.Add(tf);
                }
            }

            return(translationFileList);
        }
        public List <Theme> ScanThemeDirectory(string path)
        {
            var  themes           = new List <Theme>();
            var  directoryInfo    = new DirectoryInfo(path);
            bool isThemeActivated = false;

            foreach (var themeDir in directoryInfo.EnumerateDirectories())
            {
                try
                {
                    if (themeDir.Name == "ChildThemes")
                    {
                        continue;
                    }

                    var configFileLocation = Path.Combine(themeDir.FullName, ThemeInfoFileName);
                    if (File.Exists(configFileLocation))
                    {
                        var theme = NccFileHelper.LoadObject <Theme>(configFileLocation);

                        theme.ThemeId        = themeDir.Name;
                        theme.Folder         = themeDir.Name;
                        theme.ConfigFilePath = configFileLocation;

                        if (Directory.Exists(themeDir.FullName + "\\Bin\\Debug\\netcoreapp2.0"))
                        {
                            theme.ResourceFolder = themeDir.FullName + "\\Bin\\Debug\\netcoreapp2.0\\Resources";
                            theme.AssemblyPath   = themeDir.FullName + "\\Bin\\Debug\\netcoreapp2.0\\" + theme.ThemeId + ".dll";
                        }
                        else if (Directory.Exists(themeDir.FullName + "\\Bin\\Release\\netcoreapp2.0"))
                        {
                            theme.AssemblyPath   = themeDir.FullName + "\\Bin\\Release\\netcoreapp2.0\\" + theme.ThemeId + ".dll";
                            theme.ResourceFolder = themeDir.FullName + "\\Bin\\Release\\netcoreapp2.0\\Resources";
                        }

                        if (string.IsNullOrEmpty(theme.AssemblyPath) == false && File.Exists(theme.AssemblyPath))
                        {
                            var themeAssembly = Assembly.LoadFile(theme.AssemblyPath);
                            themes.Add(theme);
                            if (isThemeActivated == false && theme.IsActive)
                            {
                                isThemeActivated        = true;
                                ThemeHelper.ActiveTheme = theme;
                            }
                            else
                            {
                                if (theme.IsActive)
                                {
                                    theme.IsActive = false;
                                    NccFileHelper.WriteObject <Theme>(configFileLocation, theme);
                                }
                            }
                            GlobalContext.Themes.Add(theme);
                        }
                    }
                    else
                    {
                        //RegisterErrorMessage("Theme config file Theme.json not found");
                    }
                }
                catch (Exception ex)
                {
                    RegisterErrorMessage(ex.Message);
                    throw ex;
                }
            }

            if (ThemeHelper.ActiveTheme == null)
            {
                ActivateDefaultTheme();
            }

            return(themes);
        }