← KennisKnowledgeConnaissances

The comma that broke the drawings

The characters on this site are drawings made in code: the program writes an SVG picture, a list of coordinates. In an English browser they looked fine. In a Dutch or French browser some of them came out wrong, or not at all.

The problem

One line built a coordinate like this: $"{x - 1.6}". In C#, putting a number inside a text uses the culture of the computer it runs on. On this site that code runs in the visitor's own browser, so the culture is the visitor's language. In English, 76 - 1.6 is written 74.4. In Dutch and French it is written 74,4, with a comma.

SVG does not care about languages: a coordinate is always written with a point, and the comma separates two numbers. So 74,4 became two coordinates instead of one, and the drawing fell apart.

How I found it

The tests passed, because the test computer ran in English. The bug only showed up when the culture was Dutch. Once I switched a test to Dutch, the broken coordinate appeared at once.

The fix

  • Every number that goes into a drawing now passes through one small helper that always writes it the same way: v.ToString("0.#", CultureInfo.InvariantCulture).
  • No number goes into the SVG text directly any more, not even a "simple" one.
  • A test draws every character with the culture set to Dutch and checks that no coordinate contains a comma. It is called NumbersStayDotDecimalInADutchBrowser, so the next person knows exactly what it guards.

The lesson: text meant for a machine (SVG, JSON, CSV, SQL) must never use the culture of the person. Text meant for the person should.

De komma die de tekeningen brak

De figuurtjes op deze site zijn tekeningen die in code gemaakt worden: het programma schrijft een SVG-afbeelding, een lijst coördinaten. In een Engelstalige browser zagen ze er goed uit. In een Nederlands- of Franstalige browser kwamen sommige verkeerd of helemaal niet op het scherm.

Het probleem

Eén regel maakte een coördinaat zo: $"{x - 1.6}". Als je in C# een getal in een tekst zet, gebruikt het de taalinstelling van de computer waarop het draait. Op deze site draait die code in de browser van de bezoeker, dus is dat de taal van de bezoeker. In het Engels schrijf je 76 - 1,6 als 74.4. In het Nederlands en het Frans wordt het 74,4, met een komma.

SVG trekt zich niets aan van talen: een coördinaat schrijf je altijd met een punt, en de komma scheidt twee getallen. Dus werd 74,4 twee coördinaten in plaats van één, en viel de tekening uit elkaar.

Hoe ik het vond

De tests slaagden, want de testcomputer draaide in het Engels. De fout kwam alleen boven met een Nederlandse taalinstelling. Zodra ik een test in het Nederlands liet lopen, stond de kapotte coördinaat er meteen.

De oplossing

  • Elk getal dat in een tekening gaat, loopt nu door één klein hulpje dat het altijd op dezelfde manier schrijft: v.ToString("0.#", CultureInfo.InvariantCulture).
  • Geen enkel getal gaat nog rechtstreeks in de SVG-tekst, ook geen "eenvoudig" getal.
  • Een test tekent elk figuurtje met de taal op Nederlands en controleert dat geen enkele coördinaat een komma bevat. Hij heet NumbersStayDotDecimalInADutchBrowser, zodat de volgende die het leest meteen weet wat hij bewaakt.

De les: tekst voor een machine (SVG, JSON, CSV, SQL) mag nooit de taal van de mens gebruiken. Tekst voor de mens wel.

La virgule qui cassait les dessins

Les personnages de ce site sont des dessins faits en code : le programme écrit une image SVG, une liste de coordonnées. Dans un navigateur en anglais, ils étaient parfaits. Dans un navigateur en néerlandais ou en français, certains s'affichaient mal, ou pas du tout.

Le problème

Une ligne construisait une coordonnée ainsi : $"{x - 1.6}". En C#, mettre un nombre dans un texte utilise la langue de l'ordinateur sur lequel le code tourne. Sur ce site, ce code tourne dans le navigateur du visiteur, donc c'est la langue du visiteur. En anglais, 76 - 1,6 s'écrit 74.4. En néerlandais et en français, cela devient 74,4, avec une virgule.

Le SVG ne se soucie pas des langues : une coordonnée s'écrit toujours avec un point, et la virgule sépare deux nombres. 74,4 devenait donc deux coordonnées au lieu d'une, et le dessin se défaisait.

Comment je l'ai trouvé

Les tests passaient, car l'ordinateur de test tournait en anglais. L'erreur n'apparaissait qu'avec une langue néerlandaise. Dès que j'ai fait tourner un test en néerlandais, la coordonnée cassée est apparue tout de suite.

La solution

  • Chaque nombre qui entre dans un dessin passe maintenant par une petite fonction qui l'écrit toujours de la même façon : v.ToString("0.#", CultureInfo.InvariantCulture).
  • Plus aucun nombre n'entre directement dans le texte SVG, même pas un nombre « simple ».
  • Un test dessine chaque personnage avec la langue réglée sur le néerlandais et vérifie qu'aucune coordonnée ne contient de virgule. Il s'appelle NumbersStayDotDecimalInADutchBrowser, pour que le prochain lecteur sache exactement ce qu'il protège.

La leçon : un texte destiné à une machine (SVG, JSON, CSV, SQL) ne doit jamais utiliser la langue de la personne. Un texte destiné à la personne, si.

An unhandled error has occurred. Reload 🗙