[Solved] WRITER Language of a paragraph : get / set
[Solved] WRITER Language of a paragraph : get / set
Hello,
How do I get() / set() the Language for a text range ?
Thanks in advance.
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
Re: WRITER Language of a paragraph : get / set
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: WRITER Language of a paragraph : get / set
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
Re: WRITER Language of a paragraph : get / set
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
Re: WRITER Language of a paragraph : get / set
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: WRITER Language of a paragraph : get / set
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)
Re: WRITER Language of a paragraph : get / set
Many thanks to all who have helped.
OpenOffice 4.1.10 with MacOS X 10.13.6
Re: [Solved] WRITER Language of a paragraph : get / set
@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).
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
---
Lupp from München
Re: [Solved] WRITER Language of a paragraph : get / set
My recording was from OO Tools Menu/Language/For Paragraph.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: [Solved] WRITER Language of a paragraph : get / set
@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' :
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.
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 :
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.
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
To set its 'Language' was more difficult.
Code: Select all
paraPortion.CharLocale.Language = "en"
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"
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
- Hagar Delest
- Moderator
- Posts: 32683
- Joined: Sun Oct 07, 2007 9:07 pm
- Location: France
Re: [Solved] WRITER Language of a paragraph : get / set
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.
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: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.]
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 24.2 on Xubuntu 24.04 and 7.6.4.1 portable on Windows 10