Skip to content

Commit

Permalink
Merge pull request #10311 from tk0miya/10124_invalid_language_tag
Browse files Browse the repository at this point in the history
Fix #10214: html: invalid language tag was generated for zh_CN
  • Loading branch information
tk0miya committed Apr 2, 2022
2 parents 633e079 + 11a1dbe commit 35381cc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -67,6 +67,8 @@ Bugs fixed

* #10279: autodoc: Default values for keyword only arguments in overloaded
functions are rendered as a string literal
* #10214: html: invalid language tag was generated if :confval:`language`
contains a country code (ex. zh_CN)
* #10236: html search: objects are duplicated in search result
* #9962: texinfo: Deprecation message for ``@definfoenclose`` command on
bulding texinfo document
Expand Down
15 changes: 13 additions & 2 deletions sphinx/builders/html/__init__.py
Expand Up @@ -7,7 +7,7 @@
import sys
from datetime import datetime
from os import path
from typing import IO, Any, Dict, Iterable, Iterator, List, Set, Tuple, Type
from typing import IO, Any, Dict, Iterable, Iterator, List, Optional, Set, Tuple, Type
from urllib.parse import quote

from docutils import nodes
Expand Down Expand Up @@ -68,6 +68,17 @@ def get_stable_hash(obj: Any) -> str:
return md5(str(obj).encode()).hexdigest()


def convert_locale_to_language_tag(locale: Optional[str]) -> Optional[str]:
"""Convert a locale string to a language tag (ex. en_US -> en-US).
refs: BCP 47 (:rfc:`5646`)
"""
if locale:
return locale.replace('_', '-')
else:
return None


class Stylesheet(str):
"""A metadata of stylesheet.
Expand Down Expand Up @@ -510,7 +521,7 @@ def prepare_writing(self, docnames: Set[str]) -> None:
'file_suffix': self.out_suffix,
'link_suffix': self.link_suffix,
'script_files': self.script_files,
'language': self.config.language,
'language': convert_locale_to_language_tag(self.config.language),
'css_files': self.css_files,
'sphinx_version': __display_version__,
'sphinx_version_tuple': sphinx_version,
Expand Down

0 comments on commit 35381cc

Please sign in to comment.