Skip to content

Commit

Permalink
feat(device): Add getLanguageTag function (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored May 2, 2022
1 parent 497f627 commit d268e4a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 0 deletions.
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 @@ -66,6 +66,10 @@ import Foundation
return String(Locale.preferredLanguages[0].prefix(2))
}

public func getLanguageTag() -> String {
return String(Locale.preferredLanguages[0])
}

public func getModelName() -> String {
var size = 0
sysctlbyname("hw.machine", nil, &size, nil, 0)
Expand Down
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 @@ -64,4 +64,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

0 comments on commit d268e4a

Please sign in to comment.