xquery version "1.0"; declare namespace session="http://exist-db.org/xquery/session"; declare namespace util="http://exist-db.org/xquery/util"; declare namespace i18n="http://apache.org/cocoon/i18n/2.1"; declare namespace source="http://apache.org/cocoon/source/1.0"; import module namespace ristenutil="http://www.risten.no/shared/util" at "../xquery/ristenutil.xqm"; (: Update the classification document: :) declare function local:store-translations($docref as xs:string, $lang as xs:string) as node()+ { let $coll := ristenutil:get-collection() let $xlang := if ($lang eq 'nb') then 'nob' else if ($lang eq 'no') then 'nor' else if ($lang eq 'se') then 'sme' else if ($lang eq 'nn') then 'nno' else if ($lang eq 'sv') then 'swe' else if ($lang eq 'fi') then 'fin' else if ($lang eq 'en') then 'eng' else $lang return
    { for $tr in doc($docref)//message let $id := $tr/@key cast as xs:string return if (collection($coll)// (macro|micro|nano)[@id eq $id]/label[@xml:lang = $xlang]) then
  1. NothingAdded
  2. else ( update insert following collection($coll)//(macro|micro|nano)[@id eq $id]/label[last()],
  3. Key: {$id} - {$tr/text()}
  4. ) }
}; (: Update the classification document: :) declare function local:update-meta($lang as xs:string) as node() { let $coll := ristenutil:get-collection() let $xlang := if ($lang eq 'nb') then 'nob' else if ($lang eq 'no') then 'nor' else if ($lang eq 'se') then 'sme' else if ($lang eq 'nn') then 'nno' else if ($lang eq 'sv') then 'swe' else if ($lang eq 'fi') then 'fin' else if ($lang eq 'en') then 'eng' else $lang return if ( collection($coll)/classmeta/langs/lang = $xlang ) then AlreadyThere else ( update insert {$xlang} into collection($coll)/classmeta/langs, {$xlang} ) }; (: Main function - retrieves some request and session attributes, forks further processing to other defined functions depending on the retrieved values :) declare function local:main() as element()+ { (: for $lang in /classmeta[id eq 'SD-class']/langs/lang return {$lang/node()} :) let $eXistROOT := util:system-property("user.dir"), $importlang := request:get-parameter("importlang", ""), $coll := session:get-attribute("collection"), $docref := concat('file://', $eXistROOT, '/webapp/risten/translations/', $coll, '_', $importlang, '.xml'), $result := local:store-translations($docref, $importlang), $metaupdate := local:update-meta($importlang) return

ImportMessage {$docref} {$metaupdate}
{$result}

};