| 1 | var adhocURL = "http://adhoc.osgeo.osuosl.org/livedvd/docs/en/index.html"; |
| 2 | var liveURL = "http://live.osgeo.org/en/index.html"; |
| 3 | var adhocContext = "/livedvd/docs"; |
| 4 | var localContext = "/_build/html"; |
| 5 | |
| 6 | var getContext = function(url) { |
| 7 | // substring to extract the language code (two chars, e.g. 'en', 'ca', etc) |
| 8 | var context; |
| 9 | |
| 10 | // adhoc urls |
| 11 | var index1 = url.indexOf(adhocContext); |
| 12 | if (index1 >= 0) { |
| 13 | context = url.substring(index1 + adhocContext.length); |
| 14 | } else { |
| 15 | // local file |
| 16 | var i2 = url.indexOf(localContext); |
| 17 | if (i2 >= 0) { |
| 18 | context = url.substring(i2 + localContext.length); |
| 19 | } else { |
| 20 | // default at live.osgeo.org |
| 21 | context = url; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | return context; |
| 26 | } |
| 27 | |
| 28 | // returns a substring for the language from url |
| 29 | var getLanguageFormUrl = function(url) { |
| 30 | return getContext(url).substr(1, 2); |
| 31 | } |
| 32 | |
| 33 | // creates a location object from url string to work with pathname |
| 34 | var createHrefFromString = function(theStringURL) { |
| 35 | var tmpLink = document.createElement('a'); |
| 36 | // the current context, independent from adhoc or live deployment |
| 37 | tmpLink.href = theStringURL; |
| 38 | return tmpLink; |
| 39 | } |
| 40 | |
| 41 | // just a test function |
| 42 | var testAdhocURL = function() { |
| 43 | redirectFromUrlToLang(createHrefFromString(adhocURL), "zh"); |
| 44 | redirectFromUrlToLang(createHrefFromString(liveURL), "de"); |
| 45 | } |
| 46 | |
| 47 | // public accessable function to redirect from the main menu (see page.html) |
| 48 | // parameter is the new language, eg. 'en', 'de', 'ja', etc |
| 49 | var defaultRedirect = function(newLanguage) { |
| 50 | var theLastPage = document.referrer; |
| 51 | if (theLastPage === "") { |
| 52 | theLastPage = window.location; |
| 53 | } |
| 54 | redirectFromUrlToLang(theLastPage, newLanguage); |
| 55 | } |
| 56 | |
| 57 | var redirectFromUrlToLang = function(url, lang) { |
| 58 | var language; |
| 59 | if (lang.length != 2) { |
| 60 | language = "en"; |
| 61 | } else { |
| 62 | language = lang.toLowerCase(); |
| 63 | } |
| 64 | var pathName = url.pathname; |
| 65 | var newPathName; |
| 66 | if (language === getLanguageFormUrl(pathName)) { |
| 67 | newPathName = getContext(pathName); |
| 68 | } else { |
| 69 | var completeContext = getContext(pathName); |
| 70 | var pathWithoutLanguage = completeContext.substr(3); |
| 71 | var newContext = "/" + language + pathWithoutLanguage; |
| 72 | newPathName = pathName.replace(completeContext, newContext); |
| 73 | } |
| 74 | |
| 75 | var newPage = url.href.replace(pathName, newPathName); |
| 76 | |
| 77 | window.location.href = newPage; |
| 78 | |
| 79 | } |
| 80 | No newline at end of file |