Page 1 of 1

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

PostPosted: Mon Jan 11, 2021 7:55 pm
by nunof32
Hello,

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

Thanks in advance.

Re: WRITER Language of a paragraph : get / set

PostPosted: Mon Jan 11, 2021 8:09 pm
by Villeroy
Use paragraph styles.

Re: WRITER Language of a paragraph : get / set

PostPosted: Mon Jan 11, 2021 8:11 pm
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.

Re: WRITER Language of a paragraph : get / set

PostPosted: Mon Jan 11, 2021 8:11 pm
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.

Re: WRITER Language of a paragraph : get / set

PostPosted: Mon Jan 11, 2021 8:18 pm
by Villeroy
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"

Re: WRITER Language of a paragraph : get / set

PostPosted: Mon Jan 11, 2021 10:10 pm
by JeJe
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

Re: WRITER Language of a paragraph : get / set

PostPosted: Tue Jan 12, 2021 12:08 am
by nunof32
Many thanks to all who have helped.

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

PostPosted: Tue Jan 12, 2021 6:02 pm
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).

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

PostPosted: Tue Jan 12, 2021 8:54 pm
by JeJe
My recording was from OO Tools Menu/Language/For Paragraph.

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

PostPosted: Thu Jan 14, 2021 4:03 pm
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   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.

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

PostPosted: Thu Jan 14, 2021 6:30 pm
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   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