Skip to content

Instantly share code, notes, and snippets.

@ssire
Last active December 17, 2015 19:49
Show Gist options
  • Save ssire/5663174 to your computer and use it in GitHub Desktop.
Save ssire/5663174 to your computer and use it in GitHub Desktop.
language pre-processing sample before invoking Oppidum
declare function local:localize( $path as xs:string, $languages as xs:string, $deflang as xs:string ) as xs:string {
let $options := tokenize($languages," ")
let $code := if (matches($path,"^/\w\w/?$|^/\w\w/")) then substring($path, 2, 2) else ()
return
if ($code = $options) then
(: valid 2-letters language code in URL path, return it :)
$code
else
if ($code) then
(: wrong code in URL path, pickup default language and most probably will trigger a 404 error :)
let $clue := substring(normalize-space(request:get-header('Accept-Language')), 1, 2)
return
if ($clue = $options) then $clue else if ($deflang) then $deflang else $options[1]
else if ($path = '/') then
(: site's entry point with no code in URL path, select language to be used from browser's preferences :)
let $clue := substring(normalize-space(request:get-header('Accept-Language')), 1, 2)
return
if ($clue = $options) then
$clue
else
if ($deflang) then $deflang else $options[1]
else
(: any page with no code in URL path, should be served with site's default language :)
(: FIXME: add "!xx" convention in gen:process to redirect if no default language :)
if ($deflang) then $deflang else $options[1]
};
let $mapping := fn:doc('/db/www/---APP---/config/mapping.xml')/site
let $lang := local:localize($exist:path, string($mapping/@languages), string($mapping/@default))
return
gen:process($exist:root, $exist:prefix, $exist:controller, $exist:path, $lang, true(), $access, $actions, $mapping)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment