ASP.Net Core 2.1 MVC Локализация культуры локализации - PullRequest
0 голосов
/ 22 февраля 2019

У меня есть вопрос, касающийся локализации в приложении ASP.Net Core 2.1 MVC.

Итак, у меня при запуске три поддерживаемых культуры:

services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[]
            {
                new CultureInfo("fr-CH"),
                new CultureInfo("en-GB"),
                new CultureInfo("de-DE")
            };

            options.DefaultRequestCulture = new RequestCulture(culture: "fr-CH", uiCulture: "fr-CH");
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;
            options.RequestCultureProviders.Insert(0, new CustomerCultureProvider()); // Insert on top of the list so it's the first one to be executed
            options.RequestCultureProviders.Insert(1, new WebAuthenticationBrokerCultureProvider()); // Insert as second of the list so it's the second one to be executed
        });

Мой файл ресурсов названSharedView.en.resx, SharedView.fr.resx и SharedView.de.resx.

Итак, после прочтения этой части в документации :

"When searching for a resource, localization engages in "culture fallback". Starting from the requested culture, if not found, it reverts to the parent culture of that culture. As an aside, the CultureInfo.Parent property represents the parent culture. This usually (but not always) means removing the national signifier from the ISO. For example, the dialect of Spanish spoken in Mexico is "es-MX". It has the parent "es"—Spanish non-specific to any country.

Imagine your site receives a request for a "Welcome" resource using culture "fr-CA". The localization system looks for the following resources, in order, and selects the first match:

Welcome.fr-CA.resx
Welcome.fr.resx
Welcome.resx (if the NeutralResourcesLanguage is "fr-CA")"

Я думал, что когда я сделаю запрос наподобие https://localhost:xxxxx/Account/Login?cultur=en-US,, у меня будут мои переводы на английский, но он не работает и возвращается к fr-CH.

Если я использую https://localhost:xxxxx/Account/Login?cultur=en-GB это работает, и у меня есть мой дисплей на английском языке.

Так что я не вижу, что мне не хватает, я неправильно понимаю документацию?

Заранее спасибо за помощь

1 Ответ

0 голосов
/ 22 февраля 2019

если не найден, он возвращается к родительской культуре этой культуры

en-GB не является родительской культурой en-US , поэтому он возвращается к культуре по умолчанию fr-CH .

Когда выбранная культура en-US , которой у вас нетв поддерживаемых культурах он вернется к родительской культуре en , что означает, что запрашиваемый ресурс будет welcome.en.resx .

такой же для ru культура, если он не определен, выбранным ресурсом будет welcome.resx ,

Наконец, если ресурс не найден, будет выбрана культура по умолчанию.

Между тем, у каждой культуры есть одна родительская культура, которую вы можете найти с помощью CultureInfo.Parent

Более подробная информация здесь: Процесс восстановления ресурса

...