Skip to content

Commit c1cb2d4

Browse files
committed
feat(app): Add 'getAppLanguageCode' and 'getAppLanguageTag'
1 parent 56512c9 commit c1cb2d4

File tree

5 files changed

+86
-1
lines changed

5 files changed

+86
-1
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import com.getcapacitor.annotation.CapacitorPlugin;
1515
import com.getcapacitor.util.InternalUtils;
1616

17+
import java.util.Locale;
18+
1719
@CapacitorPlugin(name = "App")
1820
public class AppPlugin extends Plugin {
1921

@@ -115,6 +117,20 @@ public void minimizeApp(PluginCall call) {
115117
call.resolve();
116118
}
117119

120+
@PluginMethod
121+
public void getAppLanguageCode(PluginCall call) {
122+
JSObject ret = new JSObject();
123+
ret.put("value", Locale.getDefault().getLanguage());
124+
call.resolve(ret);
125+
}
126+
127+
@PluginMethod
128+
public void getAppLanguageTag(PluginCall call) {
129+
JSObject ret = new JSObject();
130+
ret.put("value", Locale.getDefault().toLanguageTag());
131+
call.resolve(ret);
132+
}
133+
118134
/**
119135
* Handle ACTION_VIEW intents to store a URL that was used to open the app
120136
* @param intent

app/ios/Plugin/AppPlugin.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010
CAP_PLUGIN_METHOD(getState, CAPPluginReturnPromise);
1111
CAP_PLUGIN_METHOD(minimizeApp, CAPPluginReturnPromise);
1212
CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnPromise);
13+
CAP_PLUGIN_METHOD(getAppLanguageCode, CAPPluginReturnPromise);
14+
CAP_PLUGIN_METHOD(getAppLanguageTag, CAPPluginReturnPromise);
1315
)

app/ios/Plugin/AppPlugin.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,27 @@ public class AppPlugin: CAPPlugin {
104104
@objc func minimizeApp(_ call: CAPPluginCall) {
105105
call.unimplemented()
106106
}
107+
108+
@objc func getAppLanguageCode(_ call: CAPPluginCall) {
109+
110+
// According to https://developer.apple.com/news/?id=u2cfuj88, the current
111+
// app language is returned by 'Bundle.main.preferredLocalizations.first'.
112+
// Fallback to device language if app language is not defined.
113+
114+
let appLanguageWithFallbackToDeviceLanguage =
115+
Bundle.main.preferredLocalizations.first ?? Locale.preferredLanguages[0]
116+
let code = String(appLanguageWithFallbackToDeviceLanguage.prefix(2))
117+
118+
call.resolve([
119+
"value": code
120+
])
121+
}
122+
123+
@objc func getAppLanguageTag(_ call: CAPPluginCall) {
124+
let tag = Bundle.main.preferredLocalizations.first ?? Locale.preferredLanguages[0]
125+
126+
call.resolve([
127+
"value": tag
128+
])
129+
}
107130
}

app/src/definitions.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,24 @@ export type URLOpenListener = (event: URLOpenListenerEvent) => void;
130130
export type RestoredListener = (event: RestoredListenerEvent) => void;
131131
export type BackButtonListener = (event: BackButtonListenerEvent) => void;
132132

133+
export interface GetLanguageCodeResult {
134+
/**
135+
* Two character language code.
136+
*
137+
* @since 5.1.0
138+
*/
139+
value: string;
140+
}
141+
142+
export interface LanguageTag {
143+
/**
144+
* Returns a well-formed IETF BCP 47 language tag.
145+
*
146+
* @since 5.1.0
147+
*/
148+
value: string;
149+
}
150+
133151
export interface AppPlugin {
134152
/**
135153
* Force exit the app. This should only be used in conjunction with the `backButton` handler for Android to
@@ -171,6 +189,20 @@ export interface AppPlugin {
171189
*/
172190
minimizeApp(): Promise<void>;
173191

192+
/**
193+
* Get the app specific language locale code.
194+
*
195+
* @since 5.1.0
196+
*/
197+
getAppLanguageCode(): Promise<GetLanguageCodeResult>;
198+
199+
/**
200+
* Get the app specific language locale tag.
201+
*
202+
* @since 5.1.0
203+
*/
204+
getAppLanguageTag(): Promise<LanguageTag>;
205+
174206
/**
175207
* Listen for changes in the app or the activity states.
176208
*

app/src/web.ts

Lines changed: 13 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, GetLanguageCodeResult, LanguageTag } from './definitions';
44

55
export class AppWeb extends WebPlugin implements AppPlugin {
66
constructor() {
@@ -44,4 +44,16 @@ export class AppWeb extends WebPlugin implements AppPlugin {
4444
this.notifyListeners('resume', null);
4545
}
4646
};
47+
48+
async getAppLanguageCode(): Promise<GetLanguageCodeResult> {
49+
return {
50+
value: navigator.language.split('-')[0].toLowerCase(),
51+
};
52+
}
53+
54+
async getAppLanguageTag(): Promise<LanguageTag> {
55+
return {
56+
value: navigator.language,
57+
};
58+
}
4759
}

0 commit comments

Comments
 (0)