1

I need to detect the keyboard language not the locale or device language or anything else. and i can't write java code. is there any way to find the language of the keyboard ?

in this case it should show 'en' or 'ltr'

in this case it must show en or ltr

in this case it should show 'fa' or 'rtl'

and in this case it must say fa or rtl

i am using i18n but i don't need the locale, its fixed and i can't get the info i need from it.

react-native : 0.59.5

Gray
  • 531
  • 1
  • 10
  • 28

1 Answers1

1

You can get the keyboard language if you create an native module.

Here's a example of how to get it using java for android:

private void printInputLanguages() {
   InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
   List<InputMethodInfo> ims = imm.getEnabledInputMethodList();

   for (InputMethodInfo method : ims) {
       List<InputMethodSubtype> submethods = imm.getEnabledInputMethodSubtypeList(method, true);
       for (InputMethodSubtype submethod : submethods) {
          if (submethod.getMode().equals("keyboard")) {
             String currentLocale = submethod.getLocale();
             Log.i(TAG, "Available input method locale: " + currentLocale);
          }
       }
   }
}

And here's how to do it with swift, for IOS:

var language = textfield.textInputMode?.primaryLanguage

And here's how to create a native module for react-native

[EDIT]

You might get what you need using the NativeModules module from react-native itself, like as follows:

import { NativeModules } from 'react-native'

[...]

//Getting the device language:
// iOS
let deviceLanguage = NativeModules.SettingsManager.settings.AppleLocale || NativeModules.SettingsManager.settings.AppleLanguages[0]

// Android
let deviceLanguage = NativeModules.I18nManager.localeIdentifier

//Getting keyboard Information (1)
// iOS:
let keyboardData = NativeModules.SettingsManager.settings

// Android:
let keyboardData = NativeModules.I18nManager

(1): I did not test this, you will need to see the output and see if it helps in your case. But, the output for IOS should look like this:

{
    [...]

    AppleKeyboards: [
        "es_419@sw=QWERTY-Spanish;hw=Automatic",
        "en_US@sw=QWERTY;hw=Automatic",
        "fr_FR@hw=US;sw=QWERTY",
        "emoji@sw=Emoji"
    ]
    AppleLanguages: ["fr-US", "en-US", "es-US", "en"],
    NSLanguages: ["fr-US", "en-US", "es-US", "en"],
    AppleLanguagesDidMigrate: "9.2",
    AppleLocale: "fr_FR"
    
   [...]
}

Got this answer here.