opal-i18next 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6e23990c8b1a275471ba5880ea7b75db97794620ef037d28b411c21986b1d6d
4
- data.tar.gz: 1ef817b85846c9bb8b7a93321aef9bf15a637bec76947ca6362579ec497cb94b
3
+ metadata.gz: a608d9ba3fba9ee08cc35ee247cad22a5c89c9950232c7f9f70c73e588d6da9a
4
+ data.tar.gz: 8e82b9dadb4b73f55d5156ac1deccfe7c1b17f1c64abec548734c33f7b9c040b
5
5
  SHA512:
6
- metadata.gz: 4288938af6057505cc90f478ff7c366e0a95be8a35257729800c1480b814ffc5f88e8d86b9e3a8a2e0e014933a0e85bbc14b97fba16f578f251d3019d2fb36a6
7
- data.tar.gz: ad2988876b8b3dafd899da0949d0380280d85ff564d52eb3be1b187a12d0cd2bd02b69606e48e71e47476f0177cb8750bd256312eed74653ae364f55d80131cc
6
+ metadata.gz: 904501d0d9ef54c0c23a011cb5326db007e06f5d6ae42880e778330ce68003e3b667a6d5c19b92a3be4ee95f9dcc7b1c147e4fd2e732208139038f0bb85ba74e
7
+ data.tar.gz: 7494d5183b943f29a92745405684c97569bff3309396f7abc9eebdf558185c0c616e3e68c6156d8866ba3f4be8cf1a4774a99d8d5c172242a4674152ac872240
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --no-private opal/i18next/**/*.rb
@@ -1,5 +1,6 @@
1
1
  require "opal"
2
2
  require "native"
3
+ require "json"
3
4
 
4
5
  # Uses PromiseV2 if present
5
6
  # @see https://opalrb.com/docs/guides/v1.5.1/async PromiseV2
@@ -9,14 +10,29 @@ require "native"
9
10
 
10
11
  # {I18next} is a basic wrapper around the JavaScript {https://www.i18next.com i18next module}.
11
12
  #
12
- # It wraps i18next methods {https://www.i18next.com/overview/api#init init},
13
- # {https://www.i18next.com/overview/api#use use},
14
- # {https://www.i18next.com/overview/api#t t},
15
- # {https://www.i18next.com/overview/api#changelanguage changeLanguage}, and
16
- # {https://www.i18next.com/overview/api#language language}.
13
+ # It wraps i18next methods {https://www.i18next.com/overview/api#addResource addResource},
14
+ # {https://www.i18next.com/overview/api#addResources addResources},
15
+ # {https://www.i18next.com/overview/api#changelanguage changeLanguage},
16
+ # {https://www.i18next.com/overview/api#dir dir},
17
+ # {https://www.i18next.com/overview/api#exists exists},
18
+ # {https://www.i18next.com/overview/api#getResource getResource},
19
+ # {https://www.i18next.com/overview/api#getResourceBundle getResourceBundle},
20
+ # {https://www.i18next.com/overview/api#init init},
21
+ # {https://www.i18next.com/overview/api#language language},
22
+ # {https://www.i18next.com/overview/api#languages languages},
23
+ # {https://www.i18next.com/overview/api#loadNamespaces loadNamespaces},
24
+ # {https://www.i18next.com/overview/api#resolvedLanguage resolvedLanguage},
25
+ # {https://www.i18next.com/overview/api#setDefaultNamespace setDefaultNamespace},
26
+ # {https://www.i18next.com/overview/api#t t}, and
27
+ # {https://www.i18next.com/overview/api#use use}.
17
28
  # It also provides method {#import_js_module} for loading {https://www.i18next.com/overview/plugins-and-utils i18next plugins}.
18
29
  class I18next
19
30
 
31
+ # Each I18next instance has its own i18next Javascript module
32
+ def initialize
33
+ @i18next = `i18next.createInstance()`
34
+ end
35
+
20
36
  # Imports a JavaScript module (ESM)
21
37
  #
22
38
  # Use this method to import {https://www.i18next.com/overview/plugins-and-utils i18next JavaScript plugins}
@@ -44,16 +60,16 @@ require "native"
44
60
  # @param js_module a plugin's JavaScript module that was imported
45
61
  # by method {#import_js_module}
46
62
  def use(js_module)
47
- `i18next.use(js_module.default)`
63
+ `#{@i18next}.use(js_module.default)`
48
64
  end
49
65
 
50
66
  # Initializes {https://www.i18next.com/overview/api#init i18next}
51
- # @param options [Hash] a hash with keys matching the {https://www.i18next.com/overview/configuration-options i18next options}
67
+ # @param options [Hash] a hash with keys matching the {https://www.i18next.com/overview/configuration-options i18next options}.
52
68
  # @return [Promise] a promise that resolves when i18next has been initialized
53
- def init(options)
69
+ def init(options = {})
54
70
  promise = Promise.new
55
71
  `
56
- i18next.init(#{options.to_n})
72
+ #{@i18next}.init(#{options.to_n})
57
73
  .then(
58
74
  t => {
59
75
  promise.$resolve(t);
@@ -69,9 +85,9 @@ require "native"
69
85
  def change_language(language)
70
86
  promise = Promise.new
71
87
  `
72
- i18next.changeLanguage(language).then(
73
- t => {
74
- promise.$resolve(t)
88
+ #{@i18next}.changeLanguage(language).then(
89
+ () => {
90
+ promise.$resolve()
75
91
  });
76
92
  `
77
93
  promise
@@ -79,14 +95,150 @@ require "native"
79
95
 
80
96
  # @return [String] the current {https://www.i18next.com/overview/api#language i18next} language
81
97
  def language
82
- `i18next.language`
98
+ `#{@i18next}.language`
83
99
  end
84
100
 
85
101
  # The {https://www.i18next.com/overview/api#t i18next} translation associated with a key
86
- # @param [String] key a key that references a translation, single key only
87
- # @return [String] the translation associated with the key
88
- def t(key)
89
- `i18next.t(key)`
102
+ # @param [String, Array<String>] key one or more keys that reference translations
103
+ # @param [Hash] options options for formatters, post processors, etc.
104
+ # @return [String] the translation associated with the first key that resolves
105
+ def t(key, options={})
106
+ `#{@i18next}.t(key, #{options.to_n})`
107
+ end
108
+
109
+ # @return [Boolean] true if the key exists
110
+ def exists(key)
111
+ `#{@i18next}.exists(key)`
112
+ end
113
+
114
+ # @private
115
+ def get_fixed_t
116
+ raise 'Not implemented'
117
+ end
118
+
119
+ # @see https://www.i18next.com/overview/api#languages The i18next languages method
120
+ # @return language codes that will be used to look up the translation value
121
+ def languages
122
+ `#{@i18next}.languages`
123
+ end
124
+
125
+ # @see https://www.i18next.com/overview/api#resolvedLanguage The i18next resolvedLanguage method
126
+ # @return the current resolved language
127
+ def resolved_language
128
+ `#{@i18next}.resolvedLanguage`
129
+ end
130
+
131
+ # Loads additional namespaces not defined in init options
132
+ # @param [String, Array<String>] ns one or more namespaces
133
+ # @return [Promise] a promise that resolves when the namespaces have been loaded
134
+ # @see https://www.i18next.com/overview/api#loadNamespaces The i18next loadNamespaces method
135
+ def load_namespaces(ns)
136
+ promise = Promise.new
137
+ `
138
+ #{@i18next}.loadNamespaces(ns)
139
+ .then(
140
+ () => {
141
+ promise.$resolve()
142
+ });
143
+ `
144
+ promise
145
+ end
146
+
147
+ # @private
148
+ def load_languages(*lngs)
149
+ raise 'Not implemented'
150
+ end
151
+
152
+ # @private
153
+ def reload_resources
154
+ raise 'Not implemented'
155
+ end
156
+
157
+ # Changes the default namespace.
158
+ # @param ns [String] new default namespace
159
+ # @see https://www.i18next.com/overview/api#setDefaultNamespace The i18next setDefaultNamespace method
160
+ def set_default_namespace(ns)
161
+ `#{@i18next}.setDefaultNamespace(ns)`
162
+ end
163
+
164
+ # Get a language's reading direction
165
+ # @param lng [String] the language; if omitted, the current language is used
166
+ # @return "ltr" or "rtl"
167
+ # @see https://www.i18next.com/overview/api#dir The i18next dir method
168
+ def dir(lng)
169
+ `#{@i18next}.dir(lng)`
90
170
  end
171
+
172
+ # @private
173
+ def format(data, format, lng)
174
+ raise 'Not implemented'
175
+ end
176
+
177
+ # @private
178
+ def create_instance(options)
179
+ raise 'Not implemented'
180
+ end
181
+
182
+ # @private
183
+ def clone_instance(options)
184
+ raise 'Not implemented'
185
+ end
186
+
187
+ # @private
188
+ def off
189
+ raise 'Not implemented'
190
+ end
191
+
192
+ # @private
193
+ def on
194
+ raise 'Not implemented'
195
+ end
196
+
197
+ # Gets one value by given key.
198
+ # @see https://www.i18next.com/overview/api#getResource The i18next getResource method
199
+ def get_resource(lng, ns, key, options = {})
200
+ `#{@i18next}.getResource(lng, ns, key, options)`
201
+ end
202
+
203
+ # Adds one key/value.
204
+ # @see https://www.i18next.com/overview/api#addResource The i18next addResource method
205
+ def add_resource(lng, ns, key, value, options = {})
206
+ `#{@i18next}.addResource(lng, ns, key, value, options)`
207
+ end
208
+
209
+ # Adds multiple key/values.
210
+ # @param resources [Hash] key/value pairs
211
+ # @see https://www.i18next.com/overview/api#addResources The i18next addResources method
212
+ def add_resources(lng, ns, resources)
213
+ `#{@i18next}.addResources(lng, ns, #{resources.to_n})`
214
+ end
215
+
216
+ # @private
217
+ def add_resource_bundle(lng, ns, resouces, deep, overwrite)
218
+ raise 'Not implemented'
219
+ end
220
+
221
+ # @private
222
+ def has_resource_bundle(lng, ns)
223
+ raise 'Not implemented'
224
+ end
225
+
226
+ # @private
227
+ def get_data_by_language(lng)
228
+ raise 'Not implemented'
229
+ end
230
+
231
+ # Gets a resource bundle.
232
+ # @return [Hash] key/value pairs
233
+ # @see https://www.i18next.com/overview/api#getResourceBundle The i18next getResourceBundle method
234
+ def get_resource_bundle(lng, ns)
235
+ JSON.parse(`JSON.stringify(#{@i18next}.getResourceBundle(lng, ns))`)
236
+ end
237
+
238
+ # @private
239
+ def remove_resource_bundle(lng, ns)
240
+ raise 'Not implemented'
241
+ end
242
+
91
243
  end
92
244
  end
@@ -1,3 +1,3 @@
1
1
  module I18next
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
data/opal-i18next.gemspec CHANGED
@@ -6,8 +6,10 @@ Gem::Specification.new do |spec|
6
6
  spec.summary = "An Opal wrapper for the JavaScript i18next module."
7
7
  spec.description = <<~DESC
8
8
  A basic Opal wrapper for the JavaScript i18next module that supports methods
9
- init, use, t, changeLanguage, and language. Is also provides method
10
- import_js_module for loading i18next plugins.
9
+ addResource, addResources, changeLanguage, dir, exists, getResource,
10
+ getResourceBundle, init, language, languages, loadNamespaces, resolvedLanguage,
11
+ t, and use. It also provides method import_js_module for
12
+ loading i18next plugins.
11
13
  DESC
12
14
  spec.authors = ["Larry North"]
13
15
 
@@ -22,6 +24,7 @@ Gem::Specification.new do |spec|
22
24
  }
23
25
 
24
26
  spec.files += Dir["*.gemspec"]
27
+ spec.files += Dir[".yardopts"]
25
28
  spec.files += Dir["lib/**/*"]
26
29
  spec.files += Dir["opal/**/*"]
27
30
  spec.require_paths = ["lib"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-i18next
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Larry North
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-23 00:00:00.000000000 Z
11
+ date: 2022-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal
@@ -26,14 +26,17 @@ dependencies:
26
26
  version: 1.5.0
27
27
  description: |
28
28
  A basic Opal wrapper for the JavaScript i18next module that supports methods
29
- init, use, t, changeLanguage, and language. Is also provides method
30
- import_js_module for loading i18next plugins.
29
+ addResource, addResources, changeLanguage, dir, exists, getResource,
30
+ getResourceBundle, init, language, languages, loadNamespaces, resolvedLanguage,
31
+ t, and use. It also provides method import_js_module for
32
+ loading i18next plugins.
31
33
  email:
32
34
  - lnorth@swnorth.com
33
35
  executables: []
34
36
  extensions: []
35
37
  extra_rdoc_files: []
36
38
  files:
39
+ - ".yardopts"
37
40
  - lib/opal-i18next.rb
38
41
  - lib/opal/i18next.rb
39
42
  - opal-i18next.gemspec