2013/3/10 Johnny Rosenberg <gurus.knu...@gmail.com>:
> 2013/3/10 Alex Thurgood <alex.thurg...@gmail.com>:
>> Le 09/03/2013 21:52, Johnny Rosenberg a écrit :
>>
>> Hi Johnny,
>>
>> http://api.libreoffice.org/docs/common/ref/com/sun/star/i18n/LocaleDataItem.html#decimalSeparator
>
> Thanks! That looks like what I was looking for!
> Now I only need to figure out how to use it…
> But don't worry, I will.
>
>
> Johnny Rosenberg
>
>
>>
>>
>> Alex

I did some more searching after a couple of failed experiments, and I
found this code at
http://www.oooforum.org/forum/viewtopic.phtml?t=121837:
REM - - - CODE STARTS HERE - - -
Sub printAllLocalesToNewSpreadSheet()
    Dim oDoc, oSheet, i18n, oInfo, a(), b(),i%, oItem
    i18n = createUnoService("com.sun.star.i18n.LocaleData")
    a() = i18n.getAllInstalledLocaleNames()
    dim r(uBound(a()) +1)

    Const cCols = 7
    r(0) = 
Array("Locale","Language","Country","Decimal","Date","Time","1000","List")
    For i = 0 to uBound(a())
       oInfo = i18n.getLanguageCountryInfo(a(i))
       oItem = i18n.getLocaleItem(a(i))
       b() = Array( _
       getLocaleString(a(i)), _
       oInfo.LanguageDefaultName, _
       oInfo.CountryDefaultName, _
       oItem.decimalSeparator, _
       oItem.dateSeparator, _
       oItem.timeSeparator, _
       oItem.thousandSeparator, _
       oItem.listSeparator _
       )
        r(i +1) = b()
    next
    oDoc = 
StarDesktop.loadComponentFromURL("private:factory/scalc","_default",0,Array())
    oSheet = oDoc.getSheets().getByIndex(0)
    oSheet.getCellRangeByPosition(0, 0, cCols, uBound(r())).setDataArray(r())
End Sub
REM - - - CODE ENDS HERE - - -

However, it seems like some code is not complete. There is no
getLocaleString and there are a few more issues with it, so I edited
it a bit:
✓ I wrote a new getLocaleString function.
✓ I eliminated the need for the cCols variable, making the whole thing
work a little bit more ”automatically”.
✓ I removed the lines that create a new spreadsheet, printing on the
existing one instead. This is just a test thing anyway.
✓ I ”Johnnyfied” the code slightly, which isn't necessarily a good
thing… For example the Dim statements: For some of them, I couldn't
decide whether to use Object or Variant, please correct me if
necessary.

Here's my version anyway, and it works:

REM - - - CODE STARTS HERE - - -
Sub printAllLocalesToSpreadSheet()
    Dim oSheet As Object
    Dim i18n As Object, oInfo As Object, oItem As Object
    Dim a() As Variant, b() As Variant
    Dim i As Integer

    i18n=createUnoService("com.sun.star.i18n.LocaleData")
    a()=i18n.getAllInstalledLocaleNames()

    Dim r(uBound(a())+1)
    
r(0)=Array("Locale","Language","Country","Decimal","Date","Time","1000","List")

    For i=0 to uBound(a())
       oInfo=i18n.getLanguageCountryInfo(a(i))
       oItem=i18n.getLocaleItem(a(i))
       b()=Array( _
          getLocaleString(a(i)), _
          oInfo.LanguageDefaultName, _
          oInfo.CountryDefaultName, _
          oItem.decimalSeparator, _
          oItem.dateSeparator, _
          oItem.timeSeparator, _
          oItem.thousandSeparator, _
          oItem.listSeparator _
       )
        r(i+1)=b()
    Next

    oSheet=ThisComponent.getSheets().getByIndex(0)
    
oSheet.getCellRangeByPosition(0,0,uBound(r(0)),uBound(r())).setDataArray(r())
End Sub

Function getLocaleString(x As Variant) As String
    Dim sString As String

    sString=x.Language & "_" & x.Country
    If x.Variant="" Then
        getLocaleString=sString
    Else
        getLocaleString=getLocaleString & "(" & x.Variant & ")"
    EndIf
End Function
REM - - - CODE ENDS HERE - - -

From this code I did some further experiments and came up with this:
REM - - - CODE STARTS HERE - - -
Sub Test
'   Tests for Swedish default decimal- and thousand separator.
   Dim i18n As Object, oItem As Object
   Dim a As New com.sun.star.lang.Locale

   i18n=createUnoService("com.sun.star.i18n.LocaleData")
   With a
       .Language="sv"
       .Country="SE"
       .Variant=""
   End With
   oItem=i18n.getLocaleItem(a)

   Dim Message as String
   Message="Locale: " & a.Language & "_" & a.Country & " " & a.Variant & _
      Chr(13) & Chr(13) & _
      "Decimal separator: " & Chr(34) & oItem.decimalSeparator & Chr(34) _
      & Chr(13) & _
       "Thousand separator: " & Chr(34) & oItem.thousandSeparator & Chr(34)
   MsgBox(Message)
End Sub
REM - - - CODE ENDS HERE - - -

After fiddling around a bit I finally ended up with this one, that
will tell you the locale and separators of cell A1:
REM - - - CODE STARTS HERE - - -
Sub Test
    ' Tests for decimal- and thousand separator of cell A1 in first sheet.
    Dim i18n As Object, oItem As Object
    Dim Sheet As Object, Cell As Object

    i18n=createUnoService("com.sun.star.i18n.LocaleData")

    Sheet=ThisComponent.getSheets().getByIndex(0)
    Cell=Sheet.getCellByPosition(0,0) ' Cell A1

    oItem=i18n.getLocaleItem(Cell.CharLocale)
    ' Type of Cell.CharLocale = com.sun.star.lang.Locale

    Dim Message as String
    Message="Locale: " & _
        Cell.CharLocale.Language & "_" & _
        Cell.CharLocale.Country & " " & _
        Cell.CharLocale.Variant & Chr(13) & Chr(13) & _
        "Decimal separator: " & Chr(34) & oItem.decimalSeparator & Chr(34) _
        & Chr(13) & _
        "Thousand separator: " & Chr(34) & oItem.thousandSeparator & Chr(34)
    MsgBox(Message)
End Sub
REM - - - CODE ENDS HERE - - -

So it seems like the problem is solved now.
Thanks for the hints you provided!


Johnny Rosenberg

-- 
For unsubscribe instructions e-mail to: users+h...@global.libreoffice.org
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted

Reply via email to