[Basic, Python] Reporting all number format locales

Creating Extension - Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This forum is not for asking questions about writing your own macros.

[Basic, Python] Reporting all number format locales

Postby Villeroy » Sun Nov 30, 2014 2:45 pm

The following code dumps info about all number format locales into a new spreadsheet. In addition it formats the first 1024 columns of each row (until column IV) in the "General" number format of the respective locale so you can test the input patterns and default appearance of dates and times etc.
Code: Select all   Expand viewCollapse view
Sub printAllLocalesToNewSpreadSheet()
Dim oDoc, oSheet, i18n, oInfo, a(), b(),i%, oItem, nf&, nfs&

oDoc = StarDesktop.loadComponentFromURL("private:factory/scalc","_default",0,Array())
oSheet = oDoc.getSheets().getByIndex(0)
nfs = com.sun.star.i18n.NumberFormatIndex.NUMBER_STANDARD

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()

nf = oDoc.NumberFormats.getStandardIndex(a(i))
oSheet.getCellRangeByPosition(0, i+1, 255, i+1).NumberFormat = nf
next
oSheet.getCellRangeByPosition(0, 0, cCols, uBound(r())).setDataArray(r())
End Sub

Function getLocaleString(oL)
dim s$
s = oL.Language
if len(oL.Country) then
   s = s &"-"& oL.Country
   if len(oL.Variant) then s = s &"-"& oL.Variant
End If
getLocaleString = s
End Function

 Edit: Sorry to the first 15 viewers. I forgot to paste helper function getLocaleString() 

 Edit: Just in case someone prefers Python: 

Code: Select all   Expand viewCollapse view
import uno

class Office:
    '''Frequently used methods in office context'''
    def __init__(self, ctx=uno.getComponentContext()):
        self.ctx = ctx
        self.smgr = self.ctx.ServiceManager
       
    def createUnoService(self, service):
        return self.smgr.createInstance(service)

    def getDesktop(self):
        return self.smgr.createInstanceWithContext("com.sun.star.frame.Desktop",self.ctx)

    def getCurrentComponent(self):
        return self.getDesktop().getCurrentComponent()

    def getCurrentFrame(self):
        return self.getDesktop().getCurrentFrame()

    def getCurrentComponentWindow(self):
        return self.getCurrentFrame().getComponentWindow()

    def getCurrentContainerWindow(self):
        return self.getCurrentFrame().getContainerWindow()

    def getCurrentController(self):
        return self.getCurrentFrame().getController()

    def callMRI(self,obj=None):
        if not obj:
            obj = self.getCurrentController().getSelection()
        mri = self.createUnoService("mytools.Mri")
        mri.inspect(obj)

def getLocaleString(oL):
    s = '-'
    a = [oL.Language]
    if oL.Country:a.append(oL.Country)
    if oL.Variant:a.append(oL.Variant)
    return s.join(a)

def printAllLocalesToNewSpreadSheet():
    office = Office()
    StarDesktop = office.getDesktop()
    oDoc = StarDesktop.loadComponentFromURL("private:factory/scalc","_default",0,tuple())
    oSheet = oDoc.getSheets().getByIndex(0)
    i18n = office.createUnoService("com.sun.star.i18n.LocaleData")
    a = i18n.getAllInstalledLocaleNames()
    cCols = 7
    r = list()
    r.append(("Locale","Language","Country","Decimal","Date","Time","1000","List"))
    for i in a:
       oInfo = i18n.getLanguageCountryInfo(i)
        oItem = i18n.getLocaleItem(i)
       b = (
          getLocaleString(i),
          oInfo.LanguageDefaultName,
          oInfo.CountryDefaultName,
          oItem.decimalSeparator,
          oItem.dateSeparator,
          oItem.timeSeparator,
          oItem.thousandSeparator,
          oItem.listSeparator
       )
       r.append(b)
        rc = len(r)
        nf = oDoc.NumberFormats.getStandardIndex(i)
        oSheet.getCellRangeByPosition(0, rc-1, 255, rc-1).NumberFormat = nf
    oSheet.getCellRangeByPosition(0, 0, cCols, rc-1).setDataArray(tuple(r))
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 16.04, OpenOffice 4.x & LibreOffice 5.x
User avatar
Villeroy
Volunteer
 
Posts: 24661
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 3 guests