Last active
December 22, 2015 01:19
-
-
Save todofixthis/6395194 to your computer and use it in GitHub Desktop.
Adds JSONP support to `jquery.i18n.properties`. See http://code.google.com/p/jquery-i18n-properties/#Known_issues for more info.
This file contains 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
/* ... snip ... */ | |
/** Load and parse .properties files */ | |
function loadAndParseFile(filename, settings) { | |
/* If we are making a cross-domain request, switch to JSONP mode. */ | |
var dataType = 'text'; | |
if( /^(https?:)?\/\//.test(filename) ) { | |
dataType = 'jsonp'; | |
} | |
$.ajax({ | |
url: filename, | |
async: false, | |
cache: settings.cache, | |
contentType:'text/plain;charset='+ settings.encoding, | |
dataType: dataType, | |
success: function(data, status) { | |
parseData(data, settings.mode); | |
} | |
}); | |
} | |
/* ... snip ... */ | |
/* Invoke like this: */ | |
(function($) { | |
$(function() { | |
$.i18n.properties({ | |
path: 'http://www.example.com/path/to/serverside-i18n.php?file=', | |
/* ... */ | |
}); | |
}) | |
})(jQuery); |
This file contains 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
<?php | |
foreach( array('file', 'callback') as $key ) | |
{ | |
if( empty($_GET[$key]) ) | |
{ | |
header('HTTP/1.0 400 Bad Request'); | |
exit; | |
} | |
} | |
$file = $_GET['file']; | |
$func = $_GET['callback']; | |
# @todo Verify that file has the correct naming format. | |
$path = sprintf('%s/%s', __DIR__, basename($file)); | |
if( is_file($path) ) | |
{ | |
header('Content-type: application/javascript'); | |
printf( | |
'%s(%s);' | |
, $func | |
, json_encode(file_get_contents($path)) | |
); | |
} | |
else | |
{ | |
header('HTTP/1.0 404 Not Found'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment