Example #1
0
        /// <summary>
        /// Downloads a dictionary file for a given language
        /// </summary>
        /// <param name="language">en-GB, it-IT, ko-KO</param>
        /// <returns></returns>
        public static bool DownloadDictionary(string language, string basePath = null)
        {
            if (basePath == null)
            {
                basePath = App.InitialStartDirectory;
            }

            try
            {
                var diItem = DictionaryDownloads.FirstOrDefault(di => language.Equals(di.Code, StringComparison.InvariantCultureIgnoreCase));
                if (diItem != null)
                {
                    var url     = string.Format(DictionaryDownloadUrl, diItem.Code);
                    var dicFile = Path.Combine(basePath, "Editor", language + ".dic");
                    var web     = new WebClient();
                    web.DownloadFile(new Uri(url), dicFile);
                    var affFile = dicFile.Replace(".dic", ".aff");
                    web.DownloadFile(new Uri(url), affFile);

                    return(File.Exists(dicFile) && File.Exists(affFile));
                }
            }
            catch { }

            return(false);
        }
        public static bool AskForLicenseAcceptance(string language)
        {
            var form = new BrowserMessageBox()
            {
                Owner = mmApp.Model.Window
            };

            mmApp.Model.Window.ShowStatusProgress($"Downloading dictionary license for {language}");

            //download license
            var url = $"https://raw.githubusercontent.com/wooorm/dictionaries/main/dictionaries/{language}/license";

            var dd = DictionaryDownloads.FirstOrDefault(dx => dx.Code == language);

            if (!string.IsNullOrEmpty(dd?.CustomDownloadUrlForLicense))
            {
                url = dd.CustomDownloadUrlForLicense;
            }

            string md;

            try
            {
                using (var wc = new WebClient())
                {
                    wc.Encoding = Encoding.UTF8;
                    md          = wc.DownloadString(url);
                }
            }
            catch
            {
                mmApp.Model.Window.ShowStatusError($"Failed to download dictionary for '{language} from {url}");
                return(false);
            }

            mmApp.Model.Window.ShowStatusSuccess($"Downloaded license for {language}");

            if (string.IsNullOrEmpty(md))
            {
                return(false);
            }

            form.ShowMarkdown(md);
            form.Icon = mmApp.Model.Window.Icon;
            form.ButtonOkText.Text = "Accept";
            form.SetMessage("Please accept the license for the dictionary:");
            form.ShowDialog();

            return(form.ButtonResult == form.ButtonOk);
        }
        /// <summary>
        /// Downloads a dictionary file for a given language
        /// </summary>
        /// <param name="language">en-GB, it-IT, ko-KO</param>
        /// <param name="basePath">The path that dictionaries are downloaded to</param>
        /// <returns></returns>
        public static bool DownloadDictionary(string language, string basePath = null)
        {
            if (basePath == null)
            {
                basePath = ExternalDictionaryFolder;
            }

            try
            {
                var diItem = DictionaryDownloads.FirstOrDefault(di =>
                                                                language.Equals(di.Code, StringComparison.InvariantCultureIgnoreCase));
                if (diItem != null)
                {
                    string url = null;
                    if (!string.IsNullOrEmpty(diItem.CustomDownloadUrlForDic))
                    {
                        url = diItem.CustomDownloadUrlForDic;
                    }
                    else
                    {
                        url = string.Format(DictionaryDownloadUrl, diItem.Code);
                    }

                    if (!Directory.Exists(basePath))
                    {
                        Directory.CreateDirectory(basePath);
                    }

                    var dicFile = Path.Combine(basePath, language + ".dic");
                    using (var web = new WebClient())
                    {
                        web.DownloadFile(new Uri(url), dicFile);

                        var affFile = dicFile.Replace(".dic", ".aff");
                        url = url.Replace(".dic", ".aff");
                        web.DownloadFile(new Uri(url), affFile);

                        return(File.Exists(dicFile) && File.Exists(affFile));
                    }
                }
            }
            catch (Exception ex)
            {
                mmApp.Log("Failed to download dictionary", ex, false, LogLevels.Warning);
            }

            return(false);
        }