OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

[Java] Set writer document language (locale)

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
woerie
Newbie
Newbie


Joined: 06 Oct 2009
Posts: 1

PostPosted: Tue Oct 06, 2009 6:32 am    Post subject: [Java] Set writer document language (locale) Reply with quote

Hello

I have an embedded OOo Writer in a java application, through the officebean. And want to set the document language when loading it.

First I tried to set the Language by receiving the document and it's XPropertySet and setting the "Language" property. The property gets set, but it doesn't change anything in the embedded Writer. I tested by setting other properties like the title or author and those worked perfectly.
Code:

OfficeDocument a = myOOoBean.getDocument();
XDocumentPropertiesSupplier xdps = (XDocumentPropertiesSupplier)
UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, a);   
XDocumentProperties xdp = xdps.getDocumentProperties();       

XDocumentInfoSupplier xdis = (XDocumentInfoSupplier)UnoRuntime.queryInterface(XDocumentInfoSupplier.class,a);
XDocumentInfo xdi = xdis.getDocumentInfo();
XPropertySet xps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xdi);
XPropertySetInfo xpsi = xps.getPropertySetInfo();
Property[] propy = xpsi.getProperties();

// Try to set a value
Object r = "TEST TITLE";
xps.setPropertyValue("Title", r);
r = "Test Author";
xps.setPropertyValue("Author", r);
// Works perfectly

// Doesn't work
xps.setPropertyValue("Language", new Locale("af","ZA",""));


Then I tried a different approach. Retrieving and setting the linguistic LinguProperties, but this also yielded no results.
Code:

protected XMultiServiceFactory mxFactory = null;
private boolean getLingProps() throws Exception
{
      if (mxFactory != null)
      {
          Object aObj = mxFactory.createInstance("com.sun.star.linguistic2.LinguProperties" );
          mxLinguProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, aObj);
      }
      return mxLinguProps != null;
}

private void setLang2() throws Exception, java.lang.Exception
{
   XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
   XMultiComponentFactory xLocalServiceManager = xContext.getServiceManager();

   Object xUrlResolver = xLocalServiceManager.createInstanceWithContext
               ("com.sun.star.bridge.UnoUrlResolver", xContext );

   XUnoUrlResolver urlResolver = (XUnoUrlResolver)UnoRuntime.queryInterface(XUnoUrlResolver.class, xUrlResolver);

   Object rInitialObject = urlResolver.resolve( "uno:" + "socket,host=localhost,port=8100"+ ";urp;StarOffice.NamingService");

   XNamingService rName = (XNamingService)UnoRuntime.queryInterface(XNamingService.class,rInitialObject );

        if( rName != null )
        {
            Object rXsmgr = rName.getRegisteredObject( "StarOffice.ServiceManager");
            mxFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, rXsmgr);
         }

        if(getLingProps())
        {
            System.out.println("Got the LingProps");
            Locale aLocale = new Locale("af", "ZA", "");

            Object s = mxLinguProps.getPropertyValue("DefaultLocale");
            Locale p = (Locale)s;
            System.out.println("Default Locale:");
            System.out.println(p.Language + "  " + p.Country);

            System.out.println("Set Default Locale");
            mxLinguProps.setPropertyValue("DefaultLocale", (Object)aLocale);
        }
}

The successfully retrieves the properties and displays the default locale, but setting it doesn't do squat.

Next I'm considering to select all the text in the document and set the language property for the selected text. But I first wanted to inquire if what I'm attempting is even possible and if so what course of action to take.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group