opal-i18next 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c6e23990c8b1a275471ba5880ea7b75db97794620ef037d28b411c21986b1d6d
4
+ data.tar.gz: 1ef817b85846c9bb8b7a93321aef9bf15a637bec76947ca6362579ec497cb94b
5
+ SHA512:
6
+ metadata.gz: 4288938af6057505cc90f478ff7c366e0a95be8a35257729800c1480b814ffc5f88e8d86b9e3a8a2e0e014933a0e85bbc14b97fba16f578f251d3019d2fb36a6
7
+ data.tar.gz: ad2988876b8b3dafd899da0949d0380280d85ff564d52eb3be1b187a12d0cd2bd02b69606e48e71e47476f0177cb8750bd256312eed74653ae364f55d80131cc
@@ -0,0 +1,3 @@
1
+ require 'opal'
2
+
3
+ Opal.append_path File.expand_path("../../../opal", __FILE__).untaint
@@ -0,0 +1 @@
1
+ require 'opal/i18next'
@@ -0,0 +1,92 @@
1
+ require "opal"
2
+ require "native"
3
+
4
+ # Uses PromiseV2 if present
5
+ # @see https://opalrb.com/docs/guides/v1.5.1/async PromiseV2
6
+ module I18next
7
+
8
+ Promise = defined?(PromiseV2) ? PromiseV2 : ::Promise
9
+
10
+ # {I18next} is a basic wrapper around the JavaScript {https://www.i18next.com i18next module}.
11
+ #
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}.
17
+ # It also provides method {#import_js_module} for loading {https://www.i18next.com/overview/plugins-and-utils i18next plugins}.
18
+ class I18next
19
+
20
+ # Imports a JavaScript module (ESM)
21
+ #
22
+ # Use this method to import {https://www.i18next.com/overview/plugins-and-utils i18next JavaScript plugins}
23
+ # that can be passed to the {#use} method.
24
+ #
25
+ # @param module_path [String] the path to the module's *.js file, may be a
26
+ # file path or a URL
27
+ #
28
+ # @return [Promise] a promise that resolves to the JavaScript module that can be passed
29
+ # to the {#use} method
30
+ #
31
+ def import_js_module(module_path)
32
+ promise = Promise.new
33
+ `
34
+ import(module_path).then(
35
+ module => {
36
+ promise.$resolve(module);
37
+ });
38
+ `
39
+ promise
40
+ end
41
+
42
+ # Loads an {https://www.i18next.com/overview/api#use i18next} plugin
43
+ #
44
+ # @param js_module a plugin's JavaScript module that was imported
45
+ # by method {#import_js_module}
46
+ def use(js_module)
47
+ `i18next.use(js_module.default)`
48
+ end
49
+
50
+ # 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}
52
+ # @return [Promise] a promise that resolves when i18next has been initialized
53
+ def init(options)
54
+ promise = Promise.new
55
+ `
56
+ i18next.init(#{options.to_n})
57
+ .then(
58
+ t => {
59
+ promise.$resolve(t);
60
+ });
61
+ `
62
+ promise
63
+ end
64
+
65
+ # Changes the {https://www.i18next.com/overview/api#changelanguage i18next} language
66
+ # @param language [String] the new language
67
+ # @return [Promise] a promise that resolves when the language's
68
+ # translations have been loaded
69
+ def change_language(language)
70
+ promise = Promise.new
71
+ `
72
+ i18next.changeLanguage(language).then(
73
+ t => {
74
+ promise.$resolve(t)
75
+ });
76
+ `
77
+ promise
78
+ end
79
+
80
+ # @return [String] the current {https://www.i18next.com/overview/api#language i18next} language
81
+ def language
82
+ `i18next.language`
83
+ end
84
+
85
+ # 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)`
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,3 @@
1
+ module I18next
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'i18next/i18next'
2
+ require 'i18next/version'
@@ -0,0 +1,32 @@
1
+ require_relative "opal/i18next/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "opal-i18next"
5
+ spec.version = I18next::VERSION
6
+ spec.summary = "An Opal wrapper for the JavaScript i18next module."
7
+ spec.description = <<~DESC
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.
11
+ DESC
12
+ spec.authors = ["Larry North"]
13
+
14
+ spec.license = "MIT"
15
+ spec.email = ["lnorth@swnorth.com"]
16
+ spec.homepage = "https://github.com/LarryNorth/opal-i18next"
17
+ spec.metadata = {
18
+ "bug_tracker_uri" => "https://github.com/LarryNorth/opal-i18next/issues",
19
+ "documentation_uri" => "https://www.rubydoc.info/gems/opal-i18next",
20
+ "homepage_uri" => spec.homepage,
21
+ "source_code_uri" => "https://github.com/LarryNorth/opal-i18next"
22
+ }
23
+
24
+ spec.files += Dir["*.gemspec"]
25
+ spec.files += Dir["lib/**/*"]
26
+ spec.files += Dir["opal/**/*"]
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.required_ruby_version = ">= 3.1"
30
+
31
+ spec.add_dependency 'opal', '~> 1.5.0'
32
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opal-i18next
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Larry North
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-08-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: opal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.5.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.5.0
27
+ description: |
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.
31
+ email:
32
+ - lnorth@swnorth.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/opal-i18next.rb
38
+ - lib/opal/i18next.rb
39
+ - opal-i18next.gemspec
40
+ - opal/i18next/i18next.rb
41
+ - opal/i18next/version.rb
42
+ - opal/opal-i18next.rb
43
+ homepage: https://github.com/LarryNorth/opal-i18next
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ bug_tracker_uri: https://github.com/LarryNorth/opal-i18next/issues
48
+ documentation_uri: https://www.rubydoc.info/gems/opal-i18next
49
+ homepage_uri: https://github.com/LarryNorth/opal-i18next
50
+ source_code_uri: https://github.com/LarryNorth/opal-i18next
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '3.1'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.3.7
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: An Opal wrapper for the JavaScript i18next module.
70
+ test_files: []