Skip to content

Instantly share code, notes, and snippets.

@ariefbayu
Last active October 1, 2015 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariefbayu/1991636 to your computer and use it in GitHub Desktop.
Save ariefbayu/1991636 to your computer and use it in GitHub Desktop.
Pure HTML+JSON i18n page
{"title": "this is title", "content": "this is english content"}
{"title": "ini adalah judul", "content": "ini adalah isi dalam bahasa indonesia"}
<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