Wouldn't there be a more efficient way by globally changing the point to comma in Lua?
We can globally replace any periods between numbers with commas in text by adding a new font feature and in math by manually walking the node list:
\documentclass{article}
\usepackage{luacode}
\begin{luacode*}
-- Constants
local comma = utf8.codepoint(",")
local digits = { utf8.codepoint("0123456789", 1, -1)}
local digits_set = table.tohash(digits)
local glyph_id = node.id("glyph")
local period = utf8.codepoint(".")
-- Cache the results of a function as a table.
local function memoized(func)
return setmetatable({}, { __index = function(cache, key)
local ret = func(key, cache)
cache[key] = ret
return ret
end })
end
-- Convert a font ID and character index to Unicode characters.
local font_to_unicode = memoized(function(font_id)
local font = font.getfont(font_id)
return memoized(function(char)
local codes = (font.characters[char] or {}).unicode
if type(codes) == "table" then
return utf8.char(unpack(codes))
elseif type(codes) == "number" then
return utf8.char(codes)
else
return utf8.char(char)
end
end)
end)
-- Text mode: Add a new font feature that replaces periods surrounded by
-- digits with commas.
fonts.handlers.otf.addfeature {
name = "periodtocomma",
type = "chainsubstitution",
lookups = {
{
type = "multiple",
data = {
[period] = { comma },
}
},
},
data = {
rules = {
{
before = { digits },
current = { { period } },
after = { digits },
lookups = { 1 },
},
},
},
}
-- Enable this feature for all fonts by default.
luaotfload.features.global["periodtocomma"] = true
-- Math mode: Manually iterate over all glyph nodes, and replace periods
-- surrounded by digits with commas.
local function recurse(head)
for n in node.traverse(head) do
if n.id == glyph_id then
local char = font_to_unicode[n.font][n.char]
if char == "." then
local prev = n.prev
local next = n.next
if prev and
next and
prev.id == glyph_id and
next.id == glyph_id
then
local prev_char = font_to_unicode[prev.font][prev.char]
local next_char = font_to_unicode[next.font][next.char]
if digits_set[utf8.codepoint(prev_char)] and
digits_set[utf8.codepoint(next_char)]
then
n.char = comma
end
end
end
end
if n.list then
n.list = recurse(n.list)
end
end
return head
end
luatexbase.add_to_callback("post_mlist_to_hlist_filter", recurse, "periodtocomma-math")
\end{luacode*}
\usepackage{fontspec}
\setmainfont{Latin Modern Roman}
\usepackage{unicode-math}
\setmathfont{Latin Modern Math}
\pagestyle{empty}
\begin{document}
1.2 123.34 $1.2$ $123.34$
\end{document}

This also works as expected with luadraw:
\documentclass{standalone}
\usepackage{luacode}
\begin{luacode*}
-- Constants
local comma = utf8.codepoint(",")
local digits = { utf8.codepoint("0123456789", 1, -1)}
local digits_set = table.tohash(digits)
local glyph_id = node.id("glyph")
local period = utf8.codepoint(".")
-- Cache the results of a function as a table.
local function memoized(func)
return setmetatable({}, { __index = function(cache, key)
local ret = func(key, cache)
cache[key] = ret
return ret
end })
end
-- Convert a font ID and character index to Unicode characters.
local font_to_unicode = memoized(function(font_id)
local font = font.getfont(font_id)
return memoized(function(char)
local codes = (font.characters[char] or {}).unicode
if type(codes) == "table" then
return utf8.char(unpack(codes))
elseif type(codes) == "number" then
return utf8.char(codes)
else
return utf8.char(char)
end
end)
end)
-- Text mode: Add a new font feature that replaces periods surrounded by
-- digits with commas.
fonts.handlers.otf.addfeature {
name = "periodtocomma",
type = "chainsubstitution",
lookups = {
{
type = "multiple",
data = {
[period] = { comma },
}
},
},
data = {
rules = {
{
before = { digits },
current = { { period } },
after = { digits },
lookups = { 1 },
},
},
},
}
-- Enable this feature for all fonts by default.
luaotfload.features.global["periodtocomma"] = true
-- Math mode: Manually iterate over all glyph nodes, and replace periods
-- surrounded by digits with commas.
local function recurse(head)
for n in node.traverse(head) do
if n.id == glyph_id then
local char = font_to_unicode[n.font][n.char]
if char == "." then
local prev = n.prev
local next = n.next
if prev and
next and
prev.id == glyph_id and
next.id == glyph_id
then
local prev_char = font_to_unicode[prev.font][prev.char]
local next_char = font_to_unicode[next.font][next.char]
if digits_set[utf8.codepoint(prev_char)] and
digits_set[utf8.codepoint(next_char)]
then
n.char = comma
end
end
end
end
if n.list then
n.list = recurse(n.list)
end
end
return head
end
luatexbase.add_to_callback("post_mlist_to_hlist_filter", recurse, "periodtocomma-math")
\end{luacode*}
\usepackage{luadraw}%https://github.com/pfradin/luadraw/
\usepackage[locale=FR]{siunitx}% pour avoir la virgule
\begin{document}
\begin{luadraw}{name=virgule}
local g = graph:new{
window={-1,4,-1,8,1,0.4},
size={10,10}
}
--
defaultlabelshift = 0
g:Labelsize("scriptsize")
--
g:Daxes({0,0.4,1},
{
arrows="->",
limits={{0,3.2},{0,8}},
nbsubdiv={1,1},
legend={"Temps (en seconde)","Hauteur (en mètre)"},
legendpos={0.8,0.975},
legendsep={0.4,0.},
labelpos={"none","left"},-- pour ne pas mettre les graduations x
grid=true,
gridcolor="lightgray"
})
--
-- bricolage pour avoir la virgule
--
for k = 0, 2.8 , 0.4 do
g:Dlabel("$\\num{"..k.."}$",k,{pos="S",dist=0.08})
end
--
g:Show()
\end{luadraw}
\end{document}
