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

Creating a macro - Writing a Script - Using the API

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

Postby nunof32 » Mon Jan 11, 2021 7:55 pm

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.8 with MacOS X 10.13.6
nunof32
 
Posts: 56
Joined: Sat Jul 28, 2012 9:52 am

Re: WRITER Language of a paragraph : get / set

Postby Villeroy » Mon Jan 11, 2021 8:09 pm

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, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 29259
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: WRITER Language of a paragraph : get / set

Postby nunof32 » Mon Jan 11, 2021 8:11 pm

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.8 with MacOS X 10.13.6
nunof32
 
Posts: 56
Joined: Sat Jul 28, 2012 9:52 am

Re: WRITER Language of a paragraph : get / set

Postby RoryOF » Mon Jan 11, 2021 8:11 pm

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.8 on Xubuntu 20.04.1 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 32155
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: WRITER Language of a paragraph : get / set

Postby Villeroy » Mon Jan 11, 2021 8:18 pm

Using templates and styles makes sense in any context. A single statement to apply a whole set of attributes:
Code: Select all   Expand viewCollapse view
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, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 29259
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: WRITER Language of a paragraph : get / set

Postby JeJe » Mon Jan 11, 2021 10:10 pm

Macro recorder seems to work for this

Code: Select all   Expand viewCollapse view
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
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1397
Joined: Wed Mar 09, 2016 2:40 pm

Re: WRITER Language of a paragraph : get / set

Postby nunof32 » Tue Jan 12, 2021 12:08 am

Many thanks to all who have helped.
OpenOffice 4.1.8 with MacOS X 10.13.6
nunof32
 
Posts: 56
Joined: Sat Jul 28, 2012 9:52 am

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

Postby Lupp » Tue Jan 12, 2021 6:02 pm

@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 7.0 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2996
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

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

Postby JeJe » Tue Jan 12, 2021 8:54 pm

My recording was from OO Tools Menu/Language/For Paragraph.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1397
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby nunof32 » Thu Jan 14, 2021 4:03 pm

@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   Expand viewCollapse view
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   Expand viewCollapse view
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   Expand viewCollapse view
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.8 with MacOS X 10.13.6
nunof32
 
Posts: 56
Joined: Sat Jul 28, 2012 9:52 am

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

Postby Hagar Delest » Thu Jan 14, 2021 6:30 pm

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   Expand viewCollapse view
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.0.3 on Xubuntu 20.10 and LibreOffice 6.4.6 (portable) on Windows 10.
User avatar
Hagar Delest
Moderator
 
Posts: 29399
Joined: Sun Oct 07, 2007 9:07 pm
Location: France


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 8 guests