Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sitemap_locales option #25

Merged
merged 1 commit into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,34 @@ Multilingual Configuration
For multilingual sitemaps, you have to generate a sitemap per language/locale
and then manually add their locations to a `sitemapindex`_ file.

The extension will look at the `language`_ config value for the current language
being built, and the `locale_dirs`_ value for the directory for alternate
languages, so make sure those are set.
Primary language is `language`_ config value. Alternative languages are either
manually set by ``sitemap_locales`` option or auto-detected by the extension from
the `locale_dirs`_ config value, so make sure one of those is set.

``sitemap_locales`` configuration is handy you want to list in the sitemap only some
of existing locales, if third-party extension adds locale_dirs to Sphinx for the
languages which you don't support in your docs, or to "exclude" primary language
(`language`_). For example, if primary language is en, sitemap will contain it twice::

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://my-site.com/docs/en/index.html</loc>
<xhtml:link href="https://my-site.com/docs/es/index.html" hreflang="es" rel="alternate"/>
<xhtml:link href="https://my-site.com/docs/fr/index.html" hreflang="fr" rel="alternate"/>
<xhtml:link href="https://my-site.com/docs/en/index.html" hreflang="en" rel="alternate"/>
</url>
<url>
<loc>https://my-site.com/docs/en/about.html</loc>
<xhtml:link href="https://my-site.com/docs/es/about.html" hreflang="es" rel="alternate"/>
<xhtml:link href="https://my-site.com/docs/fr/about.html" hreflang="fr" rel="alternate"/>
<xhtml:link href="https://my-site.com/docs/en/about.html" hreflang="en" rel="alternate"/>
</url>
</urlset>

If you limit sitemap::

sitemap_locales = ['es', 'fr']

The end result is something like the following for each language/version build::

Expand All @@ -64,13 +89,27 @@ The end result is something like the following for each language/version build::
<loc>https://my-site.com/docs/en/index.html</loc>
<xhtml:link href="https://my-site.com/docs/es/index.html" hreflang="es" rel="alternate"/>
<xhtml:link href="https://my-site.com/docs/fr/index.html" hreflang="fr" rel="alternate"/>
<xhtml:link href="https://my-site.com/docs/en/index.html" hreflang="en" rel="alternate"/>
</url>
<url>
<loc>https://my-site.com/docs/en/about.html</loc>
<xhtml:link href="https://my-site.com/docs/es/about.html" hreflang="es" rel="alternate"/>
<xhtml:link href="https://my-site.com/docs/fr/about.html" hreflang="fr" rel="alternate"/>
<xhtml:link href="https://my-site.com/docs/en/about.html" hreflang="en" rel="alternate"/>
</url>
</urlset>

If you set special value ``[None]``::

sitemap_locales = [None]

only primary language is generated::

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://my-site.com/docs/en/index.html</loc>
</url>
<url>
<loc>https://my-site.com/docs/en/about.html</loc>
</url>
</urlset>

Expand Down
20 changes: 20 additions & 0 deletions sphinx_sitemap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def setup(app):
default="{lang}{version}{link}",
rebuild=False
)
app.add_config_value(
'sitemap_locales',
default=None,
rebuild=False
jdillard marked this conversation as resolved.
Show resolved Hide resolved
)

try:
app.add_config_value(
Expand All @@ -50,6 +55,21 @@ def setup(app):


def get_locales(app, exception):
# Manually configured list of locales
sitemap_locales = app.builder.config.sitemap_locales
if sitemap_locales:
# special value to add nothing -> use primary language only
if sitemap_locales == [None]:
return

# otherwise, add each locale
for locale in sitemap_locales:
# skip primary language
if locale != app.builder.config.language:
app.locales.append(locale)
return

# Or autodetect
for locale_dir in app.builder.config.locale_dirs:
locale_dir = os.path.join(app.confdir, locale_dir)
if os.path.isdir(locale_dir):
Expand Down