i18n-light 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/i18n-light.rb +58 -0
  3. metadata +43 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 89d497f27c0126e6b1368a1493d41d8a61e75b1522ff903f98d59ebd431a86a9
4
+ data.tar.gz: 714ba9302f59f40944a876d0f41a70d34f8e3c73ba8380671afc34607fefb120
5
+ SHA512:
6
+ metadata.gz: '09cd62c03ad589d5f01b58f79dbf726a6705bfe6a3945e3cd61116b0f7877b1a3de0958d5ce5753f2fa9f8f1287b28a37780223e619ffe00604e31b7e68edb1d'
7
+ data.tar.gz: b12de18d004c32657355f06b16555cebffea0b4c6c69ee99958d4ca2123583f8f2cae3654d17d9b6543e5e9930f32ba21fae6290478453c944741d2c203b1a4e
@@ -0,0 +1,58 @@
1
+ # Exception to handle unknown locale
2
+ class UnknownLocale < StandardError
3
+ end
4
+
5
+ # i18n class, the center of the program
6
+ class I18n
7
+
8
+ # @return [String, Symbol] the locale selection
9
+ attr_accessor :locale
10
+
11
+ # Initialize the i18n class
12
+ #
13
+ # @param strings [Hash] all the strings
14
+ # @param fallback_locale [Strings, Symbol] the locale to use if any is set
15
+ # @param locale [Strings, Symbol, nil] set a locale (optional)
16
+ # @param warning [Boolean] enable warnings during runtime (default: false)
17
+ def initialize strings, fallback_locale, locale = nil, warnings = false
18
+ @warnings = warnings
19
+ @strings = strings
20
+ @fallback_locale = fallback_locale
21
+ end
22
+
23
+ # Get a string in the configured locale
24
+ #
25
+ # @param key [String, Symbol] key of the string to return
26
+ # @param subkey [String, Symbol, nil] subkey for categories (optional)
27
+ # @param params [Hash, nil] params for formatting
28
+ # @return the requested key
29
+ def t key, subkey = nil, params = {}
30
+
31
+ # locale selection
32
+ locale = @locale.nil? ? @fallback_locale : @locale
33
+ t = @strings[locale]
34
+ if t.nil?
35
+ raise Unknownlocale, "[i18n] error : unknown locale"
36
+ end
37
+
38
+ # key selection
39
+ t = t[key]
40
+ if t.nil?
41
+ puts "[i18n] warning : unknown key #{key}" if @warnings
42
+ return
43
+ end
44
+
45
+ # subkey selection (if exists)
46
+ t = t[subkey] unless subkey.nil?
47
+
48
+ # formatting parameters
49
+ params.each do |name, value|
50
+ puts "[i18n] warning : unknown parameter #{name}" if @warnings && t !~ /{#{name}}/
51
+ t.sub!("{#{name}}", value)
52
+ end
53
+ puts "[i18n] warning : unfilled formatting parameters" if @warnings && t =~ /{.+}/
54
+
55
+ # returning the string
56
+ t
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n-light
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Exybore
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-02-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A light implementation to add internationalization into your apps.
14
+ email: exybore@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/i18n-light.rb
20
+ homepage: https://github.com/exybore/i18n-light
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.0.1
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Light implementation of i18n.
43
+ test_files: []