Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jonathan Ben Avraham 43 posts 216 karma points
    Apr 29, 2016 @ 09:49
    Jonathan Ben Avraham
    0

    Using LocalizationService to update LanguageText table

    I'm developing a package for Umbraco but I am having an issue with updating LanguageText values from a CSV. Could anyone give me any support or explain where I'm going wrong?

    Here is my Code:

    public void SaveLTNew()
                {
                    var ls = ApplicationContext.Current.Services.LocalizationService; 
                    var myContext = Request.TryGetHttpContext();
    
                    List<string> values = new List<string>();
                    List<string> languages = new List<string>();
                    List<string> keys = new List<string>();
    
                        if (myContext.Success)
                        {
                            HttpPostedFileBase myFileNew = myContext.Result.Request.Files["file"];
                            List<DictionaryTranslation> _hello = new List<DictionaryTranslation>();
                            List<DictionaryTranslation> _submit = new List<DictionaryTranslation>();
                            List<DictionaryTranslation> _form = new List<DictionaryTranslation>();
                            List<DictionaryTranslation> _bootstrap = new List<DictionaryTranslation>();
                            List<DictionaryTranslation> _world = new List<DictionaryTranslation>();
                            List<DictionaryTranslation> _heaven = new List<DictionaryTranslation>();
                            List<DictionaryTranslation> _hell = new List<DictionaryTranslation>();
                            List<DictionaryTranslation> _this = new List<DictionaryTranslation>();
                            List<DictionaryTranslation> _sublime1 = new List<DictionaryTranslation>();
                            if (myFileNew == null)
                            {
                                throw new HttpException("Invalid File");
                            }
                            else
                            {
                                StreamReader csvreader = new StreamReader(myFileNew.InputStream);
    
                                while (!csvreader.EndOfStream)
                                {
                                    var line = csvreader.ReadLine();
                                    if (line != "Value")
                                        values.Add(line);
                                    if (line != "languageId")
                                        languages.Add(line);
                                    if (line != "Key")
                                        keys.Add(line);
                                }
                            }
                             UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
                                var remove = new Sql("DELETE FROM cmsLanguageText");
                                int rem = db.Execute(remove);
    
                            foreach (string value in values)
                            {
                                List<DictionaryTranslation> langIds = new List<DictionaryTranslation>();
                                    var language = ls.GetLanguageByIsoCode("he-IL");
                                    var lang1 = ls.GetLanguageByIsoCode("ru");
                                    var lang2 = ls.GetLanguageByIsoCode("en-US");
    
                            if (keys != null)
                            {
                                DictionaryTranslation key = new DictionaryTranslation(language, value);
                                DictionaryTranslation key1 = new DictionaryTranslation(lang1, value);
                                DictionaryTranslation key2  = new DictionaryTranslation(lang2, value);
                                break;
                            }
                            else
                            {
                                throw new HttpException("invalid values");
                            }
                        }
                            IDictionaryItem hello = ls.DictionaryItemExists("hello_button") ? ls.GetDictionaryItemByKey("hello_button") : new DictionaryItem("hello_button");
                            IDictionaryItem submit = ls.DictionaryItemExists("submit_button") ? ls.GetDictionaryItemByKey("submit_button") : new DictionaryItem("submit_button");
                            IDictionaryItem form = ls.DictionaryItemExists("form_button") ? ls.GetDictionaryItemByKey("form_button") : new DictionaryItem("form_button");
                            IDictionaryItem btstrp = ls.DictionaryItemExists("bootstrap_button") ? ls.GetDictionaryItemByKey("bootstrap_button") : new DictionaryItem("bootstrap_button");
                            IDictionaryItem world = ls.DictionaryItemExists("world_button") ? ls.GetDictionaryItemByKey("world_button") : new DictionaryItem("world_button");
                            IDictionaryItem heaven = ls.DictionaryItemExists("heaven_button") ? ls.GetDictionaryItemByKey("heaven_button") : new DictionaryItem("heaven_button");
                            IDictionaryItem hell = ls.DictionaryItemExists("hell_button") ? ls.GetDictionaryItemByKey("hell_button") : new DictionaryItem("hell_button");
                            IDictionaryItem This = ls.DictionaryItemExists("this_button") ? ls.GetDictionaryItemByKey("this_button") : new DictionaryItem("this_button");
                            IDictionaryItem sublime = ls.DictionaryItemExists("sublime_button") ? ls.GetDictionaryItemByKey("sublime_button") : new DictionaryItem("sublime_button");
                            hello.Translations = _hello;
                            submit.Translations = _submit;
                            form.Translations = _form;
                            btstrp.Translations = _bootstrap;
                            world.Translations = _world;
                            heaven.Translations = _heaven;
                            hell.Translations = _hell;
                            This.Translations = _this;
                            sublime.Translations = _sublime1;
    
                            //Save the values
                            ls.Save(hello);
                            ls.Save(submit);
                            ls.Save(form);
                            ls.Save(btstrp);
                            ls.Save(world);
                            ls.Save(heaven);
                            ls.Save(hell);
                            ls.Save(This);
                            ls.Save(sublime);
                }
            }
    

    This is compiling and running but not adding anything into the database table, could anyone help me get the values from the one column in the CSV into the database using LocalisationService?

    Thanks in advance!

Please Sign in or register to post replies

Write your reply to:

Draft