
セキレイ多言語サイトマップ
多言語対応のセキレイサイト向けのGoogle対応サイトマップ
Webサイトで多言語コンテンツを処理する場合は、適切にフォーマットされたサイトマップファイルを生成する必要があります。

複数の言語で書かれたコンテンツをウェブサイトで生成する場合は、ローカライズされたバージョンのページ(およびその他のページインデックスエンジン)についてGoogleに通知する必要があります。
To make this work properly, you'll need your site to provide two specific files, a robots.txt, and a sitemap.xml, both at the / root of your domain. While the Wagtail CMS: Django Content Management System does have a sitemap framework that seems capable of handling i18n translations, and the Wagtail Localize is a translation plugin that makes handling translated content a breeze. However I have to say that I couldn't get properly-formatted sitemap files to generate with these tools. Language alternate tags were present, the hreflang language codes were correct, but the href tags were always pointing at the default (English) language URL.
これを解決してメソッドを上書きするのに多くの時間を費やした後、 robots.txtの場合と同じように、Djangoテンプレートを使用してこれを実行することにしました。
これは、指示に従い、プロジェクトの設定で言語のリストを構成したことを前提としています。次に例を示します。
# my_project/settings.py
WAGTAIL_CONTENT_LANGUAGES = LANGUAGES = [
('en', "English"),
('fr', "French"),
('es', "Spanish"),
]
I first had to create my template files, one for the robots, and one for sitemap.
Here is the content of the my_project/templates/sitemap.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
{% spaceless %}
{% for url in urlset %}
<url>
<loc>{{ url.location }}</loc>
{% if url.lastmod %}
<lastmod>{{ url.lastmod|date:'Y-m-d' }}</lastmod>
{% endif %}
{% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
{% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
{% if url.language %}<lriority>{{ url.language }}</lriority>{% endif %}
{% for item in url.alternates %}
<xhtml:link rel="alternate" hreflang="{{ item.lang_code }}" href="{{ item.location }}"/>
{% endfor %}
</url>
{% endfor %}
{% endspaceless %}
</urlset>
Here is the content of the my_project/templates/robots.txt template file:
User-Agent: *
Disallow: /admin/
# Sitemap files
Sitemap: {{ wagtail_site.root_url }}/sitemap.xml
Now the code.
Below is the content of the my_project/blog/views.py file.
It will only query active pages in the current active language. For each page, we look to see if there is a live translation in any of the other configured languages.
Make sure to set the values of changefreq and priority to whatever your needs are, the values in the script are provided as an example
Note that the value of changefreq provides search engines with general information and does not necessarily reflect the actual frequency of exploration of the page. Accepted values are:
- always
- hourly
- daily
- weekly
- monthly
- yearly
- never
The value "always" should be used to describe documents that change with each access. The value "never" should be used to describe archived URLs.
from django.views.generic import TemplateView
from wagtail.core.models import Site
from .models import BlogPage as Article
from django.urls import reverse
from django.conf import settings
from wagtail.core.models import Page
from wagtail.core.models import Locale
from django.conf import settings
class RobotsView(TemplateView):
content_type = 'text/plain'
template_name = 'robots.txt'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
request = context['view'].request
context['wagtail_site'] = Site.find_for_request(request)
return context
class SiteMap(TemplateView):
content_type = 'application/xml'
template_name = 'sitemap.xml'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
request = context['view'].request
changefreq = 'daily'
priority = 0.8
context['urlset'] = []
current = Locale.get_active().language_code
domains = settings.LANGUAGES
MySitemap = Article.objects.filter(locale=Locale.objects.get(language_code=current)).live()
for page in MySitemap:
url_info = {
'location': page.url,
'lastmod': page.last_published_at,
'changefreq': changefreq,
'priority': priority,
'alternates': [],
}
for l in domains:
lang = l[0]
if lang == current:
continue
translation = Locale.objects.get(language_code=lang)
if page.has_translation(translation):
translated_blog_page = page.get_translation(translation)
if translated_blog_page.live:
url_info['alternates'].append({
'location': translated_blog_page.url,
'lang_code': lang,
})
context['urlset'].append(url_info)
return context
そして最後に、すべてが機能するようにし、 my_project / urls.pyファイルを編集して、以下を追加します。
from my_project.blog import views as blog
...
urlpatterns = [
...
path('robots.txt', blog.RobotsView.as_view()),
path('sitemap.xml', blog.SiteMap.as_view()),
....
]
いつものように、2つのWagtail実装は同じではないため、ブログ、BlogPage、Articleを正確な構成に調整する必要がありますが、これがうまく機能し、時間を節約できることを願っています。
Tagged with:
Wagtail web