As an attempt to practice with expl3, I am trying to parse numerical date string to French. There is a \mymodule_display_date_year_month_day:n
which receives a string of form yyyy-mm-dd
(or yyyy-mm
or mm-dd
, which is not done yet), and tries to convert it to French text. However, it cannot successfully parse the yyyy-mm-dd
string and pass the three integers to \mymodule_display_date_year_month_day_french:nnn
.
Here is what I've tried so far. Where does it go wrong?
\documentclass{article}
\usepackage[french,english]{babel}
\begin{document}
\selectlanguage{french}
\ExplSyntaxOn
\int_new:N \l__mymodule_year_int
\int_new:N \l__mymodule_month_int
\int_new:N \l__mymodule_day_int
\cs_new:Nn \mymodule_text_superscript:n { \textsuperscript { #1 } }
\cs_new_protected:Nn \mymodule_display_date_year_month_day:n
{
\regex_match:nnTF { \A \d{4}-\d{1,2}-\d{1,2} \Z } { #1 }
{
\__mymodule_parse_yyyymmdd:www #1 \q_stop
\exp_args:Nx \cs_if_exist_use:cTF { mymodule_display_date_year_month_day_ \languagename :nnn }
{
{ \l__mymodule_year_int }
{ \l__mymodule_month_int }
{ \l__mymodule_day_int }
}
{ #1 }
}
{
\regex_match:nnTF { \A \d{4}-\d{1,2} \Z } { #1 }
{
%%
}
{
\regex_match:nnTF { \A \d{4}-\d{1,2}-\d{1,2} \Z } { #1 }
{
%%
}
{ ?? }
}
}
}
\cs_new:Npn \__mymodule_parse_yyyymmdd:www #1-#2-#3 \q_stop
{
\int_set:Nn \l__mymodule_year_int { #1 }
\int_set:Nn \l__mymodule_month_int { #2 }
\int_set:Nn \l__mymodule_day_int { #3 }
}
\cs_new:Nn \mymodule_display_date_year_month_day_french:nnn
{
\mymodule_display_date_month_day_french:nn { #2 } { #3 }
\c_space_tl
#1
}
\cs_new:Nn \mymodule_display_date_year_month_french:nn
{
\mymodule_display_date_month_french:n { #2 }
\c_space_tl
#1
}
\cs_new_protected:Nn \mymodule_display_date_month_day_french:nn
{
\mymodule_display_date_day_french:n { #2 }
\c_space_tl
\mymodule_display_date_month_french:n { #1 }
}
\cs_new:Nn \mymodule_display_date_month_french:n
{
\int_case:nnF { #1 }
{
{ 1 } { janvier }
{ 2 } { février }
{ 3 } { mars }
{ 4 } { avril }
{ 5 } { mai }
{ 6 } { juin }
{ 7 } { juillet }
{ 8 } { août }
{ 9 } { septembre }
{ 10 } { octobre }
{ 11 } { novembre }
{ 12 } { décembre }
} { #1 }
}
\cs_new:Nn \mymodule_display_date_day_french:n
{
\int_case:nnF { #1 }
{
{ 1 } { 1 \mymodule_text_superscript:n { er } }
} { #1 }
}
\mymodule_display_date_year_month_day:n {2021-06-03}
\ExplSyntaxOff
\end{document}
\cs_new_protected:Npn
when your macros contain other non-expandable macros. Also, variables (like integers) should start with their scope, so\l__mymodule_year_int
instead of\__mymodule_year_int
interface3
, macros marked with a ★ or a ☆ are expandable, and everything else is not. If your macro contains any non-expandable macro, it is also non-expandable. If you are using some non-expl3 macro, you can find out experimentally by doing\edef\x{<macro>}\show\x
. If what shows in the terminal is what you expect, the macro is (likely) expandable. If it explodes into an error or shows gibberish, it's not expandable