string ParseTranslationResult(string html, string OriginalText)
        {
            try
            {
                // This is a Hack for reading Google Translation while Google doens't change their response format
                int iStart = html.IndexOf("TRANSLATED_TEXT='") + "TRANSLATED_TEXT='".Length;
                int iEnd   = html.IndexOf("';var", iStart);

                string Translation = html.Substring(iStart, iEnd - iStart);

                // Convert to normalized HTML
                Translation = System.Text.RegularExpressions.Regex.Replace(Translation,
                                                                           @"\\x([a-fA-F0-9]{2})",
                                                                           match => char.ConvertFromUtf32(Int32.Parse(match.Groups[1].Value, System.Globalization.NumberStyles.HexNumber)));

                // Convert ASCII Characters
                Translation = System.Text.RegularExpressions.Regex.Replace(Translation,
                                                                           @"&#(\d+);",
                                                                           match => char.ConvertFromUtf32(Int32.Parse(match.Groups[1].Value)));

                Translation = Translation.Replace("<br>", "\n");

                if (OriginalText.ToUpper() == OriginalText)
                {
                    Translation = Translation.ToUpper();
                }
                else
                if (GoogleTranslation.UppercaseFirst(OriginalText) == OriginalText)
                {
                    Translation = GoogleTranslation.UppercaseFirst(Translation);
                }
                else
                if (GoogleTranslation.TitleCase(OriginalText) == OriginalText)
                {
                    Translation = GoogleTranslation.TitleCase(Translation);
                }

                return(Translation);
            }
            catch (System.Exception ex)
            {
                Debug.LogError(ex.Message);
                return(string.Empty);
            }
        }
Beispiel #2
0
        public void OnLocalize(bool Force = false)
        {
            if (!Force && (!enabled || gameObject == null || !gameObject.activeInHierarchy))
            {
                return;
            }

            if (string.IsNullOrEmpty(LocalizationManager.CurrentLanguage))
            {
                return;
            }

            if (!AlwaysForceLocalize && !Force && !LocalizeCallBack.HasCallback() && LastLocalizedLanguage == LocalizationManager.CurrentLanguage)
            {
                return;
            }
            LastLocalizedLanguage = LocalizationManager.CurrentLanguage;

            if (!HasTargetCache())
            {
                FindTarget();
            }

            if (!HasTargetCache())
            {
                return;
            }

            // These are the terms actually used (will be mTerm/mSecondaryTerm or will get them from the objects if those are missing. e.g. Labels' text and font name)
            if (string.IsNullOrEmpty(FinalTerm) || string.IsNullOrEmpty(FinalSecondaryTerm))
            {
                GetFinalTerms(out FinalTerm, out FinalSecondaryTerm);
            }


            bool hasCallback = Application.isPlaying && LocalizeCallBack.HasCallback();

            if (!hasCallback && string.IsNullOrEmpty(FinalTerm) && string.IsNullOrEmpty(FinalSecondaryTerm))
            {
                return;
            }

            CallBackTerm          = FinalTerm;
            CallBackSecondaryTerm = FinalSecondaryTerm;
            MainTranslation       = string.IsNullOrEmpty(FinalTerm) || FinalTerm == "-" ? null : LocalizationManager.GetTermTranslation(FinalTerm, false);
            SecondaryTranslation  = string.IsNullOrEmpty(FinalSecondaryTerm) || FinalSecondaryTerm == "-" ? null : LocalizationManager.GetTermTranslation(FinalSecondaryTerm, false);

            if (!hasCallback && /*string.IsNullOrEmpty (MainTranslation)*/ string.IsNullOrEmpty(FinalTerm) && string.IsNullOrEmpty(SecondaryTranslation))
            {
                return;
            }

            CurrentLocalizeComponent = this;
            if (Application.isPlaying)
            {
                LocalizeCallBack.Execute(this);                   // This allows scripts to modify the translations :  e.g. "Player {0} wins"  ->  "Player Red wins"
                LocalizationManager.ApplyLocalizationParams(ref MainTranslation, gameObject);
            }
            bool applyRTL = LocalizationManager.IsRight2Left && !IgnoreRTL;

            if (applyRTL)
            {
                if (AllowMainTermToBeRTL && !string.IsNullOrEmpty(MainTranslation))
                {
                    MainTranslation = LocalizationManager.ApplyRTLfix(MainTranslation, MaxCharactersInRTL, IgnoreNumbersInRTL);
                }
                if (AllowSecondTermToBeRTL && !string.IsNullOrEmpty(SecondaryTranslation))
                {
                    SecondaryTranslation = LocalizationManager.ApplyRTLfix(SecondaryTranslation);
                }
            }

            if (PrimaryTermModifier != TermModification.DontModify)
            {
                MainTranslation = MainTranslation ?? string.Empty;
            }

            switch (PrimaryTermModifier)
            {
            case TermModification.ToUpper: MainTranslation = MainTranslation.ToUpper(); break;

            case TermModification.ToLower: MainTranslation = MainTranslation.ToLower(); break;

            case TermModification.ToUpperFirst: MainTranslation = GoogleTranslation.UppercaseFirst(MainTranslation); break;

            case TermModification.ToTitle: MainTranslation = GoogleTranslation.TitleCase(MainTranslation); break;
            }

            if (SecondaryTermModifier != TermModification.DontModify)
            {
                SecondaryTranslation = SecondaryTranslation ?? string.Empty;
            }

            switch (SecondaryTermModifier)
            {
            case TermModification.ToUpper: SecondaryTranslation = SecondaryTranslation.ToUpper();  break;

            case TermModification.ToLower: SecondaryTranslation = SecondaryTranslation.ToLower();  break;

            case TermModification.ToUpperFirst: SecondaryTranslation = GoogleTranslation.UppercaseFirst(SecondaryTranslation); break;

            case TermModification.ToTitle: SecondaryTranslation = GoogleTranslation.TitleCase(SecondaryTranslation); break;
            }
            if (!string.IsNullOrEmpty(TermPrefix))
            {
                MainTranslation = applyRTL ? MainTranslation + TermPrefix : TermPrefix + MainTranslation;
            }
            if (!string.IsNullOrEmpty(TermSuffix))
            {
                MainTranslation = applyRTL ? TermSuffix + MainTranslation : MainTranslation + TermSuffix;
            }

            EventDoLocalize(MainTranslation, SecondaryTranslation);
            CurrentLocalizeComponent = null;
        }
Beispiel #3
0
        public void OnLocalize(bool Force = false)
        {
            if (!Force && (!enabled || gameObject == null || !gameObject.activeInHierarchy))
            {
                return;
            }

            if (string.IsNullOrEmpty(LocalizationManager.CurrentLanguage))
            {
                return;
            }

            if (!Force && LastLocalizedLanguage == LocalizationManager.CurrentLanguage)
            {
                return;
            }
            LastLocalizedLanguage = LocalizationManager.CurrentLanguage;

            if (!HasTargetCache())
            {
                FindTarget();
            }

            if (!HasTargetCache())
            {
                return;
            }

            // This are the terms actually used (will be mTerm/mSecondaryTerm or will get them from the objects if those are missing. e.g. Labels' text and font name)
            if (string.IsNullOrEmpty(FinalTerm) || string.IsNullOrEmpty(FinalSecondaryTerm))
            {
                GetFinalTerms(out FinalTerm, out FinalSecondaryTerm);
            }

            if (string.IsNullOrEmpty(FinalTerm) && string.IsNullOrEmpty(FinalSecondaryTerm))
            {
                return;
            }

            CallBackTerm          = FinalTerm;
            CallBackSecondaryTerm = FinalSecondaryTerm;
            MainTranslation       = LocalizationManager.GetTermTranslation(FinalTerm);
            SecondaryTranslation  = LocalizationManager.GetTermTranslation(FinalSecondaryTerm);

            if (string.IsNullOrEmpty(MainTranslation) && string.IsNullOrEmpty(SecondaryTranslation))
            {
                return;
            }

            CurrentLocalizeComponent = this;
            LocalizeCallBack.Execute(this);                // This allows scripts to modify the translations :  e.g. "Player {0} wins"  ->  "Player Red wins"

            if (LocalizationManager.IsRight2Left && !IgnoreRTL)
            {
                if (AllowMainTermToBeRTL && !string.IsNullOrEmpty(MainTranslation))
                {
                    MainTranslation = LocalizationManager.ApplyRTLfix(MainTranslation, MaxCharactersInRTL);
                }
                if (AllowSecondTermToBeRTL && !string.IsNullOrEmpty(SecondaryTranslation))
                {
                    SecondaryTranslation = LocalizationManager.ApplyRTLfix(SecondaryTranslation);
                }
            }
            switch (PrimaryTermModifier)
            {
            case TermModification.ToUpper: MainTranslation = MainTranslation.ToUpper(); break;

            case TermModification.ToLower: MainTranslation = MainTranslation.ToLower(); break;

            case TermModification.ToUpperFirst: MainTranslation = GoogleTranslation.UppercaseFirst(MainTranslation); break;

            case TermModification.ToTitle: MainTranslation = GoogleTranslation.TitleCase(MainTranslation); break;
            }
            switch (SecondaryTermModifier)
            {
            case TermModification.ToUpper: SecondaryTranslation = SecondaryTranslation.ToUpper();  break;

            case TermModification.ToLower: SecondaryTranslation = SecondaryTranslation.ToLower();  break;

            case TermModification.ToUpperFirst: SecondaryTranslation = GoogleTranslation.UppercaseFirst(SecondaryTranslation); break;

            case TermModification.ToTitle: SecondaryTranslation = GoogleTranslation.TitleCase(SecondaryTranslation); break;
            }
            EventDoLocalize(MainTranslation, SecondaryTranslation);
            CurrentLocalizeComponent = null;
        }
Beispiel #4
0
        public void OnLocalize(bool Force = false)
        {
            if (!Force && (!enabled || gameObject == null || !gameObject.activeInHierarchy))
            {
                return;
            }

            if (string.IsNullOrEmpty(LocalizationManager.CurrentLanguage))
            {
                return;
            }

            if (!AlwaysForceLocalize && !Force && !HasCallback() && LastLocalizedLanguage == LocalizationManager.CurrentLanguage)
            {
                return;
            }
            LastLocalizedLanguage = LocalizationManager.CurrentLanguage;

            // These are the terms actually used (will be mTerm/mSecondaryTerm or will get them from the objects if those are missing. e.g. Labels' text and font name)
            if (string.IsNullOrEmpty(FinalTerm) || string.IsNullOrEmpty(FinalSecondaryTerm))
            {
                GetFinalTerms(out FinalTerm, out FinalSecondaryTerm);
            }


            bool hasCallback = I2Utils.IsPlaying() && HasCallback();

            if (!hasCallback && string.IsNullOrEmpty(FinalTerm) && string.IsNullOrEmpty(FinalSecondaryTerm))
            {
                return;
            }

            CallBackTerm          = FinalTerm;
            CallBackSecondaryTerm = FinalSecondaryTerm;
            MainTranslation       = (string.IsNullOrEmpty(FinalTerm) || FinalTerm == "-") ? null : LocalizationManager.GetTranslation(FinalTerm, false);
            SecondaryTranslation  = (string.IsNullOrEmpty(FinalSecondaryTerm) || FinalSecondaryTerm == "-") ? null : LocalizationManager.GetTranslation(FinalSecondaryTerm, false);

            if (!hasCallback && /*string.IsNullOrEmpty (MainTranslation)*/ string.IsNullOrEmpty(FinalTerm) && string.IsNullOrEmpty(SecondaryTranslation))
            {
                return;
            }

            CurrentLocalizeComponent = this;

            {
                LocalizeCallBack.Execute(this);                   // This allows scripts to modify the translations :  e.g. "Player {0} wins"  ->  "Player Red wins"
                LocalizeEvent.Invoke();
                if (AllowParameters)
                {
                    LocalizationManager.ApplyLocalizationParams(ref MainTranslation, gameObject, AllowLocalizedParameters);
                }
            }

            if (!FindTarget())
            {
                return;
            }
            bool applyRTL = LocalizationManager.IsRight2Left && !IgnoreRTL;

            if (MainTranslation != null)
            {
                switch (PrimaryTermModifier)
                {
                case TermModification.ToUpper:      MainTranslation = MainTranslation.ToUpper(); break;

                case TermModification.ToLower:      MainTranslation = MainTranslation.ToLower(); break;

                case TermModification.ToUpperFirst: MainTranslation = GoogleTranslation.UppercaseFirst(MainTranslation); break;

                case TermModification.ToTitle:      MainTranslation = GoogleTranslation.TitleCase(MainTranslation); break;
                }
                if (!string.IsNullOrEmpty(TermPrefix))
                {
                    MainTranslation = applyRTL ? MainTranslation + TermPrefix : TermPrefix + MainTranslation;
                }
                if (!string.IsNullOrEmpty(TermSuffix))
                {
                    MainTranslation = applyRTL ? TermSuffix + MainTranslation : MainTranslation + TermSuffix;
                }

                if (AddSpacesToJoinedLanguages && LocalizationManager.HasJoinedWords && !string.IsNullOrEmpty(MainTranslation))
                {
                    var sb = new System.Text.StringBuilder();
                    sb.Append(MainTranslation[0]);
                    for (int i = 1, imax = MainTranslation.Length; i < imax; ++i)
                    {
                        sb.Append(' ');
                        sb.Append(MainTranslation[i]);
                    }

                    MainTranslation = sb.ToString();
                }
                if (applyRTL && mLocalizeTarget.AllowMainTermToBeRTL() && !string.IsNullOrEmpty(MainTranslation))
                {
                    MainTranslation = LocalizationManager.ApplyRTLfix(MainTranslation, MaxCharactersInRTL, IgnoreNumbersInRTL);
                }
            }

            if (SecondaryTranslation != null)
            {
                switch (SecondaryTermModifier)
                {
                case TermModification.ToUpper:      SecondaryTranslation = SecondaryTranslation.ToUpper(); break;

                case TermModification.ToLower:      SecondaryTranslation = SecondaryTranslation.ToLower(); break;

                case TermModification.ToUpperFirst: SecondaryTranslation = GoogleTranslation.UppercaseFirst(SecondaryTranslation); break;

                case TermModification.ToTitle:      SecondaryTranslation = GoogleTranslation.TitleCase(SecondaryTranslation); break;
                }
                if (applyRTL && mLocalizeTarget.AllowSecondTermToBeRTL() && !string.IsNullOrEmpty(SecondaryTranslation))
                {
                    SecondaryTranslation = LocalizationManager.ApplyRTLfix(SecondaryTranslation);
                }
            }

            if (LocalizationManager.HighlightLocalizedTargets)
            {
                MainTranslation = "LOC:" + FinalTerm;
            }

            mLocalizeTarget.DoLocalize(this, MainTranslation, SecondaryTranslation);

            CurrentLocalizeComponent = null;
        }
Beispiel #5
0
        public void OnLocalize(bool Force = false)
        {
            if ((!Force && (!base.enabled || base.gameObject == null || !base.gameObject.activeInHierarchy)) || string.IsNullOrEmpty(LocalizationManager.CurrentLanguage) || (!AlwaysForceLocalize && !Force && !LocalizeCallBack.HasCallback() && LastLocalizedLanguage == LocalizationManager.CurrentLanguage))
            {
                return;
            }
            LastLocalizedLanguage = LocalizationManager.CurrentLanguage;
            if (!HasTargetCache())
            {
                FindTarget();
            }
            if (!HasTargetCache())
            {
                return;
            }
            if (string.IsNullOrEmpty(FinalTerm) || string.IsNullOrEmpty(FinalSecondaryTerm))
            {
                GetFinalTerms(out FinalTerm, out FinalSecondaryTerm);
            }
            bool flag = Application.isPlaying && LocalizeCallBack.HasCallback();

            if (!flag && string.IsNullOrEmpty(FinalTerm) && string.IsNullOrEmpty(FinalSecondaryTerm))
            {
                return;
            }
            CallBackTerm          = FinalTerm;
            CallBackSecondaryTerm = FinalSecondaryTerm;
            MainTranslation       = ((string.IsNullOrEmpty(FinalTerm) || FinalTerm == "-") ? null : LocalizationManager.GetTermTranslation(FinalTerm, FixForRTL: false));
            SecondaryTranslation  = ((string.IsNullOrEmpty(FinalSecondaryTerm) || FinalSecondaryTerm == "-") ? null : LocalizationManager.GetTermTranslation(FinalSecondaryTerm, FixForRTL: false));
            if (!flag && string.IsNullOrEmpty(FinalTerm) && string.IsNullOrEmpty(SecondaryTranslation))
            {
                return;
            }
            CurrentLocalizeComponent = this;
            if (Application.isPlaying)
            {
                LocalizeCallBack.Execute(this);
                LocalizationManager.ApplyLocalizationParams(ref MainTranslation, base.gameObject);
            }
            bool flag2 = LocalizationManager.IsRight2Left && !IgnoreRTL;

            if (flag2)
            {
                if (AllowMainTermToBeRTL && !string.IsNullOrEmpty(MainTranslation))
                {
                    MainTranslation = LocalizationManager.ApplyRTLfix(MainTranslation, MaxCharactersInRTL, IgnoreNumbersInRTL);
                }
                if (AllowSecondTermToBeRTL && !string.IsNullOrEmpty(SecondaryTranslation))
                {
                    SecondaryTranslation = LocalizationManager.ApplyRTLfix(SecondaryTranslation);
                }
            }
            if (PrimaryTermModifier != 0)
            {
                MainTranslation = (MainTranslation ?? string.Empty);
            }
            switch (PrimaryTermModifier)
            {
            case TermModification.ToUpper:
                MainTranslation = MainTranslation.ToUpper();
                break;

            case TermModification.ToLower:
                MainTranslation = MainTranslation.ToLower();
                break;

            case TermModification.ToUpperFirst:
                MainTranslation = GoogleTranslation.UppercaseFirst(MainTranslation);
                break;

            case TermModification.ToTitle:
                MainTranslation = GoogleTranslation.TitleCase(MainTranslation);
                break;
            }
            if (SecondaryTermModifier != 0)
            {
                SecondaryTranslation = (SecondaryTranslation ?? string.Empty);
            }
            switch (SecondaryTermModifier)
            {
            case TermModification.ToUpper:
                SecondaryTranslation = SecondaryTranslation.ToUpper();
                break;

            case TermModification.ToLower:
                SecondaryTranslation = SecondaryTranslation.ToLower();
                break;

            case TermModification.ToUpperFirst:
                SecondaryTranslation = GoogleTranslation.UppercaseFirst(SecondaryTranslation);
                break;

            case TermModification.ToTitle:
                SecondaryTranslation = GoogleTranslation.TitleCase(SecondaryTranslation);
                break;
            }
            if (!string.IsNullOrEmpty(TermPrefix))
            {
                MainTranslation = (flag2 ? (MainTranslation + TermPrefix) : (TermPrefix + MainTranslation));
            }
            if (!string.IsNullOrEmpty(TermSuffix))
            {
                MainTranslation = (flag2 ? (TermSuffix + MainTranslation) : (MainTranslation + TermSuffix));
            }
            EventDoLocalize(MainTranslation, SecondaryTranslation);
            CurrentLocalizeComponent = null;
        }