[Solved] WRITER Language of a paragraph : get / set

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
nunof32
Posts: 59
Joined: Sat Jul 28, 2012 9:52 am

[Solved] WRITER Language of a paragraph : get / set

Post by nunof32 »

Hello,

How do I get() / set() the Language for a text range ?

Thanks in advance.
Last edited by nunof32 on Tue Jan 12, 2021 12:08 am, edited 1 time in total.
OpenOffice 4.1.10 with MacOS X 10.13.6
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: WRITER Language of a paragraph : get / set

Post by Villeroy »

Use paragraph styles.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
nunof32
Posts: 59
Joined: Sat Jul 28, 2012 9:52 am

Re: WRITER Language of a paragraph : get / set

Post by nunof32 »

Sorry, I was looking for a BASIC construct that allows me, in a BASIC macro, to get and set the language of some text range.
OpenOffice 4.1.10 with MacOS X 10.13.6
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: WRITER Language of a paragraph : get / set

Post by RoryOF »

See http://www.pitonyak.org/AndrewMacro.odt, section 10.7.3 Manipulating the Options dialog. Apparently this is not simple to do, but possible if one is determined.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: WRITER Language of a paragraph : get / set

Post by Villeroy »

Using templates and styles makes sense in any context. A single statement to apply a whole set of attributes:

Code: Select all

objParagraph.ParaStyleName = "My Spanish Text Body"
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: WRITER Language of a paragraph : get / set

Post by JeJe »

Macro recorder seems to work for this

Code: Select all

REM  *****  BASIC  *****


sub Main
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")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Language"
args1(0).Value = "Paragraph_Danish"

dispatcher.executeDispatch(document, ".uno:LanguageStatus", "", 0, args1())
end sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
nunof32
Posts: 59
Joined: Sat Jul 28, 2012 9:52 am

Re: WRITER Language of a paragraph : get / set

Post by nunof32 »

Many thanks to all who have helped.
OpenOffice 4.1.10 with MacOS X 10.13.6
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [Solved] WRITER Language of a paragraph : get / set

Post by Lupp »

@nunof32:
It seems you marked the thread SOLVED, but I cannot see in what way the question actually was answered.
The underlying inconsistency existed already in the way that you used "Language of a paragraph" in the subject, while the question itself talked of a "text range". That are different things. What should prevail?

@jeje:
You didn't tell, but I suppose the action you recorded was >Format>Character>>Select_a_Locale_at_"Language" (or equivalent).
This isn't offered under >Format>Paragraph because paragraphs not have a respective property.
Nonetheless the setting of the character property is applied to all the text of the paragraph by the recorded macro. Kind of a mess?
How would the questioner get the current Locale setting for his range (which may cover one or more paragraphs)?

To get it clear: A paragraph Style also contains its own default character settings listed under three tabs. Among the Font settings there also can be found the "Language" field where it's actually a Locale what you can choose. Once again: these settings neither need to be uniform for a paragraph, nor are they listed in a formatting dialog for a specific paragraph instance having assigned that style, nor are they applied to existing text of the paragraph if the style is re-assigned - not even if all the text is selected.
Kind of a mess again? Misleading anyway, I would think.

Actually the Locale of a text range (if uniform) is only represented by a character property .CharLocale for the API. ...And this property is "Empty" by Typename if it is requested for a paragraph (hair cursor immediately behind a paragraph pilcrow).
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] WRITER Language of a paragraph : get / set

Post by JeJe »

My recording was from OO Tools Menu/Language/For Paragraph.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
nunof32
Posts: 59
Joined: Sat Jul 28, 2012 9:52 am

Re: [Solved] WRITER Language of a paragraph : get / set

Post by nunof32 »

@Lupp

Thanks for your contribution, it was most useful. Much appreciated.

I used 'Paragraph' instead of 'Text Range' in the title of the post because I think a user looking for information on this topic is more likely to use 'Paragraph' in his search than 'Text range'. But you are right, those are different things. May be I should have used 'Text range' in the title of the post since that is what I was actually looking for.

When I ask a question on this forum and start receiving help that is completely off target I always promptly mark it SOLVED and thank all the folks who have contributed ideas. My assumption is always that if I am not receiving valuable help it is because my question was stupid, I am too dumb to understand the answers, or both. I don't want to continue bothering the regular 'solvers' of the forum (may them be thanked again for their admirable commitment to help) with a stupid question.

Meanwhile, I have found myself a way to get() / set() the 'Language' attribute of a text range.
Suppose your text range is a paragraph 'portion' that you have obtained by creating an enumeration of a paragraph's 'portions'.
To get its 'Language' :

Code: Select all

msgbox (paraPortion.CharLocale.Language)
REM paraPortion is the variable to which you assign the elements of the enumeration
REM The Language field of the Locale structure is a string, see com.sun.star.lang.locale
You get "en", "fr", "it", "nl" whatever the Language attribute of the range is (Note : the Language attribute of a range has nothing to do with the language in which the text is written)

To set its 'Language' was more difficult.

Code: Select all

paraPortion.CharLocale.Language = "en"
did not work. If the original Language was "fr" it stayed "fr", not "en", after this statement was executed. I still have to understand why this is, suspect other formatting attributes take precedence over the direct formatting of the 'portion' by the macro.

The trick I used is to first define a Character Style that has English as its Language attribute. Call it 'myCharStyleEn'
Then you assign this style to your text range :

Code: Select all

paraPortion.CharStyleName = "myCharStyleEn"
Works like a charm.

Probably not a very elegant, or even fully technically correct, solution, but it gets the job done. I leave it up to the gurus to come up with another one.


[In case you are wondering : I needed to do all this to amend a bunch of files created in MS WORD and then save them as OpenOffice/Writer documents. For some reason, those MS WORD files had some paragraphs where different Language attributes had been used all along (do not ask, I do not know why). I did try to assign to the whole document a Paragraph Style that had English has its Language attribute but that had no effect on the Language attribute of the paragraph portions that had another Language assigned to them.]



Thanks again for your help.
OpenOffice 4.1.10 with MacOS X 10.13.6
User avatar
Hagar Delest
Moderator
Posts: 32628
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Solved] WRITER Language of a paragraph : get / set

Post by Hagar Delest »

For the record, I have the same kind of situation to deal with and I also use the CharStyleName setting with a custom character style.
nunof32 wrote:[In case you are wondering : I needed to do all this to amend a bunch of files created in MS WORD and then save them as OpenOffice/Writer documents. For some reason, those MS WORD files had some paragraphs where different Language attributes had been used all along (do not ask, I do not know why). I did try to assign to the whole document a Paragraph Style that had English has its Language attribute but that had no effect on the Language attribute of the paragraph portions that had another Language assigned to them.]
I'm in a similar situation where I paste text from another application, carrying the language as a direct formatting. I've found that I've to reset the language to its default properties with that piece of code:

Code: Select all

Sub ResetLanguage
dim document   as object
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 = "Current_RESET_LANGUAGES"
dispatcher.executeDispatch(document, ".uno:LanguageStatus", "", 0, args1())
End Sub
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
Post Reply