Class: Rosette::Core::TranslationStatus
- Inherits:
-
Object
- Object
- Rosette::Core::TranslationStatus
- Defined in:
- lib/rosette/core/translation_status.rb
Overview
A utility class that encapsulates how translated a set of phrases is. The class knows how to report translation percentages, etc.
Instance Attribute Summary collapse
-
#locale_counts ⇒ Hash<Fixnum>
readonly
A hash of locale codes to counts.
-
#phrase_count ⇒ Fixnum
readonly
The total number of phrases.
Instance Method Summary collapse
-
#add_locale_count(locale_code, count) ⇒ void
Adds a locale code and count pair.
-
#fully_translated? ⇒ Boolean
Returns true if every locale is fully translated, false otherwise.
-
#fully_translated_in?(locale_code) ⇒ Boolean
Returns true if the given locale is fully translated (i.e. the number of translations is greater than or equal to the number of phrases).
-
#initialize(phrase_count) ⇒ TranslationStatus
constructor
Creates a new instance.
-
#locale_count(locale_code) ⇒ Object
Retrieves the number of completed translations for the given locale.
-
#locales ⇒ Array<String>
Retrieves a list of all the added locales.
-
#percent_translated(locale_code, precision = 2) ⇒ Float
Calculates a translated percentage for the given locale.
Constructor Details
#initialize(phrase_count) ⇒ TranslationStatus
Creates a new instance.
21 22 23 24 |
# File 'lib/rosette/core/translation_status.rb', line 21 def initialize(phrase_count) @phrase_count = phrase_count @locale_counts = {} end |
Instance Attribute Details
#locale_counts ⇒ Hash<Fixnum> (readonly)
Returns a hash of locale codes to counts. Each count represents the number of completed translations for that locale.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rosette/core/translation_status.rb', line 14 class TranslationStatus attr_reader :phrase_count, :locale_counts # Creates a new instance. # # @param [Fixnum] phrase_count The total number of phrases. # @return [TranslationStatus] def initialize(phrase_count) @phrase_count = phrase_count @locale_counts = {} end # Adds a locale code and count pair. The count should be the total number # of completed translations in the locale. # # @param [String] locale_code # @param [Fixnum] count # @return [void] def add_locale_count(locale_code, count) locale_counts[locale_code] = count end # Returns true if the given locale is fully translated (i.e. the number of # translations is greater than or equal to the number of phrases). # # @param [String] locale_code # @return [Boolean] def fully_translated_in?(locale_code) (locale_counts[locale_code] || -1) >= phrase_count end # Returns true if every locale is fully translated, false otherwise. # # @return [Boolean] def fully_translated? locale_counts.all? do |locale_code, _| fully_translated_in?(locale_code) end end # Retrieves the number of completed translations for the given locale. # Use this method in combination with +add_locale_count+. def locale_count(locale_code) locale_counts[locale_code] end # Retrieves a list of all the added locales. # # @return [Array<String>] def locales locale_counts.keys end # Calculates a translated percentage for the given locale. # # @param [String] locale_code # @param [Fixnum] precision The precision to use when rounding the # percentage. # @return [Float] the translation percentage, rounded to +precision+ # decimal places. def percent_translated(locale_code, precision = 2) pct = locale_counts[locale_code] / phrase_count.to_f [1.0, pct.round(precision)].min end end |
#phrase_count ⇒ Fixnum (readonly)
Returns the total number of phrases.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rosette/core/translation_status.rb', line 14 class TranslationStatus attr_reader :phrase_count, :locale_counts # Creates a new instance. # # @param [Fixnum] phrase_count The total number of phrases. # @return [TranslationStatus] def initialize(phrase_count) @phrase_count = phrase_count @locale_counts = {} end # Adds a locale code and count pair. The count should be the total number # of completed translations in the locale. # # @param [String] locale_code # @param [Fixnum] count # @return [void] def add_locale_count(locale_code, count) locale_counts[locale_code] = count end # Returns true if the given locale is fully translated (i.e. the number of # translations is greater than or equal to the number of phrases). # # @param [String] locale_code # @return [Boolean] def fully_translated_in?(locale_code) (locale_counts[locale_code] || -1) >= phrase_count end # Returns true if every locale is fully translated, false otherwise. # # @return [Boolean] def fully_translated? locale_counts.all? do |locale_code, _| fully_translated_in?(locale_code) end end # Retrieves the number of completed translations for the given locale. # Use this method in combination with +add_locale_count+. def locale_count(locale_code) locale_counts[locale_code] end # Retrieves a list of all the added locales. # # @return [Array<String>] def locales locale_counts.keys end # Calculates a translated percentage for the given locale. # # @param [String] locale_code # @param [Fixnum] precision The precision to use when rounding the # percentage. # @return [Float] the translation percentage, rounded to +precision+ # decimal places. def percent_translated(locale_code, precision = 2) pct = locale_counts[locale_code] / phrase_count.to_f [1.0, pct.round(precision)].min end end |
Instance Method Details
#add_locale_count(locale_code, count) ⇒ void
This method returns an undefined value.
Adds a locale code and count pair. The count should be the total number of completed translations in the locale.
32 33 34 |
# File 'lib/rosette/core/translation_status.rb', line 32 def add_locale_count(locale_code, count) locale_counts[locale_code] = count end |
#fully_translated? ⇒ Boolean
Returns true if every locale is fully translated, false otherwise.
48 49 50 51 52 |
# File 'lib/rosette/core/translation_status.rb', line 48 def fully_translated? locale_counts.all? do |locale_code, _| fully_translated_in?(locale_code) end end |
#fully_translated_in?(locale_code) ⇒ Boolean
Returns true if the given locale is fully translated (i.e. the number of translations is greater than or equal to the number of phrases).
41 42 43 |
# File 'lib/rosette/core/translation_status.rb', line 41 def fully_translated_in?(locale_code) (locale_counts[locale_code] || -1) >= phrase_count end |
#locale_count(locale_code) ⇒ Object
Retrieves the number of completed translations for the given locale. Use this method in combination with add_locale_count.
56 57 58 |
# File 'lib/rosette/core/translation_status.rb', line 56 def locale_count(locale_code) locale_counts[locale_code] end |
#locales ⇒ Array<String>
Retrieves a list of all the added locales.
63 64 65 |
# File 'lib/rosette/core/translation_status.rb', line 63 def locales locale_counts.keys end |
#percent_translated(locale_code, precision = 2) ⇒ Float
Calculates a translated percentage for the given locale.
74 75 76 77 |
# File 'lib/rosette/core/translation_status.rb', line 74 def percent_translated(locale_code, precision = 2) pct = locale_counts[locale_code] / phrase_count.to_f [1.0, pct.round(precision)].min end |