Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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 @@ -115,6 +117,20 @@ public void minimizeApp(PluginCall call) {
call.resolve();
}

@PluginMethod
public void getAppLanguageCode(PluginCall call) {
JSObject ret = new JSObject();
ret.put("value", Locale.getDefault().getLanguage());
call.resolve(ret);
}

@PluginMethod
public void getAppLanguageTag(PluginCall call) {
JSObject ret = new JSObject();
ret.put("value", Locale.getDefault().toLanguageTag());
call.resolve(ret);
}

/**
* Handle ACTION_VIEW intents to store a URL that was used to open the app
* @param intent
Expand Down
2 changes: 2 additions & 0 deletions app/ios/Plugin/AppPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
CAP_PLUGIN_METHOD(getState, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(minimizeApp, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getAppLanguageCode, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getAppLanguageTag, CAPPluginReturnPromise);
)
23 changes: 23 additions & 0 deletions app/ios/Plugin/AppPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,27 @@ public class AppPlugin: CAPPlugin {
@objc func minimizeApp(_ call: CAPPluginCall) {
call.unimplemented()
}

@objc func getAppLanguageCode(_ call: CAPPluginCall) {

// According to https://developer.apple.com/news/?id=u2cfuj88, the current
// app language is returned by 'Bundle.main.preferredLocalizations.first'.
// Fallback to device language if app language is not defined.

let appLanguageWithFallbackToDeviceLanguage =
Bundle.main.preferredLocalizations.first ?? Locale.preferredLanguages[0]
let code = String(appLanguageWithFallbackToDeviceLanguage.prefix(2))

call.resolve([
"value": code
])
}

@objc func getAppLanguageTag(_ call: CAPPluginCall) {
let tag = Bundle.main.preferredLocalizations.first ?? Locale.preferredLanguages[0]

call.resolve([
"value": tag
])
}
}
32 changes: 32 additions & 0 deletions app/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ export type URLOpenListener = (event: URLOpenListenerEvent) => void;
export type RestoredListener = (event: RestoredListenerEvent) => void;
export type BackButtonListener = (event: BackButtonListenerEvent) => void;

export interface GetLanguageCodeResult {
Comment thread
jcesarmobile marked this conversation as resolved.
Outdated
/**
* Two character language code.
*
* @since 5.1.0
*/
value: string;
}

export interface LanguageTag {
Comment thread
jcesarmobile marked this conversation as resolved.
Outdated
/**
* Returns a well-formed IETF BCP 47 language tag.
*
* @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
Expand Down Expand Up @@ -171,6 +189,20 @@ export interface AppPlugin {
*/
minimizeApp(): Promise<void>;

/**
* Get the app specific language locale code.
*
* @since 5.1.0
*/
getAppLanguageCode(): Promise<GetLanguageCodeResult>;

/**
* Get the app specific language locale tag.
*
* @since 5.1.0
*/
getAppLanguageTag(): Promise<LanguageTag>;

/**
* Listen for changes in the app or the activity states.
*
Expand Down
14 changes: 13 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, GetLanguageCodeResult, LanguageTag } from './definitions';

export class AppWeb extends WebPlugin implements AppPlugin {
constructor() {
Expand Down Expand Up @@ -44,4 +44,16 @@ export class AppWeb extends WebPlugin implements AppPlugin {
this.notifyListeners('resume', null);
}
};

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

async getAppLanguageTag(): Promise<LanguageTag> {
return {
value: navigator.language,
};
}
}