<commit_before>var at = require('../../ast').js;
var l10nContext = require('../l10n/context.js');
var utils = require('../../build/misc/utils.js');

//
// NEW API
//
module.exports = function(file, flow, defineHandler, globalScope){
  var fconsole = flow.console;
  var basisResolveURI = flow.js.basis.resource.resolveURI;

  flow.l10n = l10nContext.create(flow, file);

  var resolveL10nToken = function(key, dictPath){
    var id = key + '@' + dictPath;
    var tokenDescriptor = flow.l10n.getToken(id);

    if (!tokenDescriptor.jsToken)
    {
      var token = ['object', []];
      token.objSource = {};
      token.obj = {
        compute: at.createRunner(function(token_, this_, args){
          var id = key + '.{?}' + '@' + dictPath;

          fconsole.log('[basis.l10n] basis.l10n.Dictionary#token.compute ' + id);
          token_.obj = resolveL10nToken(key + '.{?}', dictPath).jsToken.obj;
        })
      };

      tokenDescriptor.jsToken = token;
    }

    return tokenDescriptor;
  };

  var resolveDictionary = function(filename){
    var dict = flow.l10n.getDictionary(filename);

    if (!dict.jsToken)
    {
      var file = dict.file;
      var token = ['object', []];
      token.file = file;
      token.objSource = {};
      token.obj = {
        token: at.createRunner(function(token_, this_, args){
          var key = this.scope.simpleExpression(args[0]);

          if (key && key[0] == 'string')
          {
            var id = key[1] + '@' + file.filename;
            fconsole.log('[basis.l10n] basis.l10n.Dictionary#token ' + id);

            var l10nToken = resolveL10nToken(key[1], file.filename);
            l10nToken.addRef(this.file, token_);
            token_.obj = l10nToken.jsToken.obj;
          }
          else
          {
            flow.warn({
              file: this.file.relpath,
              message: 'basis.l10n.dictionary#token: first argument is not resolved, token: ' + at.translate(token_)
            });
          }
        })
      };

      dict.jsToken = token;
    }

    return dict;
  };

  // TODO: fetch culture list from basis.l10n
  defineHandler(globalScope, 'basis.l10n.setCultureList', function(token, this_, args){
    // FIXME: make better resolve solution
    var list = at.getCallArgs(args, this.file.context, flow, this.file)[0];
    flow.l10n.cultures = {};

    fconsole.start('[basis.l10n] ' + at.translate(token) + ' in ' + this.file.relpath);

    if (typeof list == 'string')
      list = list.trim().split(/\s+/);

    if (Array.isArray(list))
    {
      for (var i = 0, cultureDef; cultureDef = list[i]; i++)
      {
        var clist = cultureDef.split(/\//);<commit_msg>extract: better warning for non-resolved argument in dictionary.token()<commit_after>var at = require('../../ast').js;
var l10nContext = require('../l10n/context.js');
var utils = require('../../build/misc/utils.js');

//
// NEW API
//
module.exports = function(file, flow, defineHandler, globalScope){
  var fconsole = flow.console;
  var basisResolveURI = flow.js.basis.resource.resolveURI;

  flow.l10n = l10nContext.create(flow, file);

  var resolveL10nToken = function(key, dictPath){
    var id = key + '@' + dictPath;
    var tokenDescriptor = flow.l10n.getToken(id);

    if (!tokenDescriptor.jsToken)
    {
      var token = ['object', []];
      token.objSource = {};
      token.obj = {
        compute: at.createRunner(function(token_, this_, args){
          var id = key + '.{?}' + '@' + dictPath;

          fconsole.log('[basis.l10n] basis.l10n.Dictionary#token.compute ' + id);
          token_.obj = resolveL10nToken(key + '.{?}', dictPath).jsToken.obj;
        })
      };

      tokenDescriptor.jsToken = token;
    }

    return tokenDescriptor;
  };

  var resolveDictionary = function(filename){
    var dict = flow.l10n.getDictionary(filename);

    if (!dict.jsToken)
    {
      var file = dict.file;
      var token = ['object', []];
      token.file = file;
      token.objSource = {};
      token.obj = {
        token: at.createRunner(function(token_, this_, args){
          var key = this.scope.simpleExpression(args[0]);

          if (key && key[0] == 'string')
          {
            var id = key[1] + '@' + file.filename;
            fconsole.log('[basis.l10n] basis.l10n.Dictionary#token ' + id);

            var l10nToken = resolveL10nToken(key[1], file.filename);
            l10nToken.addRef(this.file, token_);
            token_.obj = l10nToken.jsToken.obj;
          }
          else
          {
            flow.warn({
              file: this.file.relpath,
              message: 'First argument of basis.l10n.dictionary#token() is not resolved: ' + at.translate(token_),
              loc: this.file.location((token_[2][0] || token_[1]).start)
            });
          }
        })
      };

      dict.jsToken = token;
    }

    return dict;
  };

  // TODO: fetch culture list from basis.l10n
  defineHandler(globalScope, 'basis.l10n.setCultureList', function(token, this_, args){
    // FIXME: make better resolve solution
    var list = at.getCallArgs(args, this.file.context, flow, this.file)[0];
    flow.l10n.cultures = {};

    fconsole.start('[basis.l10n] ' + at.translate(token) + ' in ' + this.file.relpath);

    if (typeof list == 'string')
      list = list.trim().split(/\s+/);

    if (Array.isArray(list))
    {
      for (var i = 0, cultureDef; cultureDef = list[i]; i++)
      {
        var clist = cultureDef.split(/\//);
{'fraction_non_alphanumeric': 0.15802016498625115, 'fraction_numerical': 0.018148487626031166, 'mean_word_length': 2.280817799158148, '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': '7556556', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1728, 'n_words': 345}