RingDotNetfor Ring

Cultures

"1,234.5" and "1.234,5" are the same number written in two languages. This lesson shows how .NET writes, reads, sorts and capitalizes text for different cultures, and when to avoid culture altogether.

What you will learn#

What is a culture?#

A culture is a language plus, optionally, a country: en-US (English in the United States), de-DE (German in Germany), ar-EG (Arabic in Egypt). It decides:

Every ToString and Parse that you call without saying a culture uses the culture of the computer's user. That is exactly right for text that people read, and exactly wrong for data that programs exchange.

The program#

samples/UsingDotNet/tutorials/ch03_cultures.ring
# cultures - the same number, date and text written, read and sorted in different languages
load "dotnet.ring"

using {
	"System"
	"System.Globalization"
}

func main

	oString = new String()
	oConvert = new Convert()
	aCultures = ["en-US","de-DE","fr-FR","ar-EG","ja-JP"]

	# A culture (language + country) decides how numbers, money and dates are written
	? "A number, money and a date in five cultures:"
	oDate = new DateTime(2024,7,4)
	for cName in aCultures
		oCulture = new CultureInfo(cName)
		cNumber = oString.Format([ :provider = oCulture, :format = "{0:N2}", :arg0 = 1234567.891 ])
		cMoney = oString.Format([ :provider = oCulture, :format = "{0:C}", :arg0 = 1234.5 ])
		cLong = oDate.Format([ :format = "D", :provider = oCulture ])
		? "   " + cName + " : " + cNumber + "  |  " + cMoney + "  |  " + cLong
	next

	# Reading must use the same culture as the writing: "1.234" is 1.234 in English and 1234 in German
	? nl + "Reading '1.234,5' and '1,234.5' with two cultures:"
	oGerman = new CultureInfo("de-DE")
	oEnglish = new CultureInfo("en-US")
	? "   German reads '1.234,5' as   : " + new Double().Parse([ :s = "1.234,5", :provider = oGerman ])
	? "   English reads '1,234.5' as  : " + new Double().Parse([ :s = "1,234.5", :provider = oEnglish ])
	? "   English reads '1.234,5' ok? : " + new Double().TryParse([ :s = "1.234,5", :style = "Any", :provider = oEnglish ])[1]

	# The invariant culture never changes: use it for files and data that programs exchange
	oInvariant = new CultureInfo("en-US").InvariantCulture
	? nl + "Invariant culture   : " + oString.Format([ :provider = oInvariant, :format = "{0:N2}", :arg0 = 1234567.891 ]) + "  (for files, not for people)"

	# Sorting and comparing: letters sort differently in different languages
	? nl + "Comparing 'a' and 'B' (negative: the first sorts first, positive: the second):"
	? "   by character code (Ordinal) : " + oString.Compare([ :strA = "a", :strB = "B", :comparisonType = "Ordinal" ]) + "   (a = 97 is after B = 66)"
	? "   by the English alphabet     : " + oString.Compare([ :strA = "a", :strB = "B", :ignoreCase = False, :culture = oEnglish ]) + "   (a comes before b)"

	# Case changes also depend on the culture (the Turkish i is the famous example)
	oTurkish = new CultureInfo("tr-TR")
	? nl + "'title' in upper case, English: " + oEnglish.TextInfo.ToUpper("title")
	? "'title' in upper case, Turkish: " + oTurkish.TextInfo.ToUpper("title")

	# The culture of this computer
	? nl + "The current culture is known: " + (len(new CultureInfo("en-US").CurrentCulture.Name) > 0)
Output
A number, money and a date in five cultures:
   en-US : 1,234,567.89  |  $1,234.50  |  Thursday, July 4, 2024
   de-DE : 1.234.567,89  |  1.234,50 €  |  Donnerstag, 4. Juli 2024
   fr-FR : 1 234 567,89  |  1 234,50 €  |  jeudi 4 juillet 2024
   ar-EG : 1٬234٬567٫89  |  1٬234٫50 ج.م.‏  |  الخميس، 4 يوليو 2024
   ja-JP : 1,234,567.89  |  ¥1,234  |  2024年7月4日木曜日

Reading '1.234,5' and '1,234.5' with two cultures:
   German reads '1.234,5' as   : 1234.50
   English reads '1,234.5' as  : 1234.50
   English reads '1.234,5' ok? : 0

Invariant culture   : 1,234,567.89  (for files, not for people)

Comparing 'a' and 'B' (negative: the first sorts first, positive: the second):
   by character code (Ordinal) : 31   (a = 97 is after B = 66)
   by the English alphabet     : -1   (a comes before b)

'title' in upper case, English: TITLE
'title' in upper case, Turkish: TİTLE

The current culture is known: 1

How it works#

Writing#

String.Format(culture, "{0:N2}", value) formats with a format code: N2 is a number with two decimals and a thousands separator, C is currency, D (on a date) the long date. The same value 1234567.891 becomes 1,234,567.89 in English, 1.234.567,89 in German, 1 234 567,89 in French (with a special space) and uses Arabic digits for ar-EG.

Reading#

Double.Parse(text, culture) reads a number written in that culture. The text 1.234,5 is 1234.5 for the German culture, but an error or a different number for the English culture, where the comma means "thousands". TryParse avoids the error and returns [worked, value]. Always say which culture a text was written in.

The invariant culture#

InvariantCulture is a fixed culture that never changes with the computer: . for decimals, no thousands separator problems. Use it for files, network messages, JSON, logs — anything a program writes for another program. Use the user's culture only for what you show to people.

Warning

The most common internationalization bug: a program that writes 3.5 on one computer and 3,5 on another, then fails to read the file. Format and parse data with the invariant culture.

Comparing and sorting#

String.Compare(a, b, ...) takes how to compare: "Ordinal" compares the character codes (a = 97 comes after B = 66), the English alphabet puts a before b. Other languages sort letters differently (in Swedish ö comes after z). Use a culture comparison to sort names for people, ordinal to compare identifiers and keys.

Upper and lower case#

ToUpper depends on the culture: in Turkish the capital of i is İ (with a dot), so title becomes TİTLE. Use the invariant culture for identifiers and keys.

What you learned#

Exercises#

  1. Print today's date in a short format in six cultures of your choice.
  2. Parse "3,5" with fr-FR and with en-US: which works?
  3. Sort the words zebra apple Ärger Öl ananas in German and in Swedish.
  4. Write a function toData(nNumber) that formats a number with the invariant culture, and fromData(cText) that reads it back. Test both.

Where to go next#

This was the last tutorial of the chapter Numbers, dates and money. The next chapter is Collections and algorithms: it starts with Arrays. The Tutorials page lists all the chapters, and you can start any of them.

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.