How to make LibreOffice Writer change keyboard language automatically to match surrounding text?

There is an option in MS Word’s Advanced options, “Automatically switch keyboard to match language of surrounding text” that does just this. By selecting it, one does not have to manually change keyboard language via Alt+Shift. It is very handy for those who switch between two languages a lot, especially if one of them is CTL (Persian in my case). For more information on what I mean, see Is there an option in LibreOffice Writer to enable this feature so I don’t have to manually change keyboard language frequently?

No, but it would be a good feature. Consider filing an enhancement request. Automatic keyboard switching is possible to implement cross-platform, for example SIL FieldWorks.

To others who know more about Ask LO than I do – this question was already asked. Should this be flagged as a duplicate?

Looks like the question has been answered over there: no. Given that LibreOffice is designed to be cross-platform, I wouldn’t expect such a feature to exist.

@khanson679 why not?

Because it’s complex (language and keyboard are not a one-to-one mapping, etc.) and has to be done differently on every operating system. But maybe I’m just being pessimistic :wink:

You may also want to read this older thread on the question which was unnecessarily closed.

As there isn’t such a feature, next question may be if it can be established based on user code.
There was a thread recently (concerning localized dates) to which I contributed the following piece of user code in BASIC:

Function getCurrentCharLocale(pDoc)
theCtrl = pDoc.CurrentController
theVC   = theCtrl.GetViewCursor
theLoc  = theVC.CharLocale
REM If the current selection is not unambiguous regarding the Locale then
REM TypeName(theLoc) = "Empty".
REM Otherwise a CharLocale has three attributes: .Language .Country .Variant
REM A collapsed ViewCursor ("Caret") should always return non-empty CharLocale except
REM it was actually set empty (no language).
getCurrentCharLocale = theLoc  
End Function  

The .CharLocale property is a structure containing the properties .Language .Country and .Variant.
Based on this info you will need to call a script for changing the keyboard settings. How to do so will depend on the Operating System - if possible at all. It’s not my field.

How would getCurrentCharLocale be called – based on some kind of Writer event, perhaps whenever the view cursor gets moved? It should be automatic.