tidy_i18n 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ N2E5OTkxODZhZGQ2MzFiMGI2ZTZmN2M2MjYzZTg5MzEzNTI5NmU2ZQ==
5
+ data.tar.gz: !binary |-
6
+ Zjg4MzBiODMwNjA1ZTVjOTcyM2Y3MGNjZTViMDgwZDM3N2RjMzMwOQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZDdlZTMyNGRiZWFiZTI1MzJmODQ4YjBlMzg3Mjc2MDg4NzkwN2FlNDdhNTM0
10
+ YTNkOWI4NjAxODY3N2ZhMzVhMWJhNDc0YmE5NmNlYWU3YmIwMzkxZGJjMmUw
11
+ NGRlODc0OTMwNTdkODFjZmJkMzUxYWU4MzdmOTg0ZGQwZWI0Mzc=
12
+ data.tar.gz: !binary |-
13
+ NzE1OTg0OGQ5YzNjNmZhOGU4ZTA3MTdjZmVkNzY3YjI3YmE0YmU3ODFiNjU1
14
+ NDA1MGRjYjUwNjk2ZDllODgyMjYwYWQzOTI1ZDdiMWM1ZDQxZmZjMmFiYmJm
15
+ YTA1ODVhNTBiMmZhYzczMDc3OTM0M2ZmMmFiZWExMjk0YTMxNmE=
@@ -0,0 +1,45 @@
1
+ # encoding: UTF-8
2
+ require "tidy_i18n/translations"
3
+
4
+ module TidyI18n
5
+ class DictionaryConverter
6
+
7
+ class InvalidTranslationValue < StandardError
8
+ end
9
+
10
+ def initialize(dictionary, locale)
11
+ self.dictionary = dictionary
12
+ self.locale = locale
13
+ end
14
+
15
+ def converted_dictionary
16
+ convert_dictionary(dictionary, [])
17
+ end
18
+
19
+ private
20
+
21
+ def convert_dictionary(dictionary, path)
22
+ dictionary.each_with_object({}) do |(key, value), new_dictionary|
23
+ new_dictionary[key] = convert_value(value, path + [key])
24
+ end
25
+ end
26
+
27
+ def convert_value(value, path)
28
+ case value.class.name
29
+ when "String"
30
+ locale.convert(value)
31
+ when "Hash"
32
+ convert_dictionary(value, path)
33
+ when "Array"
34
+ value.collect { |v| convert_value(v, path) }
35
+ when "Fixnum", "FalseClass", "TrueClass", "NilClass", "Symbol"
36
+ value
37
+ else
38
+ raise InvalidTranslationValue.new("#{path.join('.')} #{value.class.name} #{value.inspect}")
39
+ end
40
+ end
41
+
42
+ attr_accessor :dictionary, :locale
43
+
44
+ end
45
+ end
@@ -0,0 +1,25 @@
1
+ module TidyI18n
2
+ module Locales
3
+ class Reverse
4
+
5
+ INTERPOLATION_SEGMENT=/(%{[^\}]+})/
6
+
7
+ def convert(value)
8
+ reverse_interpolation_segments(value).reverse
9
+ end
10
+
11
+ private
12
+
13
+ def reverse_interpolation_segments(value)
14
+ interpolation_segment_match = value.match(INTERPOLATION_SEGMENT)
15
+ if interpolation_segment_match
16
+ interpolation_segment = interpolation_segment_match[1]
17
+ value.sub!(INTERPOLATION_SEGMENT, interpolation_segment.reverse)
18
+ reverse_interpolation_segments(value)
19
+ end
20
+ value
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ module TidyI18n
2
+ module Locales
3
+ class Tilde
4
+
5
+ def convert(value)
6
+ "~#{value}~"
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ require "tidy_i18n/dictionary_converter"
2
+
3
+ module TidyI18n
4
+ module Locales
5
+
6
+ def self.convert(source_locale, options)
7
+ keys_to_locale_classes = options.fetch(:to)
8
+ keys_to_locale_classes.each_with_object({}) do |(key, locale_class), hash|
9
+ converter = TidyI18n::DictionaryConverter.new(
10
+ TidyI18n::Translations.for_locale(source_locale),
11
+ locale_class.new
12
+ )
13
+ hash[key] = converter.converted_dictionary
14
+ end
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ require "i18n"
2
+ require "i18n/backend/simple"
3
+
4
+ module TidyI18n
5
+ class Translations
6
+
7
+ def self.for_locale(locale)
8
+ ::I18n.backend.send(:translations)[locale]
9
+ end
10
+
11
+ end
12
+ end
data/lib/tidy_i18n.rb ADDED
@@ -0,0 +1,39 @@
1
+ module TidyI18n
2
+ class RaiseAllErrors
3
+
4
+ def call(exception, locale, key, options)
5
+ raise exception.to_exception
6
+ end
7
+
8
+ end
9
+
10
+ def self.project_root=(path)
11
+ @project_root = path
12
+ end
13
+
14
+ def self.project_root
15
+ @project_root
16
+ end
17
+
18
+ def self.raise_error_on_missing_translation=(raise_error)
19
+ if raise_error
20
+ I18n.exception_handler = RaiseAllErrors.new
21
+ else
22
+ I18n.exception_handler = I18n::ExceptionHandler.new
23
+ end
24
+ end
25
+
26
+ def self.translate(key, options={})
27
+ if key[0] == "."
28
+ key_prefix = caller[0].
29
+ gsub("#{project_root}/", "").
30
+ split(".").
31
+ first.
32
+ gsub("/", ".")
33
+ I18n.translate("#{key_prefix}.#{key}", options)
34
+ else
35
+ I18n.translate(key, options)
36
+ end
37
+ end
38
+
39
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tidy_i18n
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eric Meyer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-27 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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Helpers for I18n. Add ways to encourage clean locale organization.
28
+ email:
29
+ - emeyer@8thlight.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/tidy_i18n/dictionary_converter.rb
35
+ - lib/tidy_i18n/locales/reverse.rb
36
+ - lib/tidy_i18n/locales/tilde.rb
37
+ - lib/tidy_i18n/locales.rb
38
+ - lib/tidy_i18n/translations.rb
39
+ - lib/tidy_i18n.rb
40
+ homepage: https://github.com/ericmeyer/tidy_i18n
41
+ licenses: []
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.0.7
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Helpers for I18n
63
+ test_files: []