Java properties files in Javascript clients

Hi, the following function can be used to parse Java properties files (e.g. for i18n) on the key=value format in a Javascript client. So far it handles

  • unicoded characters on the format \uXXXX

  • untrimmed keys and values

  • values with quotation marks

  • any number of line breaks

  • lines that are not key=value such as date/timestamps

Please feel free to improve it.

parseProperties = function(responseText) {

var i18n = {}, rows;



if (typeof responseText !== 'string') {

    return i18n;

}



rows = responseText.split(/\n/);

for (var i = 0, a; i < rows.length; i++) {

    if (!!(typeof rows[i] === 'string' && rows[i].length && rows[i].indexOf('=') !== -1)) {

        a = rows[i].split('=');

        i18n[a[0].trim()] = eval('"' + a[1].trim().replace(/"/g, '\'') + '"');

    }

}

return i18n;

};

I have made the script available in commons. Do an ajax request to /dhis-web-commons/javascripts/javaProperties.js to get it in your app.

···

On Wed, Aug 27, 2014 at 2:14 PM, Jan Henrik Øverland janhenrik.overland@gmail.com wrote:

Hi, the following function can be used to parse Java properties files (e.g. for i18n) on the key=value format in a Javascript client. So far it handles

  • unicoded characters on the format \uXXXX
  • untrimmed keys and values
  • values with quotation marks
  • any number of line breaks
  • lines that are not key=value such as date/timestamps

Please feel free to improve it.

parseProperties = function(responseText) {

var i18n = {}, rows;
if (typeof responseText !== 'string') {
    return i18n;
}
rows = responseText.split(/\n/);
for (var i = 0, a; i < rows.length; i++) {
    if (!!(typeof rows[i] === 'string' && rows[i].length && rows[i].indexOf('=') !== -1)) {
        a = rows[i].split('=');
        i18n[a[0].trim()] = eval('"' + a[1].trim().replace(/"/g, '\'') + '"');
    }
}
return i18n;

};

The script has been moved to dhis2.util.js instead. The function is called dhis2.util.parseJavaProperties.

···

On Wed, Aug 27, 2014 at 2:43 PM, Jan Henrik Øverland janhenrik.overland@gmail.com wrote:

I have made the script available in commons. Do an ajax request to /dhis-web-commons/javascripts/javaProperties.js to get it in your app.

On Wed, Aug 27, 2014 at 2:14 PM, Jan Henrik Øverland janhenrik.overland@gmail.com wrote:

Hi, the following function can be used to parse Java properties files (e.g. for i18n) on the key=value format in a Javascript client. So far it handles

  • unicoded characters on the format \uXXXX
  • untrimmed keys and values
  • values with quotation marks
  • any number of line breaks
  • lines that are not key=value such as date/timestamps

Please feel free to improve it.

parseProperties = function(responseText) {

var i18n = {}, rows;
if (typeof responseText !== 'string') {
    return i18n;
}
rows = responseText.split(/\n/);
for (var i = 0, a; i < rows.length; i++) {
    if (!!(typeof rows[i] === 'string' && rows[i].length && rows[i].indexOf('=') !== -1)) {
        a = rows[i].split('=');
        i18n[a[0].trim()] = eval('"' + a[1].trim().replace(/"/g, '\'') + '"');
    }
}
return i18n;

};