Пример #1
0
        void OnGuessCompleted(object sender, GuessCompletedEventArgs e)
        {
            if(e.Result != null)
                detectedText = e.Result.Phrase;

            if(e.Cancelled)
            {
            }
            else if(e.Result.Error != null)
            {
                detectedLanguage = Language.Unknown;
                detectedStatus = e.Result.Error.Message;
            }
            else if(e.Result.ResultNotFound || e.Result.Scores.Count == 0)
            {
                detectedLanguage = Language.Unknown;
                detectedStatus = "Failed";
            }
            else
            {
                detectedLanguage = e.Result.Language;
                detectedStatus = TranslateString("{0}, {1},  Score - {2}%, Confidence - {3}");
                detectedStatus = string.Format(detectedStatus,
                    LangPack.TranslateLanguage(e.Result.Language),
                    TranslateString(e.Result.IsReliable ? "Reliable" : "Not reliable"),
                    e.Result.Score,
                    TranslateString(e.Result.Confidence.ToString()));
            }
            UpdateDetectionStatus();

            if(!TranslateOptions.Instance.GuessingOptions.SwitchDirectionBasedOnLanguage)
                return;

            if(detectedLanguage != Language.Unknown && detectedText.Length >= TranslateOptions.Instance.GuessingOptions.MinimalTextLengthForSwitchByLanguage)
            {
                bool default_selected = currentProfile == TranslateOptions.Instance.DefaultProfile;
                UserTranslateProfile upf;

                //step 0. seek in history
                ProfilesHistory ph_to_delete = new ProfilesHistory();
                bool found = false;
                foreach(ProfilesHistoryData phd in TranslateOptions.Instance.ProfilesHistory)
                {
                    if(LanguageHelper.IntelligentCompare(phd.Language, detectedLanguage) ||
                        phd.DetectedLanguage == detectedLanguage)
                    {
                        TranslateProfile pf = TranslateOptions.Instance.Profiles.GetByName(phd.Name);
                        if(pf == null)
                        {	//here we should not to be, but
                            ph_to_delete.Add(phd);
                            continue;
                        }

                        upf = pf as UserTranslateProfile;
                        if(upf != null &&
                            (LanguageHelper.IntelligentCompare(upf.TranslationDirection.From, detectedLanguage) ||
                            (phd.DetectedLanguage == detectedLanguage && LanguageHelper.IntelligentCompare(upf.TranslationDirection.From, phd.Language))
                            ) &&
                            !upf.ShowLanguages
                            )
                        {
                            skipChangeInput = true;
                            ActivateProfile(upf);
                            tbFrom.Focus();
                            found = true;
                            break;
                        }

                        foreach(LanguagePair lp in pf.History)
                        {
                            if(LanguageHelper.IntelligentCompare(lp.From, detectedLanguage) ||
                                (phd.DetectedLanguage == detectedLanguage && LanguageHelper.IntelligentCompare(lp.From, phd.Language))
                            )
                            {
                                try
                                {
                                    skipChangeInput = true;
                                    ActivateProfile(pf);
                                    try
                                    {
                                        languageSelector.Selection = lp;
                                    }
                                    catch
                                    {
                                        UpdateCaption();
                                        skipChangeInput = false;
                                        continue;
                                    }
                                    UpdateCaption();
                                }
                                finally
                                {
                                    skipChangeInput = false;
                                }
                                tbFrom.Focus();
                                found = true;
                                break;
                            }
                        }

                        if(!found)
                            ph_to_delete.Add(phd);
                        else
                            break;
                    }
                }

                //remove unsupported profiles from history
                foreach(ProfilesHistoryData phd in ph_to_delete)
                    TranslateOptions.Instance.ProfilesHistory.DeleteProfile(phd.Name);

                if(found)
                    return;

                //step 1. seek in current if not default
                upf = currentProfile as UserTranslateProfile;
                if(upf != null)
                {
                    if(LanguageHelper.IntelligentCompare(upf.TranslationDirection.From, detectedLanguage)
                        && !upf.ShowLanguages
                    )
                    {
                        tbFrom.Focus();
                        return;
                    }

                    foreach(LanguagePair lp in languageSelector.History)
                    {
                        if(LanguageHelper.IntelligentCompare(lp.From, detectedLanguage))
                        {
                            try
                            {
                                skipChangeInput = true;
                                try
                                {
                                    languageSelector.Selection = lp;
                                }
                                catch
                                {
                                    UpdateCaption();
                                    skipChangeInput = false;
                                    continue;
                                }
                                UpdateCaption();
                            }
                            finally
                            {
                                skipChangeInput = false;
                            }
                            tbFrom.Focus();
                            return;
                        }
                    }
                }

                //step 2. Generate list of profiles. default - last
                TranslateProfilesCollection profiles = new TranslateProfilesCollection();
                foreach(TranslateProfile pf in TranslateOptions.Instance.Profiles)
                {
                    if(pf == TranslateOptions.Instance.DefaultProfile)
                            continue;

                    if(pf == currentProfile)
                            continue;

                    profiles.Add(pf);
                }
                profiles.Add(TranslateOptions.Instance.DefaultProfile);

                //step 2. seek in other not default profiles
                foreach(TranslateProfile pf in profiles)
                {
                    foreach(LanguagePair lp in pf.History)
                    {
                        if(LanguageHelper.IntelligentCompare(lp.From, detectedLanguage))
                        {
                            try
                            {
                                skipChangeInput = true;
                                ActivateProfile(pf);
                                try
                                {
                                    languageSelector.Selection = lp;
                                }
                                catch
                                {
                                    UpdateCaption();
                                    skipChangeInput = false;
                                    continue;
                                }
                                UpdateCaption();
                            }
                            finally
                            {
                                skipChangeInput = false;
                            }
                            tbFrom.Focus();
                            return;
                        }
                    }

                    upf = pf as UserTranslateProfile;
                    if(upf != null)
                    {
                        if(LanguageHelper.IntelligentCompare(upf.TranslationDirection.From, detectedLanguage))
                        {
                            skipChangeInput = true;
                            ActivateProfile(upf);
                            tbFrom.Focus();
                            return;
                        }
                    }

                    //switch auto
                    upf = currentProfile as UserTranslateProfile;
                    if((default_selected || (upf != null && upf.ShowLanguages)) &&
                    (currentProfile.SelectedLanguagePair.From != Language.Autodetect)
                    )
                    {
                        CultureInfo systemCulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
                        if(LanguageHelper.IsLanguageSupported(systemCulture, detectedLanguage))
                        {
                            Language targetLanguage = Language.English;
                            foreach(InputLanguage il in InputLanguage.InstalledInputLanguages)
                            {
                                if(!InputLanguageManager.IsLanguageSupported(il, detectedLanguage))
                                {
                                    for(int i = 0; i < (int)Language.Last; i++)
                                    {
                                        if(InputLanguageManager.IsLanguageSupported(il, (Language)i))
                                        {
                                            targetLanguage = (Language)i;
                                            goto EndDetect;
                                        }
                                    }
                                }
                            }

                            EndDetect:

                            foreach(LanguagePair lp in currentProfile.GetLanguagePairs())
                            {
                                if(lp.From == detectedLanguage &&
                                    lp.To == targetLanguage
                                )
                                {
                                    try
                                    {
                                        languageSelector.Selection = lp;
                                    }
                                    catch
                                    {
                                        continue;
                                    }

                                    tbFrom.Focus();
                                    return;
                                }
                            }
                        }
                        else
                        {
                            foreach(LanguagePair lp in currentProfile.GetLanguagePairs())
                            {
                                if(lp.From == detectedLanguage &&
                                    LanguageHelper.IsLanguageSupported(systemCulture, lp.To)
                                )
                                {
                                    try
                                    {
                                        languageSelector.Selection = lp;
                                    }
                                    catch
                                    {
                                        continue;
                                    }
                                    tbFrom.Focus();
                                    return;
                                }
                            }
                        }
                    }

                }
            }
        }
Пример #2
0
        void TranslateMainFormInputLanguageChanged(object sender, InputLanguageChangedEventArgs e)
        {
            UpdateCurrentInputLanguage();

            if(!TranslateOptions.Instance.GuessingOptions.SwitchDirectionBasedOnLayout)
                return;

            if(skipChangeLayout)
                return;

            if(languageSelector.Selection == null)
                return;

            if(!InputLanguageManager.IsLanguageSupported(languageSelector.Selection.From))
            {

                bool default_selected = currentProfile == TranslateOptions.Instance.DefaultProfile;
                UserTranslateProfile upf;

                //step 0. seek in current if not default
                upf = currentProfile as UserTranslateProfile;
                if(upf != null)
                {
                    if(upf.TranslationDirection.From == Language.Any || InputLanguageManager.IsLanguageSupported(upf.TranslationDirection.From))
                    {
                        tbFrom.Focus();
                        return;
                    }

                    foreach(LanguagePair lp in languageSelector.History)
                    {
                        if(InputLanguageManager.IsLanguageSupported(lp.From))
                        {
                            try
                            {
                                skipChangeInput = true;
                                languageSelector.Selection = lp;
                                UpdateCaption();
                            }
                            finally
                            {
                                skipChangeInput = false;
                            }
                            tbFrom.Focus();
                            return;
                        }
                    }
                }

                //step 1. seek in history
                ProfilesHistory ph_to_delete = new ProfilesHistory();
                bool found = false;
                foreach(ProfilesHistoryData phd in TranslateOptions.Instance.ProfilesHistory)
                {
                    if(InputLanguageManager.IsLanguageSupported(phd.Language))
                    {
                        TranslateProfile pf = TranslateOptions.Instance.Profiles.GetByName(phd.Name);
                        if(pf == null)
                        {	//here we should not to be, but
                            ph_to_delete.Add(phd);
                            continue;
                        }

                        upf = pf as UserTranslateProfile;
                        if(upf != null &&
                            (upf.TranslationDirection.From == Language.Any || InputLanguageManager.IsLanguageSupported(upf.TranslationDirection.From)) &&
                            !upf.ShowLanguages
                        )
                        {
                            skipChangeInput = true;
                            ActivateProfile(upf);
                            tbFrom.Focus();
                            found = true;
                            break;
                        }

                        foreach(LanguagePair lp in pf.History)
                        {
                            if(InputLanguageManager.IsLanguageSupported(lp.From))
                            {
                                try
                                {
                                    skipChangeInput = true;
                                    ActivateProfile(pf);
                                    languageSelector.Selection = lp;
                                    UpdateCaption();
                                }
                                finally
                                {
                                    skipChangeInput = false;
                                }
                                tbFrom.Focus();
                                found = true;
                                break;
                            }
                        }

                        if(!found)
                            ph_to_delete.Add(phd);
                        else
                            break;
                    }
                }

                //remove unsupported profiles from history
                foreach(ProfilesHistoryData phd in ph_to_delete)
                    TranslateOptions.Instance.ProfilesHistory.DeleteProfile(phd.Name);

                if(found)
                    return;

                //step 2. Generate list of profiles. default - last
                TranslateProfilesCollection profiles = new TranslateProfilesCollection();
                foreach(TranslateProfile pf in TranslateOptions.Instance.Profiles)
                {
                    if(pf == TranslateOptions.Instance.DefaultProfile)
                            continue;

                    if(pf == currentProfile)
                            continue;

                    profiles.Add(pf);
                }
                profiles.Add(TranslateOptions.Instance.DefaultProfile);

                //step 2. seek in other not default profiles
                foreach(TranslateProfile pf in profiles)
                {
                    foreach(LanguagePair lp in pf.History)
                    {
                        if(InputLanguageManager.IsLanguageSupported(lp.From))
                        {
                            try
                            {
                                skipChangeInput = true;
                                ActivateProfile(pf);
                                languageSelector.Selection = lp;
                                UpdateCaption();
                            }
                            finally
                            {
                                skipChangeInput = false;
                            }
                            tbFrom.Focus();
                            return;
                        }
                    }

                    upf = pf as UserTranslateProfile;
                    if(upf != null)
                    {
                        if(InputLanguageManager.IsLanguageSupported(upf.TranslationDirection.From))
                        {
                            skipChangeInput = true;
                            ActivateProfile(upf);
                            tbFrom.Focus();
                            return;
                        }
                    }
                }

                //switch auto
                upf = currentProfile as UserTranslateProfile;
                if((default_selected || (upf != null && upf.ShowLanguages)) &&
                (currentProfile.SelectedLanguagePair.From != Language.Autodetect)
                )
                {
                    CultureInfo inputCulture = e.Culture;
                    CultureInfo systemCulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
                    if(inputCulture.EnglishName == systemCulture.EnglishName ||
                        inputCulture.Parent.EnglishName == systemCulture.Parent.EnglishName
                    )
                    {
                        Language targetLanguage = Language.English;
                        foreach(InputLanguage il in InputLanguage.InstalledInputLanguages)
                        {
                            if(inputCulture.EnglishName != il.Culture.EnglishName &&
                                inputCulture.Parent.EnglishName != il.Culture.Parent.EnglishName)
                            {
                                for(int i = 0; i < (int)Language.Last; i++)
                                {
                                    if(InputLanguageManager.IsLanguageSupported(il, (Language)i))
                                    {
                                        targetLanguage = (Language)i;
                                        goto EndDetect;
                                    }
                                }
                            }
                        }

                        EndDetect:

                        foreach(LanguagePair lp in currentProfile.GetLanguagePairs())
                        {
                            if(LanguageHelper.IsLanguageSupported(inputCulture, lp.From) &&
                                lp.To == targetLanguage
                            )
                            {
                                languageSelector.Selection = lp;
                                tbFrom.Focus();
                                return;
                            }
                        }
                    }
                    else
                    {
                        foreach(LanguagePair lp in currentProfile.GetLanguagePairs())
                        {
                            if(lp.From != Language.Any &&
                                LanguageHelper.IsLanguageSupported(inputCulture, lp.From) &&
                                LanguageHelper.IsLanguageSupported(systemCulture, lp.To)
                            )
                            {
                                languageSelector.Selection = lp;
                                tbFrom.Focus();
                                return;
                            }
                        }
                    }
                }
            }
            tbFrom.Focus();
        }
Пример #3
0
        public SetProfileNameForm(UserTranslateProfile profile, TranslateProfilesCollection profiles)
        {
            InitializeComponent();
            RegisterLanguageEvent(OnLanguageChanged);

            this.profile = profile;
            this.profiles = profiles;

            cbFrom.Items.Clear();
            cbTo.Items.Clear();

            for(int i = 0; i < (int)Language.Last; i++)
            {
                LanguageDataContainer ld = new LanguageDataContainer((Language)i, LangPack.TranslateLanguage((Language)i));
                cbFrom.Items.Add(ld);
                cbTo.Items.Add(ld);
            }

            cbFrom.SelectedIndex = 0;
            cbTo.SelectedIndex = 0;

            foreach(string subject in Manager.Subjects)
            {
                SubjectContainer sc = new SubjectContainer(subject, LangPack.TranslateString(subject));
                cbSubject.Items.Add(sc);
            }

            SubjectContainer sc1 = new SubjectContainer(SubjectConstants.Any, LangPack.TranslateString(SubjectConstants.Any));
            cbSubject.Items.Add(sc1);

            cbSubject.SelectedIndex = 0;

            for(int i = 0; i < cbFrom.Items.Count; i++)
            {
                LanguageDataContainer ld = cbFrom.Items[i] as LanguageDataContainer;

                if(ld.Language == profile.TranslationDirection.From)
                    cbFrom.SelectedItem = ld;

                if(ld.Language == profile.TranslationDirection.To)
                    cbTo.SelectedItem = ld;
            }

            for(int i = 0; i < cbSubject.Items.Count; i++)
            {
                SubjectContainer sc  = cbSubject.Items[i] as SubjectContainer;
                if(profile.Subject == sc.Subject)
                {
                    cbSubject.SelectedItem = sc;
                    break;
                }
            }

            if(!string.IsNullOrEmpty(profile.Name))
            {
                tbName.Text = profile.Name;
            }
            else
            {
                tbName.Text = GetNewProfileName();
            }

            initialized = true;
        }