page_white_codeChange the default language of existing pages
Developed by

Marius Dumitru Florea

Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

The following script is useful in case you have set a wrong default language in the Localization administration section. It will help you fix the default language meta data of the pages that were created with the wrong default language. Make sure you update the value of the oldDefaultLocale and newDefaultLocale variables to match your needs.

{{velocity}}
#set ($oldDefaultLocale = 'xx')
#set ($newDefaultLocale = 'yy')
#set ($matchDefaultLocale = 'doc.translation = 0 and doc.defaultLanguage = :defaultLocale')
#set ($matchTranslation = 'translation.translation = 1 and translation.language = :locale and doc.fullName = translation.fullName')
#set ($query = $services.query.xwql("from Document translation where $matchDefaultLocale and $matchTranslation order by doc.space, doc.name"))
#set ($discard = $query.bindValue('defaultLocale', $oldDefaultLocale))
#set ($discard = $query.bindValue('locale', $newDefaultLocale))
#set ($results = $query.execute())
$results.size() Results

#foreach ($result in $query.execute())
  * $result
  #if ($request.confirm == '1')
    #set ($defaultDoc = $xwiki.getDocument($result).document.clone())
    #set ($translatedDoc = $defaultDoc.getTranslatedDocument($newDefaultLocale, $xcontext.context))
    #foreach ($docToSave in $xwiki.wrapDocs([$defaultDoc, $translatedDoc]))
      #set ($discard = $docToSave.save("Change default locale from [$oldDefaultLocale] to [$newDefaultLocale]."))
    #end
  #end
#end
{{/velocity}}

Prerequisites & Installation Instructions

Copy the code snippet to a page and save it.

Tags:
Created by Marius Dumitru Florea on 2019/09/10 10:23
     

Get Connected