Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
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
46 changes: 46 additions & 0 deletions app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ const checkAppLaunchUrl = async () => {
* [`getState()`](#getstate)
* [`getLaunchUrl()`](#getlaunchurl)
* [`minimizeApp()`](#minimizeapp)
* [`getAppLanguageCode()`](#getapplanguagecode)
* [`getAppLanguageTag()`](#getapplanguagetag)
* [`addListener('appStateChange', ...)`](#addlistenerappstatechange)
* [`addListener('pause', ...)`](#addlistenerpause)
* [`addListener('resume', ...)`](#addlistenerresume)
Expand Down Expand Up @@ -167,6 +169,36 @@ Only available for Android.
--------------------


### getAppLanguageCode()

```typescript
getAppLanguageCode() => Promise<AppLanguageCode>
```

Get the app specific language locale code.

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

**Since:** 5.1.0

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


### getAppLanguageTag()

```typescript
getAppLanguageTag() => Promise<AppLanguageTag>
```

Get the app specific language locale tag.

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

**Since:** 5.1.0

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


### addListener('appStateChange', ...)

```typescript
Expand Down Expand Up @@ -364,6 +396,20 @@ Remove all native listeners for this plugin
| **`url`** | <code>string</code> | The url used to open the app. | 1.0.0 |


#### AppLanguageCode

| Prop | Type | Description | Since |
| ----------- | ------------------- | ---------------------------- | ----- |
| **`value`** | <code>string</code> | Two character language code. | 5.1.0 |


#### AppLanguageTag

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


#### PluginListenerHandle

| Prop | Type |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.getcapacitor.util.InternalUtils;
import java.util.Locale;

@CapacitorPlugin(name = "App")
public class AppPlugin extends Plugin {
Expand Down Expand Up @@ -115,6 +116,20 @@ public void minimizeApp(PluginCall call) {
call.resolve();
}

@PluginMethod
public void getAppLanguageCode(PluginCall call) {
JSObject ret = new JSObject();
ret.put("value", Locale.getDefault().getLanguage());
call.resolve(ret);
}

@PluginMethod
public void getAppLanguageTag(PluginCall call) {
JSObject ret = new JSObject();
ret.put("value", Locale.getDefault().toLanguageTag());
call.resolve(ret);
}

/**
* Handle ACTION_VIEW intents to store a URL that was used to open the app
* @param intent
Expand Down
2 changes: 2 additions & 0 deletions app/ios/Plugin/AppPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
CAP_PLUGIN_METHOD(getState, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(minimizeApp, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getAppLanguageCode, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getAppLanguageTag, CAPPluginReturnPromise);
)
23 changes: 23 additions & 0 deletions app/ios/Plugin/AppPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,27 @@ public class AppPlugin: CAPPlugin {
@objc func minimizeApp(_ call: CAPPluginCall) {
call.unimplemented()
}

@objc func getAppLanguageCode(_ call: CAPPluginCall) {

// According to https://developer.apple.com/news/?id=u2cfuj88, the current
// app language is returned by 'Bundle.main.preferredLocalizations.first'.
// Fallback to device language if app language is not defined.

let appLanguageWithFallbackToDeviceLanguage =
Bundle.main.preferredLocalizations.first ?? Locale.preferredLanguages[0]
let code = String(appLanguageWithFallbackToDeviceLanguage.prefix(2))

call.resolve([
"value": code
])
}

@objc func getAppLanguageTag(_ call: CAPPluginCall) {
let tag = Bundle.main.preferredLocalizations.first ?? Locale.preferredLanguages[0]

call.resolve([
"value": tag
])
}
}
32 changes: 32 additions & 0 deletions app/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ export type URLOpenListener = (event: URLOpenListenerEvent) => void;
export type RestoredListener = (event: RestoredListenerEvent) => void;
export type BackButtonListener = (event: BackButtonListenerEvent) => void;

export interface AppLanguageCode {
/**
* Two character language code.
*
* @since 5.1.0
*/
value: string;
}

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

export interface AppPlugin {
/**
* Force exit the app. This should only be used in conjunction with the `backButton` handler for Android to
Expand Down Expand Up @@ -171,6 +189,20 @@ export interface AppPlugin {
*/
minimizeApp(): Promise<void>;

/**
* Get the app specific language locale code.
*
* @since 5.1.0
*/
getAppLanguageCode(): Promise<AppLanguageCode>;

/**
* Get the app specific language locale tag.
*
* @since 5.1.0
*/
getAppLanguageTag(): Promise<AppLanguageTag>;

/**
* Listen for changes in the app or the activity states.
*
Expand Down
21 changes: 20 additions & 1 deletion app/src/web.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { WebPlugin } from '@capacitor/core';

import type { AppInfo, AppPlugin, AppLaunchUrl, AppState } from './definitions';
import type {
AppInfo,
AppPlugin,
AppLaunchUrl,
AppState,
AppLanguageCode,
AppLanguageTag,
} from './definitions';

export class AppWeb extends WebPlugin implements AppPlugin {
constructor() {
Expand Down Expand Up @@ -44,4 +51,16 @@ export class AppWeb extends WebPlugin implements AppPlugin {
this.notifyListeners('resume', null);
}
};

async getAppLanguageCode(): Promise<AppLanguageCode> {
return {
value: navigator.language.split('-')[0].toLowerCase(),
};
}

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