diff --git a/app/README.md b/app/README.md index a89622092..da8642faf 100644 --- a/app/README.md +++ b/app/README.md @@ -76,6 +76,7 @@ const checkAppLaunchUrl = async () => { * [`getState()`](#getstate) * [`getLaunchUrl()`](#getlaunchurl) * [`minimizeApp()`](#minimizeapp) +* [`getAppLanguage()`](#getapplanguage) * [`addListener('appStateChange', ...)`](#addlistenerappstatechange) * [`addListener('pause', ...)`](#addlistenerpause) * [`addListener('resume', ...)`](#addlistenerresume) @@ -167,6 +168,21 @@ Only available for Android. -------------------- +### getAppLanguage() + +```typescript +getAppLanguage() => Promise +``` + +Get the app specific language locale code. + +**Returns:** Promise<AppLanguageCode> + +**Since:** 6.0.0 + +-------------------- + + ### addListener('appStateChange', ...) ```typescript @@ -364,6 +380,13 @@ Remove all native listeners for this plugin | **`url`** | string | The url used to open the app. | 1.0.0 | +#### AppLanguageCode + +| Prop | Type | Description | Since | +| ----------- | ------------------- | ------------------------------------- | ----- | +| **`value`** | string | Two or Three character language code. | 5.1.0 | + + #### PluginListenerHandle | Prop | Type | diff --git a/app/android/src/main/java/com/capacitorjs/plugins/app/AppPlugin.java b/app/android/src/main/java/com/capacitorjs/plugins/app/AppPlugin.java index 916ad391a..8cabd2e85 100644 --- a/app/android/src/main/java/com/capacitorjs/plugins/app/AppPlugin.java +++ b/app/android/src/main/java/com/capacitorjs/plugins/app/AppPlugin.java @@ -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 { @@ -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 diff --git a/app/ios/Sources/AppPlugin/AppPlugin.swift b/app/ios/Sources/AppPlugin/AppPlugin.swift index 0f6086507..0097fb9cc 100644 --- a/app/ios/Sources/AppPlugin/AppPlugin.swift +++ b/app/ios/Sources/AppPlugin/AppPlugin.swift @@ -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 + ]) + } } diff --git a/app/src/definitions.ts b/app/src/definitions.ts index 072dda507..7eef8bdf2 100644 --- a/app/src/definitions.ts +++ b/app/src/definitions.ts @@ -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 @@ -171,6 +180,13 @@ export interface AppPlugin { */ minimizeApp(): Promise; + /** + * Get the app specific language locale code. + * + * @since 6.0.0 + */ + getAppLanguage(): Promise; + /** * Listen for changes in the app or the activity states. * diff --git a/app/src/web.ts b/app/src/web.ts index 1db1ff356..fe0aee860 100644 --- a/app/src/web.ts +++ b/app/src/web.ts @@ -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() { @@ -44,4 +50,10 @@ export class AppWeb extends WebPlugin implements AppPlugin { this.notifyListeners('resume', null); } }; + + async getAppLanguage(): Promise { + return { + value: navigator.language.split('-')[0].toLowerCase(), + }; + } }