Skip to content
Merged
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 @@ -119,6 +119,7 @@ export default config;
* [`getState()`](#getstate)
* [`getLaunchUrl()`](#getlaunchurl)
* [`minimizeApp()`](#minimizeapp)
* [`getAppLanguage()`](#getapplanguage)
* [`toggleBackButtonHandler(...)`](#togglebackbuttonhandler)
* [`addListener('appStateChange', ...)`](#addlistenerappstatechange-)
* [`addListener('pause', ...)`](#addlistenerpause-)
Expand Down Expand Up @@ -211,6 +212,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:** 8.1.0

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


### toggleBackButtonHandler(...)

```typescript
Expand Down Expand Up @@ -427,6 +443,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. | 8.1.0 |


#### ToggleBackButtonHandlerOptions

| Prop | Type | Description | Since |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
import android.content.pm.PackageInfo;
import android.net.Uri;
import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.content.pm.PackageInfoCompat;
import androidx.core.os.LocaleListCompat;
import com.getcapacitor.JSObject;
import com.getcapacitor.Logger;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
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 @@ -126,6 +129,15 @@ public void toggleBackButtonHandler(PluginCall call) {
call.resolve();
}

@PluginMethod
public void getAppLanguage(PluginCall call) {
JSObject ret = new JSObject();
LocaleListCompat appLocales = AppCompatDelegate.getApplicationLocales();
Locale appLocale = !appLocales.isEmpty() ? appLocales.get(0) : null;
ret.put("value", appLocale != null ? appLocale.getLanguage() : 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
7 changes: 7 additions & 0 deletions app/ios/Sources/AppPlugin/AppPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class AppPlugin: CAPPlugin, CAPBridgedPlugin {
public let pluginMethods: [CAPPluginMethod] = [
CAPPluginMethod(name: "exitApp", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "getInfo", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "getAppLanguage", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "getLaunchUrl", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "getState", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "minimizeApp", returnType: CAPPluginReturnPromise),
Expand Down Expand Up @@ -115,6 +116,12 @@ public class AppPlugin: CAPPlugin, CAPBridgedPlugin {
call.unimplemented()
}

@objc func getAppLanguage(_ call: CAPPluginCall) {
call.resolve([
"value": Bundle.main.preferredLocalizations.first
])
}

@objc func toggleBackButtonHandler(_ call: CAPPluginCall) {
call.unimplemented()
}
Expand Down
16 changes: 16 additions & 0 deletions app/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,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 8.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 @@ -199,6 +208,13 @@ export interface AppPlugin {
*/
minimizeApp(): Promise<void>;

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

/**
* Enables or disables the plugin's back button handling during runtime.
*
Expand Down
8 changes: 7 additions & 1 deletion app/src/web.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 +44,10 @@ export class AppWeb extends WebPlugin implements AppPlugin {
this.notifyListeners('resume', null);
}
};

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