minigl 2.3.9 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -3
- data/lib/minigl/localization.rb +79 -0
- data/lib/minigl.rb +1 -0
- data/test/data/text/english.txt +6 -0
- data/test/data/text/portuguese.txt +6 -0
- data/test/localization_tests.rb +41 -0
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0cbb7ddb129a7fea30f09ad6d5c89b3cef8b9b9b83d5f7f9eeacc76b09227ca
|
4
|
+
data.tar.gz: 7297a8c63e1df8cf85d6d61ccfa4fd80cdf8676dc06fe8467aca522ab10bbe38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ceda66b6723094d415a11d23cd3c0c5b69a496fe655f7f7a72cb7298bd5606f38d8755d9868a7e878e6a27a56350c2dbe9e513e4d3c247c7ac504a9356bd9b5
|
7
|
+
data.tar.gz: 8d1dd2b962d597547d54f51e18b2332ec201bbbca19f4fc1d3a584b52bb979937366c9f11719b05ce07fba43aade40137df270530e60935f56cc99b9b36d052f
|
data/README.md
CHANGED
@@ -37,10 +37,9 @@ After installing the Gosu dependencies, you can just `gem install minigl`.
|
|
37
37
|
* The [wiki](https://github.com/victords/minigl/wiki) is a work in progress with tutorials and examples.
|
38
38
|
* Test package and examples aren't complete!
|
39
39
|
|
40
|
-
## Version 2.
|
40
|
+
## Version 2.4.0
|
41
41
|
|
42
|
-
*
|
43
|
-
* Allow combining `center` and `margin` for `Button` text.
|
42
|
+
* Added the `Localization` class! Check out [the documentation](http://www.rubydoc.info/gems/minigl/MiniGL/Localization) to learn more.
|
44
43
|
|
45
44
|
## Contributing
|
46
45
|
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module MiniGL
|
2
|
+
# This class provides methods to easily retrieve string translations from
|
3
|
+
# text files.
|
4
|
+
class Localization
|
5
|
+
class << self
|
6
|
+
# The list of available languages. These are symbols corresponding to the
|
7
|
+
# names of the files in data/text, without the '.txt' extension.
|
8
|
+
attr_reader :languages
|
9
|
+
|
10
|
+
# The current language. It's a symbol corresponding to the name of the
|
11
|
+
# file in data/text for that language, without the '.txt' extension.
|
12
|
+
attr_reader :language
|
13
|
+
|
14
|
+
# Initializes the localization system. If you're using a custom
|
15
|
+
# +Res.prefix+, call this _after_ setting it.
|
16
|
+
#
|
17
|
+
# The localization system will look for files with extension '.txt' in
|
18
|
+
# the <code>[Res.prefix]/data/text</code> folder. In each file,
|
19
|
+
# each string should be specified in one line, with the following format:
|
20
|
+
#
|
21
|
+
# <code>identifier content content content...</code>
|
22
|
+
#
|
23
|
+
# Use tab characters between the identifier and the text, not white
|
24
|
+
# spaces. This makes it easier to make all the texts aligned and is
|
25
|
+
# required for the localization system to work. The identifiers will be
|
26
|
+
# used as symbols when retrieving strings.
|
27
|
+
#
|
28
|
+
# The text contents support placeholders, i.e., markers that can be
|
29
|
+
# replaced by arguments you pass to +Localization.text+. To specify a
|
30
|
+
# placeholder, simply use the '$' character. For example, if your string
|
31
|
+
# is:
|
32
|
+
#
|
33
|
+
# <code>my_string Values: $ and $</code>
|
34
|
+
#
|
35
|
+
# the call <code>Localization.text(:my_string, 'test', 10)</code> will
|
36
|
+
# result in "Values: test and 10."
|
37
|
+
#
|
38
|
+
# To include a literal '$'
|
39
|
+
# in the text, use '\\$' (without the quotes). Similarly, use '\\\\' to
|
40
|
+
# represent a literal backslash, and just '\\' to represent a line break
|
41
|
+
# (i.e. a "\\n" in the resulting string).
|
42
|
+
def initialize
|
43
|
+
@languages = []
|
44
|
+
@texts = {}
|
45
|
+
files = Dir["#{Res.prefix}text/*.txt"].sort
|
46
|
+
files.each do |f|
|
47
|
+
lang = f.split('/')[-1].chomp('.txt').to_sym
|
48
|
+
@languages << lang
|
49
|
+
@texts[lang] = {}
|
50
|
+
File.open(f).each do |l|
|
51
|
+
parts = l.split("\t")
|
52
|
+
@texts[lang][parts[0].to_sym] = parts[-1].chomp
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
@language = @languages[0]
|
57
|
+
end
|
58
|
+
|
59
|
+
# Sets the current language. +value+ must be a symbol corresponding to
|
60
|
+
# the name of a file in data/text, without the '.txt' extension.
|
61
|
+
def language=(value)
|
62
|
+
raise "Can't set to invalid language #{value}" unless @languages.include?(value)
|
63
|
+
|
64
|
+
@language = value
|
65
|
+
end
|
66
|
+
|
67
|
+
# Retrieves the string identified by +id+ in the current language.
|
68
|
+
#
|
69
|
+
# See +Localization.initialize+ for details on how to use +args+.
|
70
|
+
def text(id, *args)
|
71
|
+
value = @texts[@language][id] || '<MISSING STRING>'
|
72
|
+
args.each do |arg|
|
73
|
+
value = value.sub(/(^|[^\\])\$/, "\\1#{arg}")
|
74
|
+
end
|
75
|
+
value.gsub('\\$', '$').gsub(/\\(.|$)/) { |m| m[1] == '\\' ? '\\' : "\n#{m[1]}" }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/lib/minigl.rb
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require_relative '../lib/minigl'
|
3
|
+
include MiniGL
|
4
|
+
|
5
|
+
class LocalizationTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
Res.prefix = File.expand_path(File.dirname(__FILE__)) + '/data'
|
8
|
+
Localization.initialize
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_languages
|
12
|
+
assert_equal([:english, :portuguese], Localization.languages)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_language
|
16
|
+
assert_equal(:english, Localization.language)
|
17
|
+
Localization.language = :portuguese
|
18
|
+
assert_equal(:portuguese, Localization.language)
|
19
|
+
assert_raise { Localization.language = :invalid }
|
20
|
+
assert_equal(:portuguese, Localization.language)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_strings
|
24
|
+
assert_equal('Common string.', Localization.text(:str1))
|
25
|
+
assert_equal('String with $ dollar sign.', Localization.text(:str2))
|
26
|
+
assert_equal('something should be replaced.', Localization.text(:str3, 'something'))
|
27
|
+
assert_equal('Should replace both 5 and true.', Localization.text(:str4, 5, true))
|
28
|
+
assert_equal('String with \\ backslash.', Localization.text(:str5))
|
29
|
+
assert_equal("String with\nline breaks.\n", Localization.text(:str6))
|
30
|
+
assert_equal('<MISSING STRING>', Localization.text(:str7))
|
31
|
+
|
32
|
+
Localization.language = :portuguese
|
33
|
+
assert_equal('String comum.', Localization.text(:str1))
|
34
|
+
assert_equal('String com $ cifrão.', Localization.text(:str2))
|
35
|
+
assert_equal('algo deve ser substituído.', Localization.text(:str3, 'algo'))
|
36
|
+
assert_equal('Deve substituir 5 e true.', Localization.text(:str4, 5, true))
|
37
|
+
assert_equal('String com \\ barra invertida.', Localization.text(:str5))
|
38
|
+
assert_equal("String com\nquebras de linha.\n", Localization.text(:str6))
|
39
|
+
assert_equal('<MISSING STRING>', Localization.text(:str7))
|
40
|
+
end
|
41
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minigl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor David Santos
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-03-
|
11
|
+
date: 2022-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: gosu
|
@@ -43,6 +43,7 @@ files:
|
|
43
43
|
- lib/minigl/forms.rb
|
44
44
|
- lib/minigl/game_object.rb
|
45
45
|
- lib/minigl/global.rb
|
46
|
+
- lib/minigl/localization.rb
|
46
47
|
- lib/minigl/map.rb
|
47
48
|
- lib/minigl/movement.rb
|
48
49
|
- lib/minigl/text.rb
|
@@ -70,10 +71,13 @@ files:
|
|
70
71
|
- test/data/img/tile2.svg
|
71
72
|
- test/data/img/tile2b.png
|
72
73
|
- test/data/sound/1.wav
|
74
|
+
- test/data/text/english.txt
|
75
|
+
- test/data/text/portuguese.txt
|
73
76
|
- test/data/tileset/tileset1.png
|
74
77
|
- test/game.rb
|
75
78
|
- test/game_object_tests.rb
|
76
79
|
- test/iso_game.rb
|
80
|
+
- test/localization_tests.rb
|
77
81
|
- test/map_tests.rb
|
78
82
|
- test/mov_game.rb
|
79
83
|
- test/movement_tests.rb
|
@@ -107,6 +111,7 @@ test_files:
|
|
107
111
|
- test/game.rb
|
108
112
|
- test/game_object_tests.rb
|
109
113
|
- test/iso_game.rb
|
114
|
+
- test/localization_tests.rb
|
110
115
|
- test/map_tests.rb
|
111
116
|
- test/mov_game.rb
|
112
117
|
- test/movement_tests.rb
|
@@ -136,5 +141,7 @@ test_files:
|
|
136
141
|
- test/data/img/tile2.svg
|
137
142
|
- test/data/img/tile2b.png
|
138
143
|
- test/data/sound/1.wav
|
144
|
+
- test/data/text/english.txt
|
145
|
+
- test/data/text/portuguese.txt
|
139
146
|
- test/data/tileset/tileset1.png
|
140
147
|
- test/data/img/sub/image.png
|