Skip to content

Instantly share code, notes, and snippets.

@todofixthis
Last active December 22, 2015 01:19
Show Gist options
  • Save todofixthis/6395194 to your computer and use it in GitHub Desktop.
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.
/* ... 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);
<?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