How to change a cell language with a macro

I want to add a shortcut to quickly set a cell’s language, but when I record a macro to do this the actual language isn’t populated.

How can I find the available languages I can pass to .uno:Language

sub TurnJapanese
    rem ----------------------------------------------------------------------
    rem define variables
    dim document   as object
    dim dispatcher as object
    rem ----------------------------------------------------------------------
    rem get access to the document
    document   = ThisComponent.CurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
    dim args1(0) as new com.sun.star.beans.PropertyValue
    args1(0).Name = "Language"
    args1(0).Value = ???
    rem ----------------------------------------------------------------------
    dispatcher.executeDispatch(document, ".uno:Language", "", 0, Array())

end sub

Please explain what language of the cell is in question:

  • the language used for the spell checker
  • the language used to format the cell
  • And why you can’t use cell styles.

The language codes are ISO-639-1. List of ISO 639-1 codes - Wikipedia

The country codes are ISO-3166-1. ISO 3166-1 - Wikipedia

The variants are specific:

Historically:
https://www.openoffice.org/api/docs/common/ref/com/sun/star/lang/Locale.html#Language

Don’t use dispatcher for this directly unless you have a really good reason. LibreOffice is way easier than that. Here is a simple language getting/setting macro:

Sub CellLanguage()
	'Must have a cell object. This is not the same as a reference passed into a user function
	Dim Cell As Object
	'Must have a struct for this specific purpose
	Dim Locale As New com.sun.star.lang.Locale
	
	'For example, top left cell of first sheet
	Cell = ThisComponent.GetSheets().getByIndex(0).getCellByPosition(0,0)
	MsgBox Cell.CharLocale.Language
	
	'Now set this. This is like Format Cells...>Font (tab)>Language
	'	Not Format Cells...>Number>Language
	Locale.Country = "US" 'ISO 3166 (must be "" or valid, or Language may not set)
	Locale.Language = "en" 'ISO 639
	Locale.Variant = ""
	Cell.CharLocale = Locale
	
End Sub
1 Like

It would be better to link to the LibreOffice API reference because it may have more recent details, specifically in this case with the additional BCP 47 language tag semantics.

1 Like