Skip to content

Get app specific language #1693

Open
Open
@philet

Description

@philet

Feature Request

Plugin

@capacitor/device

Description

We have an app that supports the three languages German, Italian and French (but not English):

Bildschirmfoto 2023-07-19 um 10 36 27

A user has set the order of preferred languages on his device to: English, Italian, German (see left screenshot below).

Therefore he would like to see our app in Italian (see app specific language settings in right screenshot below).

iOS language settings App specific language settings

On iOS @capacitor/device getLanguageCode() returns 'en' in this case. (On Android 'it' is returned.)

On iOS there is no other method on @capacitor/device which we can call to get the preferred language 'it'.

Sample project illustrating the problem:
https://github.com/philet/capacitor-app-language-example

Platform(s)

iOS
(Android, Web for API parity.)

Preferred Solution

Add a method 'getAppLanguageCode' to @capacitor/device.

The following method returns the correct application language code ('it' in this case):

iOS

    public func getAppLanguageCode() -> String {
      
        // 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]
        return String(appLanguageWithFallbackToDeviceLanguage.prefix(2))
    }

Android

On Android @capacitor/device getLanguageCode() already returns the app language. Therefore the 'getAppLanguageCode' implementation could just call 'getLanguageCode':

@PluginMethod
public void getAppLanguageCode(PluginCall call) {
    getLanguageCode(call);
}

Alternatives

Additional Context

In addition to adding getAppLanguageCode, the existing getLanguageTag method should probably also be supplemented by a new getAppLanguageTag method.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions