<commit_before>var _ = require('underscore')._,
    mongoose = require('mongoose'),
    schema = new mongoose.Schema({
        key: {
            type: String,
            required: true
        },
        namespace: {
            type: String
        },
        created: {
            type: Date,
            required: true
        },
        sources: [{
            type: String,
            required: true
        }],
        translations: [{
            locale: {
                type: String,
                required: true
            },
            value: {
                type: String,
                required: true
            },
            changed: {
                type: Date,
                required: true
            },
            created: {
                type: Date,
                required: true,
                default: new Date()
            }
        }]
    });

schema.index({key: true, namespace: true}, {unique: true});

/**
 * Adds or updates a translation for a specific locale.
 *
 * @param {!string} locale to translate in
 * @param {?string} [value] which represents the translation
 * @param {!function(object,object)} [done] function which will be called, if the changes have been saved in the database
 * @return {{locale: string, value: string, changed: Date, created: Date}} the created or updated translation
 */
schema.methods.translate = function(locale, value, done) {
    var translation;

    if(typeof locale !== 'string') {
        throw new TypeError("The passed locale is not a string. Instead it's a: \"" + typeof(locale) + "\", " + locale);
    }
    if(value && typeof value !== 'string') {
        throw new TypeError("You have specified a value, but it's not a string. Instead it's a: \"" + typeof(value) + "\", " + value);
    }
    if(done && !_.isFunction(done)) {
        throw new TypeError("You have specified a done argument, but it's not a function. Instead it's a: \"" + typeof(value) + "\", " + done);
    }

    if(value === undefined || value === null) { // Remove translation
        this.translations = _.filter(this.translations, function(translation) {
            return translation.locale === locale;
        });
    } else { // Create update translation
        translation = _.find(this.translations, function(translation) {
            return translation.locale === locale;
        });
        if(!translation) {
            translation = {
                locale: locale,
                value: value
            }
            this.translations.push(translation);
        } else {
            translation.value = value;
        }
        translation.changed = new Date();
    }
    this.save(done);
    return translation;
}

/**
 * Returns the translation in the old format.
 *
 * @param {string|string[]} locales a locale or an array of locales to get translations for.
 * @param {boolean=false} generate translation in case it has not been translated. Default is false.
 * @returns {object|object[]} a translation in the old format, or a list of translations. Depends on the passed locales argument.
 */
schema.methods.toOldModel = function(locales, generate) {
    generate = generate || false;

    if(_.isArray(locales)) {
        var translations = [];
        _.each(locales, function(locale) {
            var translation = this.toOldModel(locale, generate);
            if(translation) {
                translations.push(translation);
            }
        }, this);
        return translations;
    } else if(_.isString(locales)) {
        var translation = _.findWhere(this.translations, {locale: locales});
        if(!translation && generate) {<commit_msg>Fixed bug with translate function, that changed date wouldn't be saved<commit_after>var _ = require('underscore')._,
    mongoose = require('mongoose'),
    schema = new mongoose.Schema({
        key: {
            type: String,
            required: true
        },
        namespace: {
            type: String
        },
        created: {
            type: Date,
            required: true
        },
        sources: [{
            type: String,
            required: true
        }],
        translations: [{
            locale: {
                type: String,
                required: true
            },
            value: {
                type: String,
                required: true
            },
            changed: {
                type: Date,
                required: true
            },
            created: {
                type: Date,
                required: true,
                default: Date.now
            }
        }]
    });

schema.index({key: true, namespace: true}, {unique: true});

/**
 * Adds or updates a translation for a specific locale.
 *
 * @param {!string} locale to translate in
 * @param {?string} [value] which represents the translation
 * @return {{locale: string, value: string, changed: Date, created: Date}} the created or updated translation
 */
schema.methods.translate = function(locale, value) {
    var translation;

    if(typeof locale !== 'string') {
        throw new TypeError("The passed locale is not a string. Instead it's a: \"" + typeof(locale) + "\", " + locale);
    }
    if(value && typeof value !== 'string') {
        throw new TypeError("You have specified a value, but it's not a string. Instead it's a: \"" + typeof(value) + "\", " + value);
    }

    if(value === undefined || value === null) { // Remove translation
        this.translations = _.filter(this.translations, function(translation) {
            return translation.locale === locale;
        });
    } else { // Create update translation
        translation = _.find(this.translations, function(translation) {
            return translation.locale === locale;
        });
        if(!translation) {
            translation = {
                locale: locale,
                value: value,
                changed: new Date()
            }
            this.translations.push(translation);
        } else {
            translation.changed = new Date();
            translation.value = value;
        }
    }
    return translation;
}

/**
 * Returns the translation in the old format.
 *
 * @param {string|string[]} locales a locale or an array of locales to get translations for.
 * @param {boolean=false} generate translation in case it has not been translated. Default is false.
 * @returns {object|object[]} a translation in the old format, or a list of translations. Depends on the passed locales argument.
 */
schema.methods.toOldModel = function(locales, generate) {
    generate = generate || false;

    if(_.isArray(locales)) {
        var translations = [];
        _.each(locales, function(locale) {
            var translation = this.toOldModel(locale, generate);
            if(translation) {
                translations.push(translation);
            }
        }, this);
        return translations;
    } else if(_.isString(locales)) {
        var translation = _.findWhere(this.translations, {locale: locales});
        if(!translation && generate) {
{'fraction_non_alphanumeric': 0.11633838741674933, 'fraction_numerical': 0.0, 'mean_word_length': 1.8890708145722472, 'pattern_counts': {'":': 0, '<': 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': '4489028', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1755, 'n_words': 559}