locale_base 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ module LocaleBase
2
+
3
+ class TrackingToken
4
+
5
+ attr_reader :position
6
+
7
+ def initialize(tracker, position)
8
+ @tracker = tracker
9
+ @position = position
10
+ end
11
+
12
+ def to_s
13
+ @tracker.retrieve(self)
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,49 @@
1
+ module LocaleBase
2
+
3
+ # VVV this is pretty awesome
4
+
5
+ # analyze performance implications
6
+ # TODO consider using a hash instead of any array - may have performance help
7
+ # on many duplicates in yaml file - is that a use case that's valid?
8
+ # is there a better structure?
9
+ class TranslationTracker
10
+
11
+ def initialize
12
+ @tracking_array = []
13
+ @holdings = {}
14
+ end
15
+
16
+ # translate every token
17
+ # TODO make translate! in easy translate
18
+ # TODO move out of this class and into the Base**
19
+ def translate_all(options)
20
+ @tracking_array = EasyTranslate.translate(@tracking_array, options.merge(:html => true))
21
+ end
22
+
23
+ # for things that should be hidden and not translated
24
+ # return they key that will be put in place and unheld later
25
+ #
26
+ # Note: this workaround is needed due to how google translate
27
+ # doesn't cleanly handle .notranslate spans
28
+ def hold(value)
29
+ key = "h%020x" % rand(1 << 80)
30
+ @holdings[key] = value
31
+ key
32
+ end
33
+
34
+ def unhold(key)
35
+ @holdings[key]
36
+ end
37
+
38
+ def track(value)
39
+ @tracking_array << value
40
+ TrackingToken.new(self, @tracking_array.size - 1)
41
+ end
42
+
43
+ def retrieve(token)
44
+ @tracking_array[token.position]
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,65 @@
1
+ module LocaleBase
2
+
3
+ class Translator
4
+
5
+ # Initialize a new LocaleBase for translating
6
+ # locale files
7
+ #
8
+ # +hash+ The object to convert
9
+ def initialize(hash = [])
10
+ @original = hash
11
+ end
12
+
13
+ # place track tokens
14
+ # translate all of them
15
+ # and then replace the tokens with their string equivelants
16
+ def translate(options = {})
17
+
18
+ tracker = TranslationTracker.new
19
+
20
+ # we use divs here instead of spans so that google doesn't mess us up
21
+ # by combining spans on requests
22
+ @created = crazy_walk(@original) do |obj|
23
+ obj.gsub!(/\{\{([^\}]+)\}\}/) do |m|
24
+ "<div class='notranslate'>#{tracker.hold($1)}</div>"
25
+ end
26
+ # insert trackers
27
+ tracker.track(obj)
28
+ end
29
+
30
+ # translate everything
31
+ tracker.translate_all(options)
32
+
33
+ # replace the tokens with the tranlated text
34
+ @created = crazy_walk(@created) do |obj|
35
+ obj = tracker.retrieve(obj) if obj.is_a?(TrackingToken)
36
+ # remove divs and replace with hold text - don't let google near this stuff
37
+ # whitespace split also needed cause google messess with our shit
38
+ obj.gsub!('><', '> <')
39
+ obj.gsub!(/<div\sclass='notranslate'>([^<]+)<\/div>/) do |m|
40
+ "{{#{tracker.unhold($1)}}}"
41
+ end
42
+
43
+ obj
44
+ end
45
+
46
+ end
47
+
48
+ private
49
+
50
+ # nice little recursive walker
51
+ def crazy_walk(obj, &block)
52
+ if obj.is_a?(Array)
53
+ obj.map { |item| crazy_walk(item, &block) }
54
+ elsif obj.is_a?(Hash)
55
+ hash = Hash.new
56
+ obj.each_pair { |key, value| hash[key] = crazy_walk(value, &block) }
57
+ hash
58
+ else
59
+ yield obj
60
+ end
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -0,0 +1,5 @@
1
+ module LocaleBase
2
+
3
+ VERSION = '0.0.2'
4
+
5
+ end
@@ -0,0 +1,14 @@
1
+ module LocaleBase
2
+
3
+ require 'uri'
4
+ require 'net/http'
5
+
6
+ require 'rubygems'
7
+ require 'easy_translate'
8
+
9
+ require 'locale_base/tracking_token'
10
+ require 'locale_base/translation_tracker'
11
+
12
+ require 'locale_base/translator'
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: locale_base
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - John Crepezzi
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-01 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: easy_translate
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ description: locale_base is a way to easily translate yaml locale files from one language to another
50
+ email: john@crepezzi.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - lib/locale_base/tracking_token.rb
59
+ - lib/locale_base/translation_tracker.rb
60
+ - lib/locale_base/translator.rb
61
+ - lib/locale_base/version.rb
62
+ - lib/locale_base.rb
63
+ has_rdoc: true
64
+ homepage: http://github.com/seejohnrun/locale_base/
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options: []
69
+
70
+ require_paths:
71
+ - lib
72
+ required_ruby_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
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ requirements: []
91
+
92
+ rubyforge_project: locale_base
93
+ rubygems_version: 1.3.7
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: EasyTranslate backed YAML locale translation
97
+ test_files: []
98
+