diff --git a/app/README.md b/app/README.md index a90390bf3..d7ef3bb4f 100644 --- a/app/README.md +++ b/app/README.md @@ -119,6 +119,7 @@ export default config; * [`getState()`](#getstate) * [`getLaunchUrl()`](#getlaunchurl) * [`minimizeApp()`](#minimizeapp) +* [`getAppLanguage()`](#getapplanguage) * [`toggleBackButtonHandler(...)`](#togglebackbuttonhandler) * [`addListener('appStateChange', ...)`](#addlistenerappstatechange-) * [`addListener('pause', ...)`](#addlistenerpause-) @@ -211,6 +212,21 @@ Only available for Android. -------------------- +### getAppLanguage() + +```typescript +getAppLanguage() => Promise +``` + +Get the app specific language locale code. + +**Returns:** Promise<AppLanguageCode> + +**Since:** 8.1.0 + +-------------------- + + ### toggleBackButtonHandler(...) ```typescript @@ -427,6 +443,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. | 8.1.0 | + + #### ToggleBackButtonHandlerOptions | Prop | Type | Description | Since | 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 c0edf2d4c..f14286f26 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 @@ -5,7 +5,9 @@ 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; @@ -13,6 +15,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 { @@ -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 diff --git a/app/ios/Sources/AppPlugin/AppPlugin.swift b/app/ios/Sources/AppPlugin/AppPlugin.swift index a6c4451fd..c93b84a6d 100644 --- a/app/ios/Sources/AppPlugin/AppPlugin.swift +++ b/app/ios/Sources/AppPlugin/AppPlugin.swift @@ -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), @@ -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() } diff --git a/app/src/definitions.ts b/app/src/definitions.ts index 3c7249546..1029be2ec 100644 --- a/app/src/definitions.ts +++ b/app/src/definitions.ts @@ -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 @@ -199,6 +208,13 @@ export interface AppPlugin { */ minimizeApp(): Promise; + /** + * Get the app specific language locale code. + * + * @since 8.1.0 + */ + getAppLanguage(): Promise; + /** * Enables or disables the plugin's back button handling during runtime. * diff --git a/app/src/web.ts b/app/src/web.ts index db4ac5f02..be23cc767 100644 --- a/app/src/web.ts +++ b/app/src/web.ts @@ -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() { @@ -44,4 +44,10 @@ export class AppWeb extends WebPlugin implements AppPlugin { this.notifyListeners('resume', null); } }; + + async getAppLanguage(): Promise { + return { + value: navigator.language.split('-')[0].toLowerCase(), + }; + } }