コード例 #1
0
        private void UseGoogleLangDetect(string text)
        {
            var twoLetterLanguageId = Utilities.AutoDetectGoogleLanguage(text);
            var cultureInfo         = CultureInfo.GetCultureInfo(twoLetterLanguageId);

            if (cultureInfo != null)
            {
                labelLanguage.Visible        = true;
                comboBoxDictionaries.Visible = true;
                comboBoxDictionaries.Items.Add("Auto");
                foreach (Word.Language language in _spellCheckLanguages)
                {
                    comboBoxDictionaries.Items.Add(language.NameLocal);
                    var langId = (Word.WdLanguageID)cultureInfo.TextInfo.LCID;
                    if (language.ID == langId)
                    {
                        comboBoxDictionaries.SelectedIndex = comboBoxDictionaries.Items.Count - 1;
                        _currentLanguage = _spellCheckLanguages[comboBoxDictionaries.Items.Count - 1];
                        _wordApp.ActiveDocument.Content.LanguageID       = langId;
                        _wordApp.ActiveDocument.Content.LanguageDetected = true;
                    }
                }

                if (comboBoxDictionaries.SelectedIndex < 0)
                {
                    comboBoxDictionaries.SelectedIndex = 0;
                }
            }
        }
コード例 #2
0
        private bool StartSpellCheckParagraph(string text)
        {
            //var cdic =  _wordApp.Application.CustomDictionaries.Add(GetCustomDicFileName());
            //_wordApp.Application.CustomDictionaries.ActiveCustomDictionary = cdic;
            Word.Range range;
            range      = _wordApp.ActiveDocument.Range();
            range.Text = string.Empty;

            //insert text box data after the content of range of active document

            var lines = text.Replace(Environment.NewLine, "\n").Split('\n');

            if (lines.Length == 2 && lines[0].Length > 1)
            {
                string last = lines[0].Substring(lines[0].Length - 1);
                if ("ABCDEFGHIJKLMNOPQRSTUVWZYXÆØÃÅÄÖÉÈÁÂÀÇÊÍÓÔÕÚŁАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯĞİŞÜÙÁÌÑ".ToLower().Contains(last.ToLower()))
                {
                    text = text.Replace(Environment.NewLine, " ");
                }
            }
            text = Utilities.RemoveHtmlTags(text);
            text = text.Replace("  ", " ");
            range.InsertAfter(text);

            if (comboBoxDictionaries.Visible)
            {
                if (comboBoxDictionaries.SelectedIndex > 0)
                {
                    _currentLanguage = _spellCheckLanguages[comboBoxDictionaries.SelectedIndex - 1];
                    _wordApp.ActiveDocument.Content.LanguageID = _currentLanguage.ID;
                }
                else
                {
                    try
                    {
                        _wordApp.ActiveDocument.Content.DetectLanguage();
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
            _currentSpellCollection      = range.SpellingErrors;
            _currentSpellCollectionIndex = -1;
            if (_currentSpellCollection.Count == 0)
            {
                return(false);
            }

            ShowNextSpellingError();
            return(true);
        }
コード例 #3
0
        private void AutoDetectLanguage()
        {
            string cleanText = string.Empty;

            try
            {
                int min = 2;
                int max = 4;
                int i   = 0;
                var sb  = new StringBuilder();
                foreach (Paragraph p in _subtitle.Paragraphs)
                {
                    i++;
                    if (i >= min)
                    {
                        if (!string.IsNullOrWhiteSpace(Utilities.RemoveHtmlTags(p.Text, true)))
                        {
                            sb.AppendLine(p.Text);
                        }
                        else
                        {
                            i--;
                        }
                    }
                    if (i > max)
                    {
                        break;
                    }
                }
                cleanText = Utilities.RemoveHtmlTags(sb.ToString().Trim(), true);
                Word.Range range;
                range                  = _wordApp.ActiveDocument.Range();
                range.Text             = cleanText;
                range.LanguageDetected = false;
                range.DetectLanguage();
                if (range.LanguageDetected)
                {
                    labelLanguage.Visible        = true;
                    comboBoxDictionaries.Visible = true;
                    comboBoxDictionaries.Items.Add("Auto");
                    foreach (Word.Language language in _spellCheckLanguages)
                    {
                        comboBoxDictionaries.Items.Add(language.NameLocal);
                        if (language.ID == range.LanguageID)
                        {
                            comboBoxDictionaries.SelectedIndex = comboBoxDictionaries.Items.Count - 1;
                            _currentLanguage = _spellCheckLanguages[comboBoxDictionaries.Items.Count - 1];
                        }
                    }
                    if (comboBoxDictionaries.SelectedIndex < 0)
                    {
                        comboBoxDictionaries.SelectedIndex = 0;
                    }
                }
                else
                {
                    UseGoogleLangDetect(cleanText);
                }
            }
            catch
            {
                UseGoogleLangDetect(cleanText);
            }
        }