<commit_before>/**
 * i18n plugin
 * register handlebars handler which looks up translations in a dictionary
 */

var path = require("path");
var fs = require("fs");
var handlebars = require("handlebars");

module.exports = function makePlugin() {
        return function (opts) {
            var data = {
                "en": {}
            };

            //load translation data
            if (opts.locales) {
                try {
                    opts.locales.forEach( function (loc) {
                        content = fs.readFileSync(path.join(__dirname + "../../../", loc.file));
                        data[loc.locale] = JSON.parse(content);
                    });
                } catch (e) {
                    console.error(e);
                }
            }

            handlebars.registerHelper("i18n", function (msg) {
                if (msg && this.file.locale && data[this.file.locale] && data[this.file.locale][msg]) {
                    return data[this.file.locale][msg];
                }
                return msg;
            });

            return function(files, metalsmith, done){
                for (filename in files) {
                    // add locale metadata to files
                    var locale = filename.split("/")[0];
                    files[filename]['locale'] = locale;
                }
                done();
            };
        };
    };<commit_msg>Handle missing title localisation file.<commit_after>/**
 * i18n plugin
 * register handlebars handler which looks up translations in a dictionary
 */

var path = require("path");
var fs = require("fs");
var handlebars = require("handlebars");

module.exports = function makePlugin() {
        return function (opts) {
            var data = {
                "en": {}
            };

            //load translation data
            if (opts.locales) {
                try {
                    opts.locales.forEach( function (loc) {
                        try {
                            content = fs.readFileSync(path.join(__dirname + "../../../", loc.file));
                            data[loc.locale] = JSON.parse(content);
                        } catch (e) {
                            console.error("Failed to load: " + e.path);
                        }
                    });
                } catch (e) {
                    console.error(e);
                }
            }

            handlebars.registerHelper("i18n", function (msg) {
                if (msg && this.file.locale && data[this.file.locale] && data[this.file.locale][msg]) {
                    return data[this.file.locale][msg];
                }
                return msg;
            });

            return function(files, metalsmith, done){
                for (filename in files) {
                    // add locale metadata to files
                    var locale = filename.split("/")[0];
                    files[filename]['locale'] = locale;
                }
                done();
            };
        };
    };
{'fraction_non_alphanumeric': 0.12409240924092409, 'fraction_numerical': 0.0033003300330033004, 'mean_word_length': 1.1572953736654805, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1409537', 'n_tokens_mistral': 755, 'n_tokens_neox': 701, 'n_words': 169}