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


### getAppLanguage()

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

Get the app specific language locale code.

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

**Since:** 6.0.0

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


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

```typescript
Expand Down Expand Up @@ -364,6 +380,13 @@ 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 or Three character language code. | 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,13 @@ public void minimizeApp(PluginCall call) {
call.resolve();
}

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

/**
* Handle ACTION_VIEW intents to store a URL that was used to open the app
* @param intent
Expand Down
6 changes: 6 additions & 0 deletions app/ios/Sources/AppPlugin/AppPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,10 @@ public class AppPlugin: CAPPlugin, CAPBridgedPlugin {
@objc func minimizeApp(_ call: CAPPluginCall) {
call.unimplemented()
}

@objc func getAppLanguage(_ call: CAPPluginCall) {
call.resolve([
"value": Bundle.main.preferredLocalizations.first
])
}
}
16 changes: 16 additions & 0 deletions app/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ export type URLOpenListener = (event: URLOpenListenerEvent) => void;
export type RestoredListener = (event: RestoredListenerEvent) => void;
export type BackButtonListener = (event: BackButtonListenerEvent) => void;

export interface AppLanguageCode {
/**
* Two or Three character language code.
*
* @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 +180,13 @@ export interface AppPlugin {
*/
minimizeApp(): Promise<void>;

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

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

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

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

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