Beispiel #1
0
        public static bool HasLanguage(string Language, bool AllowDiscartingRegion = true, bool Initialize = true)
        {
            if (Initialize)
            {
                LocalizationManager.InitializeIfNeeded();
            }

            // First look for an exact match
            for (int i = 0, imax = Sources.Count; i < imax; ++i)
            {
                if (Sources[i].GetLanguageIndex(Language, false) >= 0)
                {
                    return(true);
                }
            }

            // Then allow matching "English (Canada)" to "english"
            if (AllowDiscartingRegion)
            {
                for (int i = 0, imax = Sources.Count; i < imax; ++i)
                {
                    if (Sources[i].GetLanguageIndex(Language, true) >= 0)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
        // LanguageCodeFrom can be "auto"
        // After the translation is returned from Google, it will call OnTranslationReady(TranslationResult, ErrorMsg)
        // TranslationResult will be null if translation failed
        public static void Translate(string text, string LanguageCodeFrom, string LanguageCodeTo, Action <string, string> OnTranslationReady)
        {
            LocalizationManager.InitializeIfNeeded();
            if (!GoogleTranslation.CanTranslate())
            {
                OnTranslationReady(null, "WebService is not set correctly or needs to be reinstalled");
                return;
            }
            //LanguageCodeTo = GoogleLanguages.GetGoogleLanguageCode(LanguageCodeTo);

            if (LanguageCodeTo == LanguageCodeFrom)
            {
                OnTranslationReady(text, null);
                return;
            }

            TranslationDictionary queries = new TranslationDictionary();


            // Unsupported language
            if (string.IsNullOrEmpty(LanguageCodeTo))
            {
                OnTranslationReady(string.Empty, null);
                return;
            }


            CreateQueries(text, LanguageCodeFrom, LanguageCodeTo, queries);   // can split plurals into several queries

            Translate(queries, (results, error) =>
            {
                if (!string.IsNullOrEmpty(error) || results.Count == 0)
                {
                    OnTranslationReady(null, error);
                    return;
                }

                string result = RebuildTranslation(text, queries, LanguageCodeTo);                                                      // gets the result from google and rebuilds the text from multiple queries if its is plurals
                OnTranslationReady(result, null);
            });
        }