Skip to content

Commit 698600a

Browse files
philetjcesarmobileIT-MikeS
authored
feat(app): Add getAppLanguage (#1694)
Co-authored-by: jcesarmobile <jcesarmobile@gmail.com> Co-authored-by: IT-MikeS <20338451+IT-MikeS@users.noreply.github.com>
1 parent 34c17a5 commit 698600a

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

app/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export default config;
119119
* [`getState()`](#getstate)
120120
* [`getLaunchUrl()`](#getlaunchurl)
121121
* [`minimizeApp()`](#minimizeapp)
122+
* [`getAppLanguage()`](#getapplanguage)
122123
* [`toggleBackButtonHandler(...)`](#togglebackbuttonhandler)
123124
* [`addListener('appStateChange', ...)`](#addlistenerappstatechange-)
124125
* [`addListener('pause', ...)`](#addlistenerpause-)
@@ -211,6 +212,21 @@ Only available for Android.
211212
--------------------
212213

213214

215+
### getAppLanguage()
216+
217+
```typescript
218+
getAppLanguage() => Promise<AppLanguageCode>
219+
```
220+
221+
Get the app specific language locale code.
222+
223+
**Returns:** <code>Promise&lt;<a href="#applanguagecode">AppLanguageCode</a>&gt;</code>
224+
225+
**Since:** 8.1.0
226+
227+
--------------------
228+
229+
214230
### toggleBackButtonHandler(...)
215231

216232
```typescript
@@ -427,6 +443,13 @@ Remove all native listeners for this plugin
427443
| **`url`** | <code>string</code> | The url used to open the app. | 1.0.0 |
428444

429445

446+
#### AppLanguageCode
447+
448+
| Prop | Type | Description | Since |
449+
| ----------- | ------------------- | ------------------------------------- | ----- |
450+
| **`value`** | <code>string</code> | Two or Three character language code. | 8.1.0 |
451+
452+
430453
#### ToggleBackButtonHandlerOptions
431454

432455
| Prop | Type | Description | Since |

app/android/src/main/java/com/capacitorjs/plugins/app/AppPlugin.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
import android.content.pm.PackageInfo;
66
import android.net.Uri;
77
import androidx.activity.OnBackPressedCallback;
8+
import androidx.appcompat.app.AppCompatDelegate;
89
import androidx.core.content.pm.PackageInfoCompat;
10+
import androidx.core.os.LocaleListCompat;
911
import com.getcapacitor.JSObject;
1012
import com.getcapacitor.Logger;
1113
import com.getcapacitor.Plugin;
1214
import com.getcapacitor.PluginCall;
1315
import com.getcapacitor.PluginMethod;
1416
import com.getcapacitor.annotation.CapacitorPlugin;
1517
import com.getcapacitor.util.InternalUtils;
18+
import java.util.Locale;
1619

1720
@CapacitorPlugin(name = "App")
1821
public class AppPlugin extends Plugin {
@@ -126,6 +129,15 @@ public void toggleBackButtonHandler(PluginCall call) {
126129
call.resolve();
127130
}
128131

132+
@PluginMethod
133+
public void getAppLanguage(PluginCall call) {
134+
JSObject ret = new JSObject();
135+
LocaleListCompat appLocales = AppCompatDelegate.getApplicationLocales();
136+
Locale appLocale = !appLocales.isEmpty() ? appLocales.get(0) : null;
137+
ret.put("value", appLocale != null ? appLocale.getLanguage() : Locale.getDefault().getLanguage());
138+
call.resolve(ret);
139+
}
140+
129141
/**
130142
* Handle ACTION_VIEW intents to store a URL that was used to open the app
131143
* @param intent

app/ios/Sources/AppPlugin/AppPlugin.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class AppPlugin: CAPPlugin, CAPBridgedPlugin {
88
public let pluginMethods: [CAPPluginMethod] = [
99
CAPPluginMethod(name: "exitApp", returnType: CAPPluginReturnPromise),
1010
CAPPluginMethod(name: "getInfo", returnType: CAPPluginReturnPromise),
11+
CAPPluginMethod(name: "getAppLanguage", returnType: CAPPluginReturnPromise),
1112
CAPPluginMethod(name: "getLaunchUrl", returnType: CAPPluginReturnPromise),
1213
CAPPluginMethod(name: "getState", returnType: CAPPluginReturnPromise),
1314
CAPPluginMethod(name: "minimizeApp", returnType: CAPPluginReturnPromise),
@@ -115,6 +116,12 @@ public class AppPlugin: CAPPlugin, CAPBridgedPlugin {
115116
call.unimplemented()
116117
}
117118

119+
@objc func getAppLanguage(_ call: CAPPluginCall) {
120+
call.resolve([
121+
"value": Bundle.main.preferredLocalizations.first
122+
])
123+
}
124+
118125
@objc func toggleBackButtonHandler(_ call: CAPPluginCall) {
119126
call.unimplemented()
120127
}

app/src/definitions.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ export type URLOpenListener = (event: URLOpenListenerEvent) => void;
158158
export type RestoredListener = (event: RestoredListenerEvent) => void;
159159
export type BackButtonListener = (event: BackButtonListenerEvent) => void;
160160

161+
export interface AppLanguageCode {
162+
/**
163+
* Two or Three character language code.
164+
*
165+
* @since 8.1.0
166+
*/
167+
value: string;
168+
}
169+
161170
export interface AppPlugin {
162171
/**
163172
* 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 {
199208
*/
200209
minimizeApp(): Promise<void>;
201210

211+
/**
212+
* Get the app specific language locale code.
213+
*
214+
* @since 8.1.0
215+
*/
216+
getAppLanguage(): Promise<AppLanguageCode>;
217+
202218
/**
203219
* Enables or disables the plugin's back button handling during runtime.
204220
*

app/src/web.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { WebPlugin } from '@capacitor/core';
22

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

55
export class AppWeb extends WebPlugin implements AppPlugin {
66
constructor() {
@@ -44,4 +44,10 @@ export class AppWeb extends WebPlugin implements AppPlugin {
4444
this.notifyListeners('resume', null);
4545
}
4646
};
47+
48+
async getAppLanguage(): Promise<AppLanguageCode> {
49+
return {
50+
value: navigator.language.split('-')[0].toLowerCase(),
51+
};
52+
}
4753
}

0 commit comments

Comments
 (0)