Formatting numbers and date/time using current locale in NiceForm and NiceLabel Portal

Locale is a set of parameters that defines the user’s language, country and any special variant preferences that the user wants to see in their user interface. Usually a locale identifier consists of at least a language identifier and a region identifier.

General settings usually include the following display format settings:

  • Number format setting
  • Character classification, case conversion settings
  • Date/Time format setting
  • String collation setting
  • Currency format setting
  • Paper size setting
  • Other minor settings

The locale settings are about formatting output given a locale. For example you can see some of the settings for the Slovenian locales on my machine in the screenshot below.

Region and Language

Even more options are available if you click the “Additional settings…” button.

Customize Number, Currency, Time and Date Format

To view or modify these changes  in Windows 7 you have to follow these steps:

  1. Click Start > Control Panel.
  2. Double-click the Region and Language icon. The Region and Language dialog box appears.

Please note that you have to be logged in with an account with Administrative Privileges in order for this to work.

Working with Locales in Python Scripts

To work with locales in Python scripts in NiceForm you have to use the locale module. Your script would typically start with something like this:

import locale
locale.setlocale(locale.LC_ALL, '')

This sets the locale for all categories (number format settings, currency format settings, date/time settings, etc.) to the user’s default setting.

You can also set the locale to a specific one. For example to change current locale in your Python script to German, you could use the following code:

locale.setlocale(locale.LC_ALL, 'deu_deu')

or to French

locale.setlocale(locale.LC_ALL, 'fra_fra')

Unfortunately locale module is not supported in IronPython (that is used to implement scripting in NiceLabel Portal) so you have to write a little different code. To obtain information about current locale (or current culture as it is called in Silverlight) you have to use the following code:

import System
culture = System.Threading.Thread.CurrentThread.CurrentCulture


And to set the culture you would do this:

from System.Globalization import *
culture = CultureInfo("de-DE");
System.Threading.Thread.CurrentThread.CurrentCulture = culture

Formatting Numbers Using Current Locale

The following code converts values of two NiceForm (or NiceLabel Portal) variables (that are represented as strings) into floating point numbers using the current locales, calculates the difference between them and assigns this new value to the third variable value (again as a string value).

import sys
if sys.platform == 'silverlight':
    import System
    culture = System.Threading.Thread.CurrentThread.CurrentCulture  
    number1 = System.Single.Parse(originalSellingPrice.Value, culture.NumberFormat)
    number2 = System.Single.Parse(sellingPrice.Value, culture.NumberFormat)
    diff = number1 - number2
    subtract_price.Value = diff.ToString("F")
else:
    import locale
    locale.setlocale(locale.LC_ALL, '')
    number1 = locale.atof(originalSellingPrice.Value)
    number2 = locale.atof(sellingPrice.Value)
    diff = number1 - number2
    subtract_price.Value = locale.format("%.2f", diff)


Script should work in both NiceLabel Portal (code block after the if statement) and NiceForm (code block after the else statement).

Formatting Date/Time Using Current Locale

Again code for NiceLabel Portal and NiceForm is a little different. It does however in both cases display date and time in different formats using user’s current locale settings.

import sys
if sys.platform == 'silverlight':
    import System
    dt = System.DateTime.Now
    d1.Value = "Long time: " + dt.ToLongTimeString()
    d2.Value = "Long date: " + dt.ToLongDateString()
    d3.Value = "Short time: " + dt.ToShortTimeString()    
    d4.Value = "Short date: " + dt.ToShortDateString()
    d5.Value = "Day of the week: " + dt.ToString("ddd") + " (abbreviated), " + dt.ToString("dddd") + " (full)"
    d6.Value = "Month name: " + dt.ToString("MMM") + " (abbreviated), " + dt.ToString("MMMM") + " (full)"
else:
    from datetime import date
    import time
    import locale
    locale.setlocale(locale.LC_ALL, '')   

    date1 = date.today();        
    d1.Value = time.strftime("Long time: %H:%M:%S");
    d2.Value = date1.strftime("Long date: %d.%B %Y");        
    d3.Value = time.strftime("Short time: %X");
    d4.Value = date1.strftime("Short date: %x"); 
    d5.Value = date1.strftime("Day of the week: %a (abbreviated), %A (full)");
    d6.Value = date1.strftime("Month name: %b (abbreviated), %B (full)");

To use a specific locale (French in this case) you would do this:

import sys
if sys.platform == 'silverlight':
    import System
    from System.Globalization import *
    culture = CultureInfo("fr-FR");
    System.Threading.Thread.CurrentThread.CurrentCulture = culture
    dt = System.DateTime.Now
    d1.Value = "Long time: " + dt.ToLongTimeString()
    d2.Value = "Long date: " + dt.ToLongDateString()
    d3.Value = "Short time: " + dt.ToShortTimeString()    
    d4.Value = "Short date: " + dt.ToShortDateString()
    d5.Value = "Day of the week: " + dt.ToString("ddd", culture) + " (abbreviated), " + dt.ToString("dddd", culture) + " (full)"
    d6.Value = "Month name: " + dt.ToString("MMM", culture) + " (abbreviated), " + dt.ToString("MMMM", culture) + " (full)"
else:
    from datetime import date
    import time
    import locale
    locale.setlocale(locale.LC_ALL, 'fra_fra')   

    date1 = date.today();        
    d1.Value = time.strftime("Long time: %H:%M:%S");
    d2.Value = date1.strftime("Long date: %d.%B %Y");
    d3.Value = time.strftime("Short time: %H:%M");
    d4.Value = date1.strftime("Short date: %d.%b.%Y");
    d5.Value = date1.strftime("Day of the week: %a (abbreviated), %A (full)");
    d6.Value = date1.strftime("Month name: %b (abbreviated), %B (full)");


Related posts:

http://www.primozic.net/nl/manipulating-date-and-time-using-python-in-niceform-and-nicelabel-portal/

http://www.primozic.net/nl/working-with-modules-in-python-scripts-in-niceform-and-nicelabel-portal/

http://www.primozic.net/nl/working-with-strings-in-python-scripts-in-niceform/

http://www.primozic.net/nl/python-scripting-in-niceform-and-nicelabel-portal/

Posted in NiceForm, NiceLabel Portal Tagged with: , , , , ,