Compartir a través de


How to determine the user-preferred UI language from a Direct3D app for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

This topic describes how to determine the preferred UI language from a pure native app. Windows Phone users can choose a display language for their phone, and apps can choose to provide localized content based on the language the user selects in the phone’s settings. For a list of supported languages, see Culture and language support for Windows Phone.

Windows Phone Direct3D apps do not support many of the localization features that are available for managed apps, including localized resource files. You can query for the user-preferred UI language using the GetUserPreferredUILanguages function. This will return the current UI language of the phone language identifier format, or in language name format, although the language name format is recommended as a programming best practice. Your app can use the value that is returned to load different sets of assets for different languages.

Example

The following code example shows the usage of GetUserPreferredUILanguages. Your app calls the function twice. The first call returns the number size of the buffer it needs to contain the returned language name or ID. The function also returns the number of preferred languages on the device, but on Windows Phone this value will always be 1. If the first call succeeds, allocate a buffer of the correct size and pass it into the second call to the function. Then you can decide which assets to load based on the returned value.

    ULONG numLanguages = 0;
    DWORD cchLanguagesBuffer = 0;
    BOOL hr = GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numLanguages, NULL, &cchLanguagesBuffer);

    if (hr) {
          WCHAR* pwszLanguagesBuffer = new WCHAR[cchLanguagesBuffer];
          hr = GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numLanguages, pwszLanguagesBuffer, &cchLanguagesBuffer);
          if (hr) {
              if (wcsncmp(pwszLanguagesBuffer, L"en-", 3) == 0) { // any English: en-*
                  MyLoadResources(1);
              } else if (wcscmp(pwszLanguagesBuffer, L"pt-BR") == 0) { // Brazilian dialect of Portuguese
                  MyLoadResources(2);
              }
              delete pwszLanguagesBuffer;
        }
    }

This example uses the MUI_LANGUAGE_NAME constant as the first argument to indicate that the language should be returned in name format. Use MUI_LANGUAGE_ID to obtain the language identifier format.