|
| 1 | +LocaleRegion |
| 2 | +=== |
| 3 | + |
| 4 | +# Background |
| 5 | +Developers have requested access to programmatically choose the specific locale/region to use in WebView2. |
| 6 | +The locale/region updates the JavaScript region (objects in the ECMA standard) which gets reflected as |
| 7 | +variations in time and date formats. Currently, the locale is by default set to the same value as the |
| 8 | +display language. |
| 9 | + |
| 10 | +You can use the `LocaleRegion` API to update the locale value individually from the display |
| 11 | +language. |
| 12 | + |
| 13 | +# Description |
| 14 | +* The intended value for `LocaleRegion` is in the format of `language[-country]` where `language` is the |
| 15 | +2-letter code from [ISO 639](https://www.iso.org/iso-639-language-codes.html) and `country` is the |
| 16 | +2-letter code from [ISO 3166](https://www.iso.org/standard/72482.html). |
| 17 | +* By default, the `LocaleRegion` will be set as the value for the Limited option in the browser. |
| 18 | +That means that if the OS region and the display language share the same language code, |
| 19 | +like 'en-GB' and 'en-US', the value will be set to the OS region. |
| 20 | + |
| 21 | +# Examples |
| 22 | +```cpp |
| 23 | +auto webViewEnvironment10 = m_webViewEnvironment.try_query<ICoreWebView2Environment10>(); |
| 24 | +wil::com_ptr<ICoreWebView2ControllerOptions> options; |
| 25 | +HRESULT hr = webViewEnvironment10->CreateCoreWebView2ControllerOptions(&options); |
| 26 | +options->put_LocaleRegion(m_webviewOption.localeRegion.c_str()); |
| 27 | +``` |
| 28 | +
|
| 29 | +```c# |
| 30 | +CoreWebView2Environment _webViewEnvironment; |
| 31 | +WebViewCreationOptions _creationOptions; |
| 32 | +public CreateWebView2Controller(IntPtr parentWindow) |
| 33 | +{ |
| 34 | + CoreWebView2ControllerOptions controllerOptions = new CoreWebView2ControllerOptions(); |
| 35 | + controllerOptions.LocaleRegion = _creationOptions.localeRegion; |
| 36 | + CoreWebView2Controller controller = null; |
| 37 | + if (_creationOptions.entry == WebViewCreateEntry.CREATE_WITH_OPTION) |
| 38 | + { |
| 39 | + controller = await _webViewEnvironment.CreateCoreWebView2ControllerAsync(parentWindow, options); |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + controller = await _webViewEnvironment.CreateCoreWebView2ControllerAsync(parentWindow); |
| 44 | + } |
| 45 | + // update locale with value |
| 46 | + SetAppLocale(localeRegion); |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +# API Details |
| 51 | +```cpp |
| 52 | +[uuid(0c9a374f-20c3-4e3c-a640-67b78a7e0a48), object, pointer_default(unique)] |
| 53 | +interface ICoreWebView2ControllerOptions : IUnknown { |
| 54 | + /// Interface for locale region that is updated through the ControllerOptions |
| 55 | + /// API. |
| 56 | + /// The default region for WebView. It applies to browser UI such as |
| 57 | + /// in the time/date formats. The intended locale value is in the format of |
| 58 | + /// `language[-country]` where `language` is the 2-letter code from [ISO |
| 59 | + /// 639](https://www.iso.org/iso-639-language-codes.html) and `country` is the |
| 60 | + /// 2-letter code from [ISO 3166](https://www.iso.org/standard/72482.html). |
| 61 | + /// |
| 62 | + /// Validation is done on the V8 engine to match on the closest locale |
| 63 | + /// from the passed in locale region value. For example, passing in "en_gb" |
| 64 | + /// will reflect the "en-GB" locale in V8. |
| 65 | + /// If V8 cannot find any matching locale on the input value, it will default |
| 66 | + /// to the display language as the locale. |
| 67 | + /// |
| 68 | + /// The caller must free the returned string with `CoTaskMemFree`. See |
| 69 | + /// [API Conventions](/microsoft-edge/webview2/concepts/win32-api-conventions#strings). |
| 70 | + /// \snippet AppWindow.cpp RegionLocaleSetting |
| 71 | + |
| 72 | + [propget] HRESULT LocaleRegion([out, retval] LPWSTR* locale); |
| 73 | + |
| 74 | + /// Sets the `Region` property. |
| 75 | + |
| 76 | + [propput] HRESULT LocaleRegion([in] LPCWSTR locale); |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +```c# |
| 81 | +namespace Microsoft.Web.WebView2.Core |
| 82 | +{ |
| 83 | + runtimeclass CoreWebView2ControllerOptions |
| 84 | + { |
| 85 | + // ... |
| 86 | + [interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2ControllerOptions")] |
| 87 | + { |
| 88 | + |
| 89 | + String LocaleRegion { get; set; }; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments