diff --git a/device/README.md b/device/README.md index ede7bdb09..395b8793d 100644 --- a/device/README.md +++ b/device/README.md @@ -103,6 +103,20 @@ Get the device's current language locale code. -------------------- +### getRegionCode() + +```typescript +getRegionCode() => Promise +``` + +Get the device's current language locale code. + +**Returns:** Promise<GetRegionCodeResult> + +**Since:** 4.2.0 + +-------------------- + ### getLanguageTag() @@ -163,6 +177,12 @@ Get the device's current language locale tag. | **`value`** | string | Two character language code. | 1.0.0 | +#### GetRegionCodeResult + +| Prop | Type | Description | Since | +| ----------- | ------------------- | --------------------------------- | ----- | +| **`value`** | string | Two character of the region code. | 4.2.0 | + #### LanguageTag | Prop | Type | Description | Since | diff --git a/device/android/src/main/java/com/capacitorjs/plugins/device/DevicePlugin.java b/device/android/src/main/java/com/capacitorjs/plugins/device/DevicePlugin.java index 342835399..7441626ba 100644 --- a/device/android/src/main/java/com/capacitorjs/plugins/device/DevicePlugin.java +++ b/device/android/src/main/java/com/capacitorjs/plugins/device/DevicePlugin.java @@ -64,6 +64,13 @@ public void getLanguageCode(PluginCall call) { call.resolve(ret); } + @PluginMethod + public void getRegionCode(PluginCall call) { + JSObject ret = new JSObject(); + ret.put("value", Locale.getDefault().getCountry()); + call.resolve(ret); + } + @PluginMethod public void getLanguageTag(PluginCall call) { JSObject ret = new JSObject(); diff --git a/device/ios/Plugin/Device.swift b/device/ios/Plugin/Device.swift index 1922eb7e6..5373aaa66 100644 --- a/device/ios/Plugin/Device.swift +++ b/device/ios/Plugin/Device.swift @@ -66,6 +66,10 @@ import Foundation return String(Locale.preferredLanguages[0].prefix(2)) } + public func getRegionCode() -> String { + return String(Locale.current.regionCode) + } + public func getLanguageTag() -> String { return String(Locale.preferredLanguages[0]) } diff --git a/device/ios/Plugin/DevicePlugin.m b/device/ios/Plugin/DevicePlugin.m index 0c25f907a..8a5da6384 100644 --- a/device/ios/Plugin/DevicePlugin.m +++ b/device/ios/Plugin/DevicePlugin.m @@ -8,5 +8,6 @@ CAP_PLUGIN_METHOD(getInfo, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(getBatteryInfo, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(getLanguageCode, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(getRegionCode, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(getLanguageTag, CAPPluginReturnPromise); ) diff --git a/device/ios/Plugin/DevicePlugin.swift b/device/ios/Plugin/DevicePlugin.swift index b5dc66905..95245dbea 100644 --- a/device/ios/Plugin/DevicePlugin.swift +++ b/device/ios/Plugin/DevicePlugin.swift @@ -64,6 +64,13 @@ public class DevicePlugin: CAPPlugin { ]) } + @objc func getRegionCode(_ call: CAPPluginCall) { + let code = implementation.getRegionCode() + call.resolve([ + "value": code + ]) + } + @objc func getLanguageTag(_ call: CAPPluginCall) { let tag = implementation.getLanguageTag() call.resolve([ diff --git a/device/src/definitions.ts b/device/src/definitions.ts index 093528efb..9bbb72102 100644 --- a/device/src/definitions.ts +++ b/device/src/definitions.ts @@ -144,6 +144,15 @@ export interface GetLanguageCodeResult { value: string; } +export interface GetRegionCodeResult { + /** + * Two character of the region code. + * + * @since 4.2.0 + */ + value: string; +} + export interface LanguageTag { /** * Returns a well-formed IETF BCP 47 language tag. diff --git a/device/src/web.ts b/device/src/web.ts index ecc88e7b5..89ee8af5c 100644 --- a/device/src/web.ts +++ b/device/src/web.ts @@ -6,6 +6,7 @@ import type { DeviceInfo, DevicePlugin, GetLanguageCodeResult, + GetRegionCodeResult, LanguageTag, } from './definitions'; @@ -72,6 +73,10 @@ export class DeviceWeb extends WebPlugin implements DevicePlugin { }; } + async getRegionCode(): Promise { + throw this.unavailable('Region code not available in this browser'); + } + async getLanguageTag(): Promise { return { value: navigator.language,