Exemplo n.º 1
0
        /// <summary>
        /// Returns one L10NCultureInfo object for each distinct language found in the collection of all
        /// cultures on the computer. Some languages are represented by more than one culture, and in
        /// those cases just the first culture is returned. There are several reasons for multiple
        /// cultures per language, the predominant one being there is more than one writing system
        /// for the language. An example of this is Chinese which has a Traditional and a Simplified
        /// writing system. Other languages have a Latin and a Cyrilic writing system.
        ///
        /// Due to changes made in how this procedure determines what languages to return, it is
        /// possible that there may be an existing localization tied to a culture that is no longer
        /// returned in the collection. Because of this, a check is done to make sure all cultures
        /// represented by existing localizations are included in the list that is returned. This
        /// will result in that language being in the list twice, each instance having a different
        /// DisplayName.
        /// </summary>
        /// <param name="returnOnlyLanguagesHavingLocalizations">
        /// If TRUE then only languages represented by existing localizations are returned. If FALSE
        /// then all languages found are returned.
        /// </param>
        /// <returns>IEnumerable of L10NCultureInfo declared as IEnumerable of CultureInfo</returns>
        public static IEnumerable <L10NCultureInfo> GetUILanguages(bool returnOnlyLanguagesHavingLocalizations)
        {
            // BL-922, filter out duplicate languages. It may be surprising that we get more than one
            // neutral culture for a given language; however, some languages are written in more than one
            // script, and each script can have a neutral culture (e.g., uz-Cyrl, uz-Latn). We may eventually
            // have a need to localize into two scripts of the same language, but until users actually ask
            // for this, it just confuses things, so we're not supporting it.

            // first, get all installed cultures
            var allCultures = from ci in L10NCultureInfo.GetCultures(CultureTypes.NeutralCultures)
                              where ci.TwoLetterISOLanguageName != "iv"
                              select ci;

            // second, group by ISO 3 letter language code
            var groups = allCultures.GroupBy(c => c.ThreeLetterISOLanguageName);

            // finally, select the first culture in each language code group
            var allLangs = groups.Select(g => g.First()).ToList();

            var langsHavingLocalizations = GetAvailableLocalizedLanguages();

            // BL-1011: Make sure cultures that have existing localizations are included
            var missingCultures = langsHavingLocalizations.Where(l => allLangs.Any(al => al.Name == l) == false);

            allLangs.AddRange(missingCultures.Select(lang =>
            {
                try
                {
                    return(new L10NCultureInfo(lang));                    // return to the select, that is
                }
                catch (CultureNotFoundException)
                {
                    // Unfortunately there is no way to create a CultureInfo for a culture the system
                    // doesn't recognize, so we just can't offer this language on this system
                    // (short of at least a major change of API).
                    return(null);                    // to the Select; filtered out below
                }
            }).Where(ci => ci != null));

            if (!returnOnlyLanguagesHavingLocalizations)
            {
                return(from ci in allLangs
                       orderby ci.DisplayName
                       select ci);
            }

            return(from ci in allLangs
                   where langsHavingLocalizations.Contains(ci.Name)
                   orderby ci.DisplayName
                   select ci);
        }
        private static ILocalizationManager Create(string desiredUiLangId, string appId,
                                                   string appName, string relativeSettingPathForLocalizationFolder,
                                                   Icon applicationIcon, Func <string, ILocalizationManagerInternal <T> > createMethod)
        {
            if (string.IsNullOrEmpty(relativeSettingPathForLocalizationFolder))
            {
                relativeSettingPathForLocalizationFolder = appName;
            }
            else if (Path.IsPathRooted(relativeSettingPathForLocalizationFolder))
            {
                throw new ArgumentException("Relative (non-rooted) path expected", "relativeSettingPathForLocalizationFolder");
            }

            var directoryOfWritableTranslationFiles = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                                                                   relativeSettingPathForLocalizationFolder, "localizations");

            if (!LoadedManagers.TryGetValue(appId, out var lm))
            {
                lm = createMethod(directoryOfWritableTranslationFiles);

                LoadedManagers[appId] = lm;
                PreviouslyLoadedManagers.Remove(appId);
            }

            lm.ApplicationIcon = applicationIcon;

            if (string.IsNullOrEmpty(desiredUiLangId))
            {
                desiredUiLangId = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
            }

            var ci = L10NCultureInfo.GetCultureInfo(desiredUiLangId);

            if (!GetUILanguages(true).Contains(ci))
            {
                using (var dlg = new LanguageChoosingDialog(ci, applicationIcon))
                {
                    dlg.ShowDialog();
                    desiredUiLangId = dlg.SelectedLanguage;
                }
            }

            LocalizationManager.SetUILanguage(desiredUiLangId, false);

            LocalizationManager.EnableClickingOnControlToBringUpLocalizationDialog = true;

            return(lm);
        }
Exemplo n.º 3
0
        /// ------------------------------------------------------------------------------------
        public static void SetUILanguage(string langId,
                                         bool reapplyLocalizationsToAllObjectsInAllManagers)
        {
            if (UILanguageId == langId || string.IsNullOrEmpty(langId))
            {
                return;
            }
            var ci = L10NCultureInfo.GetCultureInfo(langId);

            if (ci.RawCultureInfo != null)
            {
                Thread.CurrentThread.CurrentUICulture = ci.RawCultureInfo;
            }
            else
            {
                Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
            }
            L10NCultureInfo.CurrentCulture = ci;
            s_uiLangId = langId;

            switch (TranslationMemoryKind)
            {
            default:
                LocalizationManagerInternal <TMXDocument> .SetAvailableFallbackLanguageIds(GetAvailableLocalizedLanguages());

                break;

            case TranslationMemory.XLiff:
                LocalizationManagerInternal <XLiffDocument> .SetAvailableFallbackLanguageIds(GetAvailableLocalizedLanguages());

                break;
            }

            if (reapplyLocalizationsToAllObjectsInAllManagers)
            {
                ReapplyLocalizationsToAllObjectsInAllManagers();
            }
        }