Skip to content

Instantly share code, notes, and snippets.

@ssire
Created May 24, 2013 18:57
Show Gist options
  • Save ssire/5645753 to your computer and use it in GitHub Desktop.
Save ssire/5645753 to your computer and use it in GitHub Desktop.
Arguable solution to generate language switch links using lang (2 letters code) injection in post-epilogue typeswitch function site:localize: the purpose of the site:lang function is to generate a relative URL when an absolute URL (starting with '/') would be rewritten in site:localize.
declare function site:lang( $cmd as element(), $view as element() ) as element()*
{
let $lang := $cmd/@lang
let $qs := request:get-query-string()
let $action := if ($cmd/@action = ('GET', 'POST')) then () else concat('/', $cmd/@action)
let $slash := if ($cmd/@type = 'item') then () else '/'
return
(
if ($lang = 'en') then
<span id="curLg">EN</span>
else
(: generates a relative path to avoid href rewriting in site:localize :)
let $xtra := if (ends-with(request:get-uri(),'/')) then '../' else ()
let $path := concat(substring($cmd/@base-url,2), $cmd/@trail, $action)
let $rewrite := concat($xtra, replace($path, "[^\\/]+(\\/)?",'..$1'),'/',$path)
return
<a href="{$rewrite}{if ($action) then () else $slash}{if ($qs) then concat('?', $qs) else ()}" title="English">EN</a>,
<span> | </span>,
if ($lang = 'fr') then
<span id="curLg">FR</span>
else
(: hard wire 'fr/' because it will not be rewritten in site:localize since @def-lang = @lang :)
<a href="{$cmd/@base-url}fr/{$cmd/@trail}{if ($action) then $action else $slash}{if ($qs) then concat('?', $qs) else ()}" title="Français">FR</a>
)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment