Reputation: 1670
I would like to change the language of a website written with the Django CMS from en-us
to de
in a way that all present contents in en-us
will be shown for de
in the future.
I tried
Page.objects.all().update(languages='de')
but afterwards there were 404 Errors everywhere.
What else do I have to change?
Upvotes: 1
Views: 1752
Reputation: 1391
The accepted answer is not compatible with the newer versions of Django CMS. The language
attribute has been changed into languages
(also see https://stackoverflow.com/a/56162296/522248). For draft mode
versions of your pages, this contains a comma separated list of available languages. Simply searching for the exact language and updating it to the next version won't work.
Given we want to change 'nl' into 'nl-nl', the following should match:
nl,en,de
de,nl,en
en,de,nl
nl
We can't just search for 'nl', because the following must not match:
nl-nl
nl-nl,en
My solution:
from_lang = 'nl'
to_lang = 'nl-nl'
# Match our lang at start, middle or end of list
lang_regex = r'^(.*,)?{}(,.*)?$'.format(from_lang)
# Get all distinct combinations of languages that contain our language
language_combinations = Page.objects\
.filter(languages__regex=lang_regex)\
.values('languages')\
.distinct()
# Create a mapping for each combination, replacing the old for the new lang
language_mappings = [
(
page['languages'],
re.sub(lang_regex, '\\1{}\\2'.format(to_lang), page['languages'])
) for page in language_combinations
]
# Update all pages to new lang
for old_languages, new_languages in language_mappings:
Page.objects\
.filter(languages=old_languages)\
.update(languages=new_languages)
# Also update Title and CMSPlugins
Title.objects.filter(language=from_lang).update(language=to_lang)
CMSPlugin.objects.filter(language=from_lang).update(language=to_lang)
Upvotes: 2
Reputation: 477
To add to @ojii's answer, with django-cms 3.6.0 & django 1.11.20 the procedure now is:
settings.py
:#LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'en'
languages
for Pages):Page.objects.filter(languages='en-us').update(languages='en')
Title.objects.filter(language='en-us').update(language='en')
CMSPlugin.objects.filter(language='en-us').update(language='en')
Upvotes: 1
Reputation: 4781
To change the language codes for all content on a django CMS site, run these commands (switching from 'en-us'
to 'de'
:
Page.objects.filter(language='en-us').update('de')
Title.objects.filter(language='en-us').update(language='de')
CMSPlugin.objects.filter(language='en-us').update(language='de')
Upvotes: 2