i18n-env-config 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +53 -0
- data/Rakefile +5 -0
- data/i18n-env-config.gemspec +25 -0
- data/lib/i18n/env/config.rb +51 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 10020fdd2ca858659a6cb6bc1ab86eafd9124c84
|
4
|
+
data.tar.gz: 803844cd9225b2e5dacd8a1d77d4837821858582
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f531d66e1f2a5442f06a15040b7a5a302fec962be8e920efeed8ae357300a5014755a48a394652cf5a69dbc9ac307dc220fea84509f1c9ee588b48cd4e2fbb1
|
7
|
+
data.tar.gz: 344ddc857cde607fbcfddff9edf62c198a2e8471ddcda47bc74c6928571e03822b824f84b05faf1cdfcbc9c4b07f24c8a4ff915469293056ab1d935f8c60b9e1
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Skye Shaw
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all 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,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# I18n::Env::Config
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/sshaw/i18n-env-config.svg)](https://secure.travis-ci.org/sshaw/i18n-env-config)
|
4
|
+
|
5
|
+
Set `I18n.locale` based on the environment variables that control the locale.
|
6
|
+
|
7
|
+
This is for Desktop applications only.
|
8
|
+
For web application see [http_accept_languge](https://github.com/iain/http_accept_language) or [http-accept](https://github.com/ioquatix/http-accept).
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
require "i18n"
|
14
|
+
require "i18n/env/config"
|
15
|
+
|
16
|
+
I18n.config = I18n::Env::Config.new
|
17
|
+
```
|
18
|
+
|
19
|
+
Now `I18n.locale` will be derived from the user's environment.
|
20
|
+
|
21
|
+
## Environment Variables
|
22
|
+
|
23
|
+
The following environment variables are searched, in order:
|
24
|
+
|
25
|
+
1. `LANGUAGE`
|
26
|
+
1. `LC_ALL`
|
27
|
+
1. `LC_MESSAGES`
|
28
|
+
1. `LANG`
|
29
|
+
|
30
|
+
(Just like [gettext](https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html#Locale-Environment-Variables)).
|
31
|
+
|
32
|
+
If an exact match is not found, the list is reevaluated using the locale's parent(s).
|
33
|
+
E.g., if `en_US` is not found, we see if `en` is available.
|
34
|
+
|
35
|
+
## Installation
|
36
|
+
|
37
|
+
Add this line to your application's Gemfile:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
gem "i18n-env-config"
|
41
|
+
```
|
42
|
+
|
43
|
+
And then execute:
|
44
|
+
|
45
|
+
$ bundle
|
46
|
+
|
47
|
+
Or install it yourself as:
|
48
|
+
|
49
|
+
$ gem install i18n-env-config
|
50
|
+
|
51
|
+
## License
|
52
|
+
|
53
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "i18n-env-config"
|
7
|
+
spec.version = "0.0.1"
|
8
|
+
spec.authors = ["Skye Shaw"]
|
9
|
+
spec.email = ["skye.shaw@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Set I18n.locale based on the environment variables that control the locale}
|
12
|
+
spec.description = %q{An I18n config class that looks at the LANGUAGE, LC_ALL, LC_MESSAGES, and LOCALE environment variables to determine I18n.locale}
|
13
|
+
spec.homepage = "https://github.com/sshaw/i18n-env-config"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "i18n", "~> 0.5"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
25
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "i18n"
|
2
|
+
|
3
|
+
module I18n
|
4
|
+
module Env
|
5
|
+
class Config < I18n::Config
|
6
|
+
VERSION = "0.0.1"
|
7
|
+
|
8
|
+
# Order is important
|
9
|
+
VARS = %w[LANGUAGE LC_ALL LC_MESSAGES LANG]
|
10
|
+
|
11
|
+
def locale
|
12
|
+
@locale ||= find_user_locale || super
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def find_user_locale
|
18
|
+
find_primary || find_secondary
|
19
|
+
end
|
20
|
+
|
21
|
+
def find_primary
|
22
|
+
locales.find { |l| I18n.locale_available?(l) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def find_secondary
|
26
|
+
tag = nil
|
27
|
+
|
28
|
+
locales.each do |l|
|
29
|
+
tag = I18n::Locale::Tag.tag(l).parents.map(&:to_sym).find { |parent| I18n.locale_available?(parent) }
|
30
|
+
break if tag
|
31
|
+
end
|
32
|
+
|
33
|
+
tag
|
34
|
+
end
|
35
|
+
|
36
|
+
def locales
|
37
|
+
@locales ||= VARS.reject { |name| !ENV[name] || ENV[name] == "C" }.flat_map do |name|
|
38
|
+
# LANGUAGE's value can be delimited by ":"
|
39
|
+
ENV[name].split(":").map { |l| normalize(l) }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def normalize(lang)
|
44
|
+
# Remove encoding
|
45
|
+
lang = lang.split(".").first
|
46
|
+
lang.tr!("_", "-")
|
47
|
+
lang.to_sym
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: i18n-env-config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Skye Shaw
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: i18n
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
description: An I18n config class that looks at the LANGUAGE, LC_ALL, LC_MESSAGES,
|
70
|
+
and LOCALE environment variables to determine I18n.locale
|
71
|
+
email:
|
72
|
+
- skye.shaw@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- i18n-env-config.gemspec
|
84
|
+
- lib/i18n/env/config.rb
|
85
|
+
homepage: https://github.com/sshaw/i18n-env-config
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.4.8
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Set I18n.locale based on the environment variables that control the locale
|
109
|
+
test_files: []
|