summaryrefslogtreecommitdiffstats
path: root/extensions/renderer/resources/i18n_custom_bindings.js
blob: de69fa2ebe018be6800ae2013bad068e173e8b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Custom binding for the i18n API.

var binding = require('binding').Binding.create('i18n');

var i18nNatives = requireNative('i18n');
var GetL10nMessage = i18nNatives.GetL10nMessage;
var GetL10nUILanguage = i18nNatives.GetL10nUILanguage;
var DetectTextLanguage = i18nNatives.DetectTextLanguage;

binding.registerCustomHook(function(bindingsAPI, extensionId) {
  var apiFunctions = bindingsAPI.apiFunctions;

  apiFunctions.setUpdateArgumentsPreValidate('getMessage', function() {
    var args = $Array.slice(arguments);

    // The first argument is the message, and should be a string.
    var message = args[0];
    if (typeof(message) !== 'string') {
      console.warn(extensionId + ': the first argument to getMessage should ' +
                   'be type "string", was ' + message +
                   ' (type "' + typeof(message) + '")');
      args[0] = String(message);
    }

    return args;
  });

  apiFunctions.setHandleRequest('getMessage',
                                function(messageName, substitutions) {
    return GetL10nMessage(messageName, substitutions, extensionId);
  });

  apiFunctions.setHandleRequest('getUILanguage', function() {
    return GetL10nUILanguage();
  });

  apiFunctions.setHandleRequest('detectLanguage', function(text, callback) {
    window.setTimeout(function() {
      var response = DetectTextLanguage(text);
      callback(response);
    }, 0);
  });
});

exports.$set('binding', binding.generate());