Last active
October 1, 2015 12:27
-
-
Save ariefbayu/1991636 to your computer and use it in GitHub Desktop.
Pure HTML+JSON i18n page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"title": "this is title", "content": "this is english content"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"title": "ini adalah judul", "content": "ini adalah isi dalam bahasa indonesia"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="mustache.js"></script> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
<script> | |
/* | |
taken from http://stackoverflow.com/a/5158301/156869 | |
*/ | |
function getParameterByName(name) { | |
var match = RegExp('[?&]' + name + '=([^&]*)') | |
.exec(window.location.search); | |
return match && decodeURIComponent(match[1].replace(/\+/g, ' ')); | |
} | |
$(document).ready(function(){ | |
var possibleLang = ['en', 'id']; | |
var currentLang = getParameterByName("lang"); | |
console.log("parameter lang: " + currentLang); | |
console.log("possible lang: " + (jQuery.inArray(currentLang, possibleLang))); | |
if(jQuery.inArray(currentLang, possibleLang) > -1){ | |
console.log("fetching AJAX"); | |
var request = jQuery.ajax({ | |
processData: false, | |
cache: false, | |
url: "data_" + currentLang + ".json" | |
}); | |
console.log("done AJAX"); | |
request.done(function(data){ | |
console.log("got data: " + data); | |
var output = Mustache.render("{{title}} + {{content}}", data); | |
console.log("output: " + output); | |
$("#output").append(output); | |
}); | |
request.fail(function(xhr, textStatus){ | |
console.log("error: " + textStatus); | |
}); | |
console.log("last line"); | |
} | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="output"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment