% Language definition file (part of polyglossia v2.10 -- 2026/04/18)
%
\ProvidesFile{gloss-mongolian.ldf}[polyglossia: module for Mongolian]

\RequirePackage{xpg-cyrillicnumbers}

\PolyglossiaSetup{mongolian}{
  bcp47=mn,
  bcp47-language=mn,
  bcp47-script=Cyrl,
  script=Cyrillic,
  scripttag=cyrl,
  langtag=MNG,
  hyphennames={mongolian},
  hyphenmins={2,2},
  frenchspacing=true,
  localnumeral=mongoliannumerals,
  Localnumeral=Mongoliannumerals
}

% BCP-47 compliant aliases
\setlanguagealias*{mongolian}{mn}

% This file is derived from mongolian-babel. which
% provides support for Mongolian (Khalkha variety)
% with Cyrillic script
% TODO: Implement script=mongolian and maybe other
%       variants of Mongolian

\newif\ifcyrillic@numerals
\newif\ifcyrillic@asbuk@numerals

\DeclareKeys[polyglossia/gloss/mongolian]
  {
    babelshorthands.if = mongolian@babelshorthands,
    babelshorthands.default:n = true,
    numerals.choice:,
    numerals / arabic.code =
      {%
        \cyrillic@numeralsfalse
        \cyrillic@asbuk@numeralsfalse
      },
    numerals / cyrillic.code =
      {%
        \cyrillic@numeralstrue
        \cyrillic@asbuk@numeralsfalse
      },
    numerals / cyrillic-trad.code =
      {%
        \cyrillic@numeralstrue
        \cyrillic@asbuk@numeralsfalse
      },
    numerals / cyrillic-alph.code =
      {%
        \cyrillic@numeralstrue
        \cyrillic@asbuk@numeralstrue
      },
    numerals.default:n = arabic,
    numerals/unknown.code = \xpg@warning{Unknown Mongolian numerals value `#1'}
  }

\ifsystem@babelshorthands
  \SetGlossOptions{mongolian}{babelshorthands=true}
  % Register default options
  \InitializeGlossOptions{mongolian}{babelshorthands=true,numerals=arabic}
\else
  \SetGlossOptions{mongolian}{babelshorthands=false}
  % Register default options
  \InitializeGlossOptions{mongolian}{babelshorthands=false,numerals=arabic}
\fi

\InitializeBabelShorthands

\def\mongolian@shorthands{%
  \xpg@activate@shorthands%
  \def\language@group{mongolian}%
  \declare@shorthand{mongolian}{"`}{„}%
  \declare@shorthand{mongolian}{"'}{“}%
  \declare@shorthand{mongolian}{"<}{«}%
  \declare@shorthand{mongolian}{">}{»}%
  \declare@shorthand{mongolian}{""}{\xpg@invisible@break}%
  \declare@shorthand{mongolian}{"~}{\xpg@nobreak@hyphen}%
  \declare@shorthand{mongolian}{"=}{\xpg@breakable@hyphen}%
  \declare@shorthand{mongolian}{"|}{\xpg@ligature@break}%
  \declare@shorthand{mongolian}{"-}{\xpg@cyrillic@dash}%
  \declare@shorthand{mongolian}{",}{\xpg@thinspace@allow@hyphenation@after}%
}

\ifx\cyrdash\undefined
  \def\cyrdash{\leavevmode\hbox to.8em{\textendash\hss\textendash}}%
\fi

\def\nomongolian@shorthands{%
  \xpg@deactivate@shorthands%
}

% Taken from babel-mongolian
\def\captionsmongolian{%
   \def\prefacename{Өмнөх үг}%
   \def\refname{Ашигласан ном}%
   \def\abstractname{Удиртгал}%
   \def\bibname{Номзүй}%
   \def\chaptername{Бүлэг}%
   \def\appendixname{Хавсралт}%
   \@ifundefined{thechapter}%
     {\def\contentsname{Агуулга}}%
     {\def\contentsname{Гарчиг}}%
   \def\listfigurename{Зургийн жагсаалт}%
   \def\listtablename{Хүснэгтийн жагсаалт}%
   \def\indexname{Товъёг}%
   \def\authorname{Нэрийн хэлхээ}%
   \def\figurename{Зураг}%
   \def\tablename{Хүснэгт}%
   \def\partname{Хэсэг}%
   \def\enclname{Ишлэл}%
   \def\ccname{э.с.}%
   \def\headtoname{}%
   \def\pagename{тал}%
   \def\seename{талд үз}%
   \def\alsoname{мөн талд үз}%
   \def\proofname{Баталгаа}%
}

\def\datemongolian{%
  \def\today{\number\year~оны \ifcase\month\or
  1-р\or 2-р\or 3-р\or 4-р\or 5-р\or 6-р\or
  7-р\or 8-р\or 9-р\or 10-р\or 11-р\or 12-р\fi
  ~сарын \number\day}}

\newcommand{\mongoliannumerals}[2]{\mongoliannumber{#2}}
\newcommand{\Mongoliannumerals}[2]{\Mongoliannumber{#2}}

\def\mongoliannumber#1{%
  \ifcyrillic@numerals
    \ifcyrillic@asbuk@numerals
      \mongolian@asbuk@alph{#1}%
    \else
      \cyr@alph{#1}%
    \fi
  \else
    \number#1%
  \fi%
}

\def\Mongoliannumber#1{%
  \ifcyrillic@numerals
    \ifcyrillic@asbuk@numerals
      \mongolian@asbuk@Alph{#1}%
    \else
      \cyr@Alph{#1}%
    \fi
  \else
    \number#1%
  \fi%
}

\let\mongoliannumeral=\mongoliannumber
\let\Mongoliannumeral=\Mongoliannumber

\def\Asbuk#1{\expandafter\mongolian@asbuk@Alph\csname c@#1\endcsname}
\def\asbuk#1{\expandafter\mongolian@asbuk@alph\csname c@#1\endcsname}

\def\AsbukTrad#1{\expandafter\cyr@Alph\csname c@#1\endcsname}
\def\asbukTrad#1{\expandafter\cyr@alph\csname c@#1\endcsname}

% This is a poor man's cyrillic alphanumeric. It just uses the alphabet and
% thus ends at 30.
\def\mongolian@asbuk@Alph#1{\ifcase#1\or
   А\or Б\or В\or Г\or Д\or Е\or Ж\or
   З\or И\or К\or Л\or М\or Н\or О\or
   П\or Р\or С\or Т\or У\or Ф\or Х\or
   Ц\or Ч\or Ш\or Щ\or Э\or Ю\or Я%
   \else\xpg@ill@value{#1}{mongolian@asbuk@Alph}\fi%
}

\def\mongolian@asbuk@alph#1{\ifcase#1\or
   а\or б\or в\or г\or д\or е\or ж\or
   з\or и\or к\or л\or м\or н\or о\or
   п\or р\or с\or т\or у\or ф\or х\or
   ц\or ч\or ш\or щ\or э\or ю\or я%
   \else\xpg@ill@value{#1}{mongolian@asbuk@alph}\fi%
}

\def\mongolian@numbers{%
   \let\latin@alph\@alph
   \let\latin@Alph\@Alph
   \ifcyrillic@numerals%
     \def\mongolian@alph##1{\expandafter\mongoliannumeral\expandafter{\the##1}}%
     \def\mongolian@Alph##1{\expandafter\Mongoliannumeral\expandafter{\the##1}}%
      \let\@alph\mongolian@alph%
      \let\@Alph\mongolian@Alph%
   \fi
}

\def\nomongolian@numbers{%
   \let\@alph\latin@alph%
   \let\@Alph\latin@Alph%
}

\def\noextras@mongolian{%
   \ifcyrillic@numerals\nomongolian@numbers\fi%
   \ifmongolian@babelshorthands\nomongolian@shorthands\fi%
}

\def\blockextras@mongolian{%
   \ifcyrillic@numerals\mongolian@numbers\else\nomongolian@numbers\fi%
   \ifmongolian@babelshorthands\mongolian@shorthands\else\nomongolian@shorthands\fi%
}

\def\inlineextras@mongolian{%
   \ifmongolian@babelshorthands\mongolian@shorthands\else\nomongolian@shorthands\fi%
}

\endinput

