simple_locize 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/simple_locize.rb +65 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c561fef26d9e0f91768f8237adab9ad8a1832f3d163979171a076f86304db557
4
+ data.tar.gz: d35523ac889d05dd6d7a3cac6fc21f812e94ebc752a48a3f785be5d97b3a7504
5
+ SHA512:
6
+ metadata.gz: 7bd81fd5ffed3c6af8475fcb9b25f7850455cd148257c9df2c42c07a415f374ff2bddba3f142ead600231ca936c316f5a5ae7459e30c1fec033aeea6c813d769
7
+ data.tar.gz: 0e1c5a7eba4a4105135fc61c30c38353c6f50db22297225b775f5510c88c9b641679b820c9c92eb3816560472c3e9a85d8418e6e3ba2abb830eb6627c724f83f
@@ -0,0 +1,65 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ class SimpleLocize
5
+ def initialize(project_id, environment, private_key = nil)
6
+ @translations = {}
7
+ @project_id = project_id
8
+ @environment = environment
9
+ @private_key = private_key
10
+ end
11
+
12
+ def get_all_translations_from_namespace(namespace, language)
13
+ if !@translations.key?(namespace) || !@translations[namespace].key?(language.to_sym)
14
+ fetch_from_locize(namespace, language)
15
+ end
16
+
17
+ return @translations[namespace][language.to_sym]
18
+ end
19
+
20
+ def translate(namespace, language, key)
21
+ if !@translations.key?(namespace) || !@translations[namespace].key?(language.to_sym)
22
+ fetch_from_locize(namespace, language)
23
+ end
24
+
25
+ key_split = key.split('.')
26
+
27
+ key_value = if key_split.length == 1
28
+ @translations[namespace][language.to_sym][key] rescue nil
29
+ else
30
+ key_split.reduce(@translations[namespace][language.to_sym] || {}) do |acc, item|
31
+ acc[item] rescue Hash.new
32
+ end
33
+ end
34
+
35
+ return key_value.is_a?(String) ? key_value : key
36
+ end
37
+
38
+ private
39
+
40
+ def fetch_from_locize(namespace, language)
41
+ begin
42
+ uri = URI("https://api.locize.io/#{@private_key.nil? ? '' : 'private/'}#{@project_id}/#{@environment}/#{language}/#{namespace}")
43
+
44
+ headers = {}
45
+ headers['Authorization'] = @private_key unless @private_key.nil?
46
+
47
+ locize_fetch = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
48
+ request = Net::HTTP::Get.new(uri)
49
+
50
+ headers.each { |key, value| request[key] = value }
51
+
52
+ http.request(request)
53
+ end
54
+
55
+ locize_data = JSON.parse(locize_fetch.body)
56
+
57
+ @translations[namespace] = {
58
+ **(@translations[namespace] || {}),
59
+ language.to_sym => locize_data,
60
+ }
61
+ rescue
62
+ # fail
63
+ end
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_locize
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Maxamilian Demian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-05-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A lightweight library for handling translations via the Locize API
14
+ email:
15
+ - max@maxdemian.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/simple_locize.rb
21
+ homepage: https://github.com/Maxoplata/simple-locize-ruby
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.4.12
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Locize API library
44
+ test_files: []