transilator 1.0.0 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36f307d208407e582599a82f7087be239bee6eff
4
- data.tar.gz: bc0b12cfc8bef47f6dec48ba279240d3b1f78a3f
3
+ metadata.gz: 4ca38e76b1e55e598e90bf1f09d4bba64d82bb5f
4
+ data.tar.gz: 7aed382a01858d6d9c7ed3d865cc088448b134a5
5
5
  SHA512:
6
- metadata.gz: c8fd2cc1f2ffc891f6425fd1721a20195c866e08aaa0b4abdddcb7129aac3444e92a9e90b42aed25c2882b21a0c32f9df95650a5095d0bf316a7924203f2d0ae
7
- data.tar.gz: 8e8493a064a3b744b2a0ab86cec181440d03d3b50ab6256eff82e7b91bda1db6b3d481dad1b29b10b12ee0c2c49d0561489bea8e053968ff5ad31421f161a226
6
+ metadata.gz: a03646844e4bc24feb1757d15a5f927de6b18e13a77ad5abcf6ceb70c0d264e18b1a0237b9322268e0b7bce2f34418477f8428c75b58784bb59a55714bf89bf7
7
+ data.tar.gz: 2e3da9b600c787519a73abc2701f5ac09d39941361cd2dcffceb2bda329ebf34cb2aaa636b9ac684363ab489c3f211adad5ee27b5e6c34f3a391d71f4bf789fa
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Transilator
2
2
 
3
- [![Build Status](https://travis-ci.org/hendricius/transilator.svg?branch=master)](https://travis-ci.org/hendricius/transilator)
3
+ [![CircleCI](https://circleci.com/gh/hendricius/transilator.svg?style=svg)](https://circleci.com/gh/hendricius/transilator)
4
4
 
5
5
  Transilator makes storing translations for database records painless.
6
6
 
@@ -32,11 +32,11 @@ You may need to create migration for your model as usual. Assuming you have no
32
32
  table yet, create the table:
33
33
 
34
34
  ```ruby
35
- create_table :posts do |t|
36
- t.hstore :title # note for this to work you need to enable the hstore extension in your database.
37
- t.jsonb :summary # this is the suggested way.
38
- t.timestamps
39
- end
35
+ create_table :posts do |t|
36
+ t.hstore :title # note for this to work you need to enable the hstore extension in your database.
37
+ t.jsonb :summary # this is the suggested way.
38
+ t.timestamps
39
+ end
40
40
  ```
41
41
 
42
42
  Then adjust your model and integrate transilator:
@@ -49,6 +49,8 @@ end
49
49
 
50
50
  Thats it!
51
51
 
52
+ **Basic usage**
53
+
52
54
  Now you are able to translate values for the attributes :title and :description per locale:
53
55
 
54
56
  ```ruby
@@ -71,10 +73,28 @@ post.title_en = 'I have no idea what I am doing'
71
73
  post.title_en #=> I have no idea what I am doing
72
74
  ```
73
75
 
76
+ You can pass the values directly during initialisation of an object:
77
+ ```ruby
78
+ Post.new(title: {en: 'The German', de: 'Serr German'})
79
+ ```
80
+
81
+ **Fallbacks**
82
+
83
+ You can setup fallbacks in case you are missing data from one language. Then
84
+ the next language is used based on the order in the fallbacks array.
85
+
86
+ Create an initializer `initializers/transilator.rb` in your rails app.
87
+
88
+ ```ruby
89
+ Transilator.configure do |config|
90
+ config.locale_fallbacks = { "de" => ["en", "at"] }
91
+ end
92
+ ```
74
93
 
75
- You may use initialization if needed:
94
+ Now in case we wouldn't have a German translation we fallback to English. If
95
+ we have no English translation then we will proceed and try our Austrian
96
+ translation. If that doesn't work then empty string is returned.
76
97
 
77
- Post.new(title: {en: 'The German', de: 'Serr German'})
78
98
 
79
99
  ## Performance ##
80
100
 
data/lib/transilator.rb CHANGED
@@ -2,4 +2,31 @@ require "transilator/version"
2
2
  require "transilator/active_record_extensions"
3
3
 
4
4
  module Transilator
5
+ class << self
6
+ attr_accessor :configuration
7
+ end
8
+
9
+ def self.configure
10
+ self.configuration ||= Configuration.new
11
+ yield(configuration)
12
+ end
13
+
14
+ # returns the config object
15
+ def self.config
16
+ self.configuration = Configuration.new unless self.configuration
17
+ self.configuration
18
+ end
19
+
20
+ class Configuration
21
+ attr_accessor :locale_fallbacks
22
+
23
+ def initialize
24
+ @locale_fallbacks = {}
25
+ end
26
+
27
+ # returns an array with fallbacks for the given locale
28
+ def get_fallbacks_for_locale(locale)
29
+ locale_fallbacks[locale] || []
30
+ end
31
+ end
5
32
  end
@@ -5,14 +5,26 @@ module Transilator
5
5
 
6
6
  def transilator(*args)
7
7
 
8
-
9
8
  args.each do |attribute|
10
9
 
11
10
  # read the attribute from the hstore attribute and return it.
12
11
  #
13
12
  # returns the attribute or an empty string if the attribute is nil
14
13
  define_method attribute do
15
- (self[attribute] || {})[I18n.locale.to_s] || ""
14
+ locale = I18n.locale.to_s
15
+ attribute_value_hash = (self[attribute] || {})
16
+ value = (attribute_value_hash)[locale] || ""
17
+ # we have a value, so let's abort.
18
+ return value unless value.empty?
19
+ # now implement our fallback mechanism
20
+ fallbacks_for_locale = Transilator.config.get_fallbacks_for_locale(locale)
21
+ return value if fallbacks_for_locale.empty?
22
+ # now do the magic for figuring out the fallback
23
+ fallback_locale_to_use = fallbacks_for_locale.find {|f| (attribute_value_hash)[f] }
24
+ # no fallback that can be used found.
25
+ return "" unless fallback_locale_to_use
26
+ # return our value or an empty string
27
+ attribute_value_hash[fallback_locale_to_use] || ""
16
28
  end
17
29
 
18
30
  # set the attribute on the model on the hstore hash
@@ -1,3 +1,3 @@
1
1
  module Transilator
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transilator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hendrik Kleinwaechter