/
i18n.clj
113 lines (86 loc) · 5.07 KB
/
i18n.clj
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(ns chromex.ext.i18n
"Use the chrome.i18n infrastructure to implement internationalization across your whole app or extension.
* available since Chrome 33
* https://developer.chrome.com/extensions/i18n"
(:refer-clojure :only [defmacro defn apply declare meta let partial])
(:require [chromex.wrapgen :refer [gen-wrap-helper]]
[chromex.callgen :refer [gen-call-helper gen-tap-all-events-call]]))
(declare api-table)
(declare gen-call)
; -- functions --------------------------------------------------------------------------------------------------------------
(defmacro get-accept-languages
"Gets the accept-languages of the browser. This is different from the locale used by the browser; to get the locale, use
'i18n.getUILanguage'.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [languages] where:
|languages| - Array of LanguageCode
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/i18n#method-getAcceptLanguages."
([] (gen-call :function ::get-accept-languages &form)))
(defmacro get-message
"Gets the localized string for the specified message. If the message is missing, this method returns an empty string ('').
If the format of the getMessage() call is wrong — for example, messageName is not a string or the substitutions array
has more than 9 elements — this method returns undefined.
|message-name| - The name of the message, as specified in the messages.json file.
|substitutions| - Up to 9 substitution strings, if the message requires any.
|options| - https://developer.chrome.com/extensions/i18n#property-getMessage-options.
https://developer.chrome.com/extensions/i18n#method-getMessage."
([message-name substitutions options] (gen-call :function ::get-message &form message-name substitutions options))
([message-name substitutions] `(get-message ~message-name ~substitutions :omit))
([message-name] `(get-message ~message-name :omit :omit)))
(defmacro get-ui-language
"Gets the browser UI language of the browser. This is different from 'i18n.getAcceptLanguages' which returns the preferred
user languages.
https://developer.chrome.com/extensions/i18n#method-getUILanguage."
([] (gen-call :function ::get-ui-language &form)))
(defmacro detect-language
"Detects the language of the provided text using CLD.
|text| - User input string to be translated.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [result] where:
|result| - LanguageDetectionResult object that holds detected langugae reliability and array of DetectedLanguage
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/i18n#method-detectLanguage."
([text] (gen-call :function ::detect-language &form text)))
; -- convenience ------------------------------------------------------------------------------------------------------------
(defmacro tap-all-events
"Taps all valid non-deprecated events in chromex.ext.i18n namespace."
[chan]
(gen-tap-all-events-call api-table (meta &form) chan))
; ---------------------------------------------------------------------------------------------------------------------------
; -- API TABLE --------------------------------------------------------------------------------------------------------------
; ---------------------------------------------------------------------------------------------------------------------------
(def api-table
{:namespace "chrome.i18n",
:since "33",
:functions
[{:id ::get-accept-languages,
:name "getAcceptLanguages",
:callback? true,
:params
[{:name "callback",
:type :callback,
:callback {:params [{:name "languages", :type "[array-of-i18n.LanguageCodes]"}]}}]}
{:id ::get-message,
:name "getMessage",
:return-type "string",
:params
[{:name "message-name", :type "string"}
{:name "substitutions", :optional? true, :type "any"}
{:name "options", :optional? true, :since "future", :type "object"}]}
{:id ::get-ui-language, :name "getUILanguage", :since "35", :return-type "string"}
{:id ::detect-language,
:name "detectLanguage",
:since "47",
:callback? true,
:params
[{:name "text", :type "string"}
{:name "callback", :type :callback, :callback {:params [{:name "result", :type "object"}]}}]}]})
; -- helpers ----------------------------------------------------------------------------------------------------------------
; code generation for native API wrapper
(defmacro gen-wrap [kind item-id config & args]
(apply gen-wrap-helper api-table kind item-id config args))
; code generation for API call-site
(def gen-call (partial gen-call-helper api-table))