i18n_redis 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in i18n_redis.gemspec
4
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,86 @@
1
+ i18n_redis gem
2
+ ==================
3
+ Gem is use full migrate existing YAML based i18n solution to Redis based solution.
4
+
5
+ ### Requirement
6
+ - redis server
7
+ - redis gem
8
+ - gem 'i18n_redis'
9
+
10
+ ### Methods
11
+ Connect to redis databse using redis gem.
12
+
13
+ I18nRedis.connect
14
+
15
+ Convert Existing YAML To Redis Database It can be use full for any YAML to Redis
16
+
17
+ I18nRedis.yaml_to_redis(yaml_file_path)
18
+
19
+ Add new locale
20
+
21
+ I18nRedis.add_locale(name,value)`
22
+
23
+ e.g
24
+ Add us locale
25
+
26
+ I18nRedis.add_locale("us")
27
+
28
+ Add us locale for id id may be user id company id etc.
29
+
30
+ I18nRedis.add_locale("23","us")
31
+
32
+ Get all locale It will return locale hash.
33
+
34
+ I18nRedis.get_locales
35
+
36
+ Remove locale
37
+
38
+ I18nRedis.remove_locale("us")
39
+
40
+ Add Key
41
+
42
+ I18nRedis.add(key,value,locale)
43
+
44
+ I18nRedis.create(key,value)
45
+
46
+ Remove Key
47
+
48
+ I18nRedis.remove(key,locale)
49
+
50
+ I18nRedis.destroy(key)
51
+
52
+ Find Key
53
+
54
+ I18nRedis.find(key)
55
+
56
+ Find all for locale
57
+
58
+ I18nRedis.find_all_for_locale(locale)`
59
+
60
+ I18nRedis.find_all(key)
61
+
62
+ Copy one locale to another locale
63
+
64
+ I18nRedis.copy_locale_to_other(src_locale,dest_locale)
65
+
66
+ Clone data for all locale It will load master data to all present locale
67
+
68
+ I18nRedis.clone_data_for_all_locale(i18n_key,master_locale="en")
69
+
70
+ Search and Replace I18n Value
71
+
72
+ I18nRedis.search_and_replace_word(search_value,replace_value,locale=nil)
73
+
74
+ If locale is nil it will replace all value otherwise it will replace for particular locale
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+
86
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "i18n_redis/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "i18n_redis"
7
+ s.version = I18nRedis::VERSION
8
+ s.authors = ["Amar Daxini"]
9
+ s.email = ["amar.daxini@livialegal.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{I18n YAML to Redis Management}
12
+ s.description = %q{I18n YAML to Redis Management.It convert exising yaml to redis.Provides various helper methods to add,update,remove,create,copy master to another locale,one locale to another locale etc.}
13
+
14
+ s.rubyforge_project = "i18n_redis"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ s.add_runtime_dependency(%q<redis>, ["~> 2.2.0"])
21
+ end
@@ -0,0 +1,3 @@
1
+ module I18nRedis
2
+ VERSION = "0.0.2"
3
+ end
data/lib/i18n_redis.rb ADDED
@@ -0,0 +1,157 @@
1
+ require 'rubygems'
2
+ require 'redis'
3
+ require 'ruby-debug'
4
+ require "i18n_redis/version"
5
+
6
+
7
+ module I18nRedis
8
+
9
+
10
+ # Connect To Redis
11
+ def self.connect(args={})
12
+ $i18n_redis = Redis.new(args)
13
+ end
14
+
15
+ # YAML TO REDIS
16
+ # yaml to redis for i18n
17
+ # file name is "/abc/en.yml"
18
+ def self.yaml_to_redis(file_name)
19
+
20
+ if File.exist?(file_name)
21
+ yaml_hash = YAML::load(File.open(file_name))
22
+ yaml_hash.each do |k,v|
23
+ # if yaml data contain further nested then create nestedkey or add key to redis
24
+ self.add_value_to_redis(yaml_hash[k],k)
25
+ end
26
+ end
27
+ end
28
+
29
+ # en.
30
+ # errors:
31
+ # template:
32
+ # header: "%{count} errors prohibited this %{model} from being saved"
33
+ # This method generates folloeing key and set its respective value
34
+ # en.errors.template.header="%{count} errors prohibited this %{model} from being saved"
35
+ def self.add_value_to_redis(yaml_hash,yaml_key)
36
+ if yaml_hash.is_a?(Hash)
37
+ yaml_hash.each do |key,value|
38
+ if yaml_hash[key].is_a?(Hash)
39
+ add_value_to_redis(yaml_hash[key],yaml_key+"."+key)
40
+ else
41
+ create(yaml_key+"."+key,value)
42
+ end
43
+ end
44
+ else
45
+ create(yaml_key,yaml_hash)
46
+ end
47
+ end
48
+
49
+
50
+ # Add to en(master) it will add to all company
51
+ # locale is a redis hash which contain default key and value as locale name
52
+ # or it can be user id company id etc
53
+ # any data is added to master it will find all the respective company and add data
54
+ # key can be en.label_text
55
+
56
+ def self.add_to_all_locale(key,val)
57
+ locale_array = $i18n_redis.hgetall("locale")
58
+ locale_array.each do |lkey,lvalue|
59
+ create("#{lvalue}.#{key}",val)
60
+ end
61
+ end
62
+
63
+ # for removing data from all locale
64
+ def self.remove_from_all_locale(key)
65
+ # key abc.xyz
66
+ locale_array = $i18n_redis.hgetall("locale")
67
+ locale_array.each do |ckey,cvalue|
68
+ destroy("#{cvalue}.#{key}")
69
+ end
70
+ end
71
+
72
+
73
+
74
+ #This contain name = "user name or id",value en_20
75
+ def self.add_locale(name,value)
76
+ value = value ? value : name
77
+ $i18n_redis.hset("locale",name,value)
78
+ end
79
+
80
+ #This contain name = "company name or id"
81
+ def self.remove_locale(name)
82
+ $i18n_redis.hdel("locale",name)
83
+ end
84
+
85
+ # find out word and replace it throughout all locale
86
+ def self.search_and_replace_word(search_value,replace_value,locale=nil)
87
+ i18n_keys = find_all("#{locale}*")
88
+ i18n_keys.each do |i18n_key|
89
+ value = $i18n_redis.get(i18n_key)
90
+ value.gsub!(/\b#{search_value}\b/,replace_value)
91
+ create(i18n_key,value)
92
+ end
93
+ end
94
+
95
+ # copy one locale data to all present locale
96
+ def self.clone_data_for_all_locale(i18n_key,master_locale="en")
97
+ # find out all master keys i.e en
98
+ master_keys= find_all("#{master_locale}.*")
99
+ master_keys.each do |key|
100
+ clone_key = "#{i18n_key}.#{key.split('.').drop(1).join(".")}"
101
+ create(clone_key,$i18n_redis.get(key))
102
+ end
103
+ end
104
+
105
+ # Copy One Locale to Other
106
+ # en to us
107
+
108
+ def self.copy_locale_to_other(src_locale,dest_locale)
109
+ src_keys= find_all("#{master_locale}.*")
110
+ src_keys.each do |key|
111
+ clone_key = "#{dest_locale}.#{key.split('.').drop(1).join(".")}"
112
+ create(clone_key,$i18n_redis.get(key))
113
+ end
114
+ end
115
+
116
+ def find_all_for_locale(locale)
117
+ self.find_all("#{locale}.*")
118
+ end
119
+
120
+ def self.find_all(key)
121
+ $i18n_redis.keys(key)
122
+ end
123
+
124
+ # get all locale hashes
125
+ def self.get_locales
126
+ $i18n_redis.hgetall("locale")
127
+ end
128
+
129
+ # add key
130
+ def add(key,value,locale)
131
+ key = "#{locale}.#{key}" if locale
132
+ self.create(key,value)
133
+ end
134
+ # add to redis
135
+ def self.create(key,value)
136
+ $i18n_redis.set(key,value)
137
+ end
138
+
139
+ # remove from redis
140
+ # remove("text_label","us")
141
+ def remove(key,locale)
142
+ key = "#{locale}.#{key}" if locale
143
+ self.destroy(key)
144
+ end
145
+
146
+ # remove from redis
147
+ def self.destroy(key)
148
+ $i18n_redis.del(key)
149
+ end
150
+
151
+ # get data from redis
152
+ def self.find(key)
153
+ $i18n_redis.get(key)
154
+ end
155
+
156
+ end
157
+
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n_redis
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Amar Daxini
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-19 00:00:00 +05:30
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: redis
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 2
32
+ - 2
33
+ - 0
34
+ version: 2.2.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: I18n YAML to Redis Management.It convert exising yaml to redis.Provides various helper methods to add,update,remove,create,copy master to another locale,one locale to another locale etc.
38
+ email:
39
+ - amar.daxini@livialegal.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - README.markdown
50
+ - Rakefile
51
+ - i18n_redis.gemspec
52
+ - lib/i18n_redis.rb
53
+ - lib/i18n_redis/version.rb
54
+ has_rdoc: true
55
+ homepage: ""
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project: i18n_redis
84
+ rubygems_version: 1.4.2
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: I18n YAML to Redis Management
88
+ test_files: []
89
+