slang 0.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a22deccd66088233b3e7c232af09e47f60fffa16
4
+ data.tar.gz: 0147bf242fb7c0f73019e4ae99990d7ac422b16e
5
+ SHA512:
6
+ metadata.gz: 135700c2e7e3fec39a7a0a71a046e3ce9032086358da8d081f429624c7737154b9cbff3c9e5329cba010f185c05f76cee10f98e5cb440b487cd5e20a415f8068
7
+ data.tar.gz: 71ad3325e0facdab9ec32b99e1258fda08256814199983fe44e415b41df35a03ed39c6ff3de515346f1661c3a0667f8775a74deeda9242e7a0c5c26ab188dab5
@@ -0,0 +1,6 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ # use a global ignore for editor/OS files: git config --global core.excludesfile '~/.gitignore_global'
3
+
4
+ log/
5
+ pkg/
6
+ slang_data/
@@ -0,0 +1,9 @@
1
+ # Slang Ruby Client Release Notes
2
+
3
+ This project adheres to [Semantic Versioning](http://semver.org/). For any release 0.x, all releases 0.x.y will be
4
+ backwards-compatible with non-breaking changes. Once we reach 1.0.0, all 1.x.y releases will be backwards-compatible
5
+ with non-breaking changes to 1.0.0.
6
+
7
+ ## 0.34.0 (2015-05-14)
8
+
9
+ * Initial release. (@sickp)
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+ gemspec
3
+
4
+ group :development do
5
+ gem "minitest"
6
+ gem "pry"
7
+ gem "yard"
8
+ end
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ slang (0.34.0)
5
+ oj (~> 2.12)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ coderay (1.1.0)
11
+ method_source (0.8.2)
12
+ minitest (5.6.1)
13
+ oj (2.12.7)
14
+ pry (0.10.1)
15
+ coderay (~> 1.1.0)
16
+ method_source (~> 0.8.1)
17
+ slop (~> 3.4)
18
+ slop (3.6.0)
19
+ yard (0.8.7.6)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ minitest
26
+ pry
27
+ slang!
28
+ yard
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014-2015 Slang Tech Inc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,242 @@
1
+ # slang-rb
2
+
3
+ The official Ruby client library for [Slang℠][slang-home] -- https://getslang.com/
4
+
5
+ Kick-ass translation and content management.
6
+
7
+
8
+ ## Getting Started
9
+
10
+ If you are using Bundler, add the Slang gem to your `Gemfile`:
11
+
12
+ ```ruby
13
+ gem "slang", "~> 0.34.0" # accept all 0.34.x releases
14
+ ```
15
+
16
+ Then use Bundler to install:
17
+
18
+ ```sh
19
+ bundle install
20
+ ```
21
+
22
+ If you're not using Bundler, just install like any other gem:
23
+
24
+ ```sh
25
+ gem install slang
26
+ ```
27
+
28
+ Next, you'll want to visit [GetSlang.com][slang-home] (if you haven't already) to obtain a **project key** and a
29
+ **developer key**. A project key uniquely identifies your project and its translations. It is safe to check
30
+ `project_key` into your code base where any developer can see it. However, your developer key is unique to you and
31
+ should be kept as a secret.
32
+
33
+
34
+ ## Basic Usage
35
+
36
+ Slang must be initialized when your application starts, before any content can be retrieved. In Rails, you would create
37
+ a `config/initializers/slang.rb` like:
38
+
39
+ ```ruby
40
+ Slang.ify(project_key: "...")
41
+ ```
42
+
43
+ At a minimum, you'll need to specify a `project_key`. For development mode, you'll need to make your `developer_key`
44
+ available as well. We recommend setting the environment variable `SLANG_DEVELOPER_KEY` in `~/.bashrc`, for example:
45
+
46
+ ```sh
47
+ export SLANG_DEVELOPER_KEY="..."
48
+ ```
49
+
50
+ If you're a lone developer (or security schmecurity!), you can also pass it directly to the `Slang.ify` initialization
51
+ method.
52
+
53
+ After Slang is initialized, all cached/embedded content and translations are immediately available. Slang will also
54
+ start a lightweight, background thread to periodically check for updates.
55
+
56
+ Slang adds an instance method `#t` to Ruby's `String` and `Symbol` classes to provide some sweet syntactic sugar:
57
+
58
+ ```ruby
59
+ :hello.t # => "Hello"
60
+ "homepage.greeting".t(name: "Adrian") # => "Welcome Adrian!"
61
+ ```
62
+
63
+ This is equivalent to calling `Slang.t` directly, like this:
64
+
65
+ ```ruby
66
+ Slang.t(:hello) # => "Hello"
67
+ Slang.t("homepage.greeting", name: "Adrian") # => "Welcome Adrian!"
68
+ ```
69
+
70
+ If your content is available in different languages, set the appropriate locale code with `Slang.locale_code=`:
71
+
72
+ ```ruby
73
+ Slang.locale_code = :de # in German (where available)
74
+ "hello".t # => "Hallo"
75
+ "homepage.greeting".t(name: "Boris") # => "Willkommen Boris!"
76
+ ```
77
+
78
+ In Rails, the `#t` instance method is automatically added to all `ActionView` and `ActionController` instances as well.
79
+
80
+
81
+ ## Ruby SDK API
82
+
83
+ Slang's Ruby SDK API is documented below. Please feel free to check out the internals (and suggest improvements), but
84
+ do not depend on the implementation details in your code! They will most likely change as Slang evolves. If you stick
85
+ to the public API, your code will not break, as this project adheres to [Semantic Versioning 2.0.0][semver-home].
86
+
87
+ ###### `Slang.ify(config={})`
88
+
89
+ Initializes Slang with the given configuration hash (see the next section below). This must be called before any other
90
+ methods, even if `config` is empty. Keys must be symbols, values must be strings.
91
+
92
+ ###### `Slang.t(key, variable_hash={})`
93
+
94
+ Fetch the translation for the given key name. The second parameter is a hash mapping variable names to their values.
95
+ This is only necessary if the given key requires variables for interpolation, pluralization, or gender rules. `key` and
96
+ any variable names are converted to lowercase strings (and are thus treated case-insensitively). A variable's value
97
+ must be convertible to a String.
98
+
99
+ In *Development mode*, if `key` is not found, a placeholder string derived from the key name (and passed variables) is
100
+ returned. The missing key is reported back to the Slang service in the background.
101
+
102
+ In *Production mode*, if `key` is not found, a placeholder string of the form `{{locale_code:key}}` is returned to
103
+ indicate this issue. Hopefully, with proper testing, this will never happen. But if your users complain about curly
104
+ braces, that might be the reason. Fortunately, the easy fix is to add the translation and push a new snapshot from
105
+ [GetSlang.com][slang-home].
106
+
107
+ ###### `Slang.locale_code`
108
+
109
+ Returns the current locale code (i.e. language, language + region), a symbol.
110
+
111
+ ###### `Slang.locale_code=(locale_code)`
112
+
113
+ Sets the locale code for the current Thread or Fiber. Converts to a symbol.
114
+
115
+ ###### `Slang.locale_codes`
116
+
117
+ Returns an array of locale codes (symbols) available in the current snapshot.
118
+
119
+ ###### `Slang.logger`
120
+
121
+ Returns the Slang logger (if set). If Rails is detected when Slang initializes, it will use `Rails.logger` by default.
122
+
123
+ ###### `Slang.logger=(logger)`
124
+
125
+ Assign a Logger-like object for Slang logging.
126
+
127
+ ###### `Slang.keys`
128
+
129
+ Returns an array of keys (strings) available in the current snapshot.
130
+
131
+ ###### `Slang::VERSION` (string constant)
132
+
133
+ A string constant representing the current slang-rb version.
134
+
135
+ ###### `Slang::SlangError` (class)
136
+
137
+ The base class for all Slang errors. This is only raised due to a configuration or initialization programming error,
138
+ and should not be caught.
139
+
140
+ ###### `Slang::Helpers` (module)
141
+
142
+ This mix-in module provides the methods `t`, `locale_code`, and `locale_code=`. You can extend your own module or class
143
+ to create module/class methods. This is how the main Slang module does it:
144
+
145
+ ```ruby
146
+ module Slang
147
+ extend Helpers # provides Slang.t, Slang.locale_code, Slang.locale_code=
148
+ ...
149
+ end
150
+ ```
151
+
152
+ Or, you can include this module in a class to get instance methods:
153
+
154
+ ```ruby
155
+ class View
156
+ include Slang::Helpers # provides t, locale_code, locale_code=
157
+ ...
158
+ end
159
+ ```
160
+
161
+ *Note: if you are using Rails, this is automagically done for your controllers and views.*
162
+
163
+ ###### `String#t` and `Symbol#t` (instance method)
164
+
165
+ The instance method `#t` is added to the `String` and `Symbol` classes. This is identical to calling `Slang.t` with
166
+ the given string or symbol.
167
+
168
+
169
+ ## Slang.ify Configuration
170
+
171
+ The Slang initialization method `Slang.ify` takes a configuration hash with symbolic keys for configuration options.
172
+
173
+ | Configuration Key | Description |
174
+ |:---------------------|:------------------------------------------------------|
175
+ | `:data_dir` | Slang data directory for state and snapshots. |
176
+ | `:developer_key` | Unique developer key / secret for development mode. |
177
+ | `:embedded_snapshot` | Path to an embedded snapshot file. |
178
+ | `:env` | Environment, typically `production` or `development`. |
179
+ | `:project_key` | Unique project identifier. |
180
+
181
+ ##### `:data_dir`
182
+
183
+ Slang's data directory should reside on permanent storage, as state information and cached snapshots (content /
184
+ translations) should persist between application and server restarts. This directory can be safely shared across
185
+ multiple worker processes on the same machine (possibly multiple machines if your NFS-Ninja is well-versed in the
186
+ black arts). Do not check this directory into your version control system.
187
+
188
+ Slang determines its data directory by:
189
+
190
+ 1. Using `:data_dir` if passed to `Slang.ify`.
191
+ 2. Checking the environment variable `SLANG_DATA_DIR`.
192
+ 3. Detecting Rails, and creating `slang_data` under `Rails.root`.
193
+ 4. Creating `slang_data` under the current work directory.
194
+
195
+ ##### `:developer_key`
196
+
197
+ A developer key is a developer-specific secret that provides access-control and allows your application to run in
198
+ *Development mode*. This value should not be checked into version control or disclosed to others. That said, developer
199
+ keys can be invalidated and re-issued. Not that you would ever need that.
200
+
201
+ If `:developer_key` is not specified explicitly, Slang checks if the environment variable `SLANG_DEVELOPER_KEY` is set.
202
+
203
+ ##### `:embedded_snapshot`
204
+
205
+ Upon initialization, Slang will look for the latest cached snapshot in its data directory. If this is a fresh
206
+ production deploy to a new machine, there may be none. In this case, it is useful to embed a snapshot in your release
207
+ so that Slang has data immediately available. Otherwise, your users may see missing content/translations until the
208
+ background update thread has a chance to pull down the latest snapshot (this typically takes only a second or two,
209
+ which makes embedded snapshots less important in *Development mode*).
210
+
211
+ If `:embedded_snapshot` is not specified explicitly, Slang checks if the environment variable `SLANG_EMBEDDED_SNAPSHOT`
212
+ is set.
213
+
214
+ ##### `:env`
215
+
216
+ Slang can run in either *Production mode* or *Development mode* (see [GetSlang.com][slang-home] for more details). If
217
+ this value starts with `dev` (case-insensitive), Slang expects to start in *Developer mode* and requires a
218
+ `developer_key`. For example, values of `development`, `dev`, `DEV` would run in *Development mode*, where as values of
219
+ `production`, `prod`, or `staging` would run in *Production mode*.
220
+
221
+ Slang determines its environment by:
222
+
223
+ 1. Using `:env` if passed to `Slang.ify`.
224
+ 2. Checking the environment variable `SLANG_ENV`.
225
+ 3. Detecting Rails, and using `Rails.env`.
226
+ 4. Check if `:developer_key` is set, if so, assumes `development`.
227
+ 5. Otherwise, assumes `production`.
228
+
229
+ ##### `:project_key`
230
+
231
+ A project key is the unique identifier for your project. It is required.
232
+
233
+ If `:project_key` is not specified explicitly, Slang expects the environment variable `SLANG_PROJECT_KEY` to be set.
234
+
235
+
236
+ ## Support
237
+
238
+ You can find more detailed documentation on [GetSlang.com][slang-home]. If you cannot figure something out or think you
239
+ have found a bug, let us know: support @ getslang.com. We're here to make this work. @sickp
240
+
241
+ [slang-home]: https://getslang.com/
242
+ [semver-home]: http://semver.org/
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ end
7
+
8
+ desc "Start console."
9
+ task :console do
10
+ exec("pry", "-Ilib", "-rslang")
11
+ end
12
+ task default: :console
@@ -0,0 +1,144 @@
1
+ # slang-rb
2
+ #
3
+ # The official Ruby client library for Slang(sm). https://getslang.com/
4
+ #
5
+ # The MIT License (MIT)
6
+ #
7
+ # Copyright (c) 2014-2015 Slang Tech Inc
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
10
+ # this software and associated documentation files (the "Software"), to deal in
11
+ # the Software without restriction, including without limitation the rights to
12
+ # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
13
+ # the Software, and to permit persons to whom the Software is furnished to do so,
14
+ # subject to the following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be included in all
17
+ # copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
21
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
22
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23
+ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ #
26
+ module Slang
27
+ class SlangError < StandardError; end
28
+
29
+ # Slang helpers module.
30
+ #
31
+ module Helpers
32
+
33
+ # Get the content / translation for the given key.
34
+ #
35
+ # In development mode, if a key is not found, a human-readable placeholder is returned (and the missing key is
36
+ # reported to the Slang service).
37
+ #
38
+ # In production mode, if a key is not found, a placeholder string of the form "{{locale_code:key}}" is returned.
39
+ #
40
+ # @param [#to_s] Key name, converted to a lowercase.
41
+ # @param [Hash{#to_s => #to_s}] variable hash (variable names to values)
42
+ # @return [String] translated string (frozen -- dup if mutation is necessary)
43
+ # @raise [SlangError] if not initialized.
44
+ #
45
+ def t(key, variable_hash={})
46
+ Slang.internal_translate(key, variable_hash)
47
+ end
48
+
49
+ # Returns the locale code for the current Thread or Fiber.
50
+ #
51
+ # @return [Symbol] locale code
52
+ #
53
+ def locale_code
54
+ Thread.current[:slang_locale_code] ||= :en
55
+ end
56
+
57
+ # Sets the current locale code (for the current Thread or Fiber).
58
+ #
59
+ # @param [#to_sym] locale code
60
+ #
61
+ def locale_code=(locale_code)
62
+ Thread.current[:slang_locale_code] = locale_code.to_sym
63
+ end
64
+
65
+ end
66
+
67
+ # Initialize Slang. This must be called first before any other methods.
68
+ #
69
+ # @param [Hash{Symbol => String}] configuration keys and values.
70
+ # @raise [SlangError] on initialization failure.
71
+ #
72
+ def self.ify(config={})
73
+ init(config)
74
+ end
75
+
76
+ # Return all the locale codes in the current snapshot.
77
+ #
78
+ # @return [Array<Symbol>] all locale codes in the current snapshot.
79
+ # @raise [SlangError] if not initialized.
80
+ #
81
+ def self.locale_codes
82
+ snapshot.locales.keys
83
+ end
84
+
85
+ # Return all the keys in the current snapshot.
86
+ #
87
+ # @return [Array<String>] all keys in the current snapshot.
88
+ # @raise [SlangError] if not initialized.
89
+ #
90
+ def self.keys
91
+ snapshot.keys
92
+ end
93
+
94
+ # Returns Slang's logger. If Rails is detected when Slang is initialized, this logger will default
95
+ # to Rails.logger.
96
+ #
97
+ # @returns [Logger, nil] current logger.
98
+ #
99
+ def self.logger
100
+ @logger
101
+ end
102
+
103
+ # Set the Logger-like instance Slang should use for logging.
104
+ #
105
+ # @param [Logger, nil] logger
106
+ #
107
+ def self.logger=(logger)
108
+ @logger = logger
109
+ end
110
+
111
+ extend Helpers # provides Slang.t, Slang.locale_code, Slang.locale_code=
112
+ end
113
+
114
+ # Experimental String extension.
115
+ #
116
+ class String
117
+
118
+ # Use this string instance as a key. See Slang.t for details.
119
+ #
120
+ # @param [Hash{#to_s => #to_s}] optional variable hash
121
+ # @return [String] translated string
122
+ # @raise [SlangError] if not initialized.
123
+ #
124
+ def t(variable_hash={})
125
+ Slang.internal_translate(self, variable_hash)
126
+ end
127
+ end
128
+
129
+ # Experimental Symbol extension.
130
+ #
131
+ class Symbol
132
+
133
+ # Use this symbol instance as a key. See Slang.t for details.
134
+ #
135
+ # @param [Hash{#to_s => #to_s}] optional variable hash
136
+ # @return [String] translated string
137
+ # @raise [SlangError] if not initialized.
138
+ #
139
+ def t(variable_hash={})
140
+ Slang.internal_translate(self, variable_hash)
141
+ end
142
+ end
143
+
144
+ require "slang/internal"