Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(device): Add getLanguageTag function #939

Merged
merged 3 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions device/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const logBatteryInfo = async () => {
* [`getInfo()`](#getinfo)
* [`getBatteryInfo()`](#getbatteryinfo)
* [`getLanguageCode()`](#getlanguagecode)
* [`getLanguageTag()`](#getlanguagetag)
* [Interfaces](#interfaces)
* [Type Aliases](#type-aliases)

Expand Down Expand Up @@ -103,6 +104,21 @@ Get the device's current language locale code.
--------------------


### getLanguageTag()

```typescript
getLanguageTag() => Promise<LanguageTag>
```

Get the device's current language locale tag.

**Returns:** <code>Promise&lt;<a href="#languagetag">LanguageTag</a>&gt;</code>

**Since:** 4.0.0

--------------------


### Interfaces


Expand Down Expand Up @@ -147,6 +163,13 @@ Get the device's current language locale code.
| **`value`** | <code>string</code> | Two character language code. | 1.0.0 |


#### LanguageTag

| Prop | Type | Description | Since |
| ----------- | ------------------- | ----------------------------------------------- | ----- |
| **`value`** | <code>string</code> | Returns a well-formed IETF BCP 47 language tag. | 4.0.0 |


### Type Aliases


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ public void getLanguageCode(PluginCall call) {
ret.put("value", Locale.getDefault().getLanguage());
call.resolve(ret);
}

@PluginMethod
public void getLanguageTag(PluginCall call) {
JSObject ret = new JSObject();
ret.put("value", Locale.getDefault().toLanguageTag());
call.resolve(ret);
}
}
4 changes: 4 additions & 0 deletions device/ios/Plugin/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ import Foundation
public func getLanguageCode() -> String {
return String(Locale.preferredLanguages[0].prefix(2))
}

public func getLanguageTag() -> String {
return String(Locale.preferredLanguages[0])
}
}
1 change: 1 addition & 0 deletions device/ios/Plugin/DevicePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
CAP_PLUGIN_METHOD(getInfo, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getBatteryInfo, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getLanguageCode, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getLanguageTag, CAPPluginReturnPromise);
)
7 changes: 7 additions & 0 deletions device/ios/Plugin/DevicePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,11 @@ public class DevicePlugin: CAPPlugin {
])
}

@objc func getLanguageTag(_ call: CAPPluginCall) {
let tag = implementation.getLanguageTag()
call.resolve([
"value": tag
])
}

}
16 changes: 16 additions & 0 deletions device/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ export interface GetLanguageCodeResult {
value: string;
}

export interface LanguageTag {
/**
* Returns a well-formed IETF BCP 47 language tag.
*
* @since 4.0.0
*/
value: string;
}

export interface DevicePlugin {
/**
* Return an unique identifier for the device.
Expand Down Expand Up @@ -171,6 +180,13 @@ export interface DevicePlugin {
* @since 1.0.0
*/
getLanguageCode(): Promise<GetLanguageCodeResult>;

/**
* Get the device's current language locale tag.
*
* @since 4.0.0
*/
getLanguageTag(): Promise<LanguageTag>;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions device/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
DeviceInfo,
DevicePlugin,
GetLanguageCodeResult,
LanguageTag,
} from './definitions';

declare global {
Expand Down Expand Up @@ -71,6 +72,12 @@ export class DeviceWeb extends WebPlugin implements DevicePlugin {
};
}

async getLanguageTag(): Promise<LanguageTag> {
return {
value: navigator.language,
};
}

parseUa(ua: string): any {
const uaFields: any = {};
const start = ua.indexOf('(') + 1;
Expand Down