lit 0.0.4.2 → 0.0.4.3
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.
- data/README.md +1 -1
- data/app/assets/javascripts/lit/application.js +1 -1
- data/app/controllers/lit/application_controller.rb +10 -0
- data/app/controllers/lit/dashboard_controller.rb +1 -1
- data/app/controllers/lit/locales_controller.rb +31 -0
- data/app/controllers/lit/localization_keys_controller.rb +1 -1
- data/app/controllers/lit/localizations_controller.rb +1 -1
- data/app/models/lit/locale.rb +1 -0
- data/app/models/lit/localization_key.rb +2 -2
- data/app/views/layouts/lit/_navigation.html.erb +9 -0
- data/app/views/lit/locales/_hide_link.html.erb +1 -0
- data/app/views/lit/locales/hide.js.erb +1 -0
- data/app/views/lit/locales/index.html.erb +20 -0
- data/app/views/lit/localization_keys/index.html.erb +3 -2
- data/config/routes.rb +3 -0
- data/db/migrate/20130511111904_add_is_hidden_to_locales.rb +5 -0
- data/lib/lit.rb +5 -1
- data/lib/lit/adapters/hash_storage.rb +4 -0
- data/lib/lit/adapters/redis_storage.rb +17 -5
- data/lib/lit/cache.rb +110 -43
- data/lib/lit/i18n_backend.rb +10 -6
- data/lib/lit/version.rb +1 -1
- metadata +9 -4
    
        data/README.md
    CHANGED
    
    
| @@ -1,6 +1,8 @@ | |
| 1 1 | 
             
            module Lit
         | 
| 2 2 | 
             
              class ApplicationController < ActionController::Base
         | 
| 3 3 | 
             
                before_filter :authenticate
         | 
| 4 | 
            +
                before_filter :stop_hits_counter
         | 
| 5 | 
            +
                after_filter :restore_hits_counter
         | 
| 4 6 |  | 
| 5 7 | 
             
                private
         | 
| 6 8 | 
             
                  def authenticate
         | 
| @@ -8,5 +10,13 @@ module Lit | |
| 8 10 | 
             
                      send(Lit.authentication_function)
         | 
| 9 11 | 
             
                    end
         | 
| 10 12 | 
             
                  end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                  def stop_hits_counter
         | 
| 15 | 
            +
                    Lit.init.cache.stop_hits_counter
         | 
| 16 | 
            +
                  end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                  def restore_hits_counter
         | 
| 19 | 
            +
                    Lit.init.cache.restore_hits_counter
         | 
| 20 | 
            +
                  end
         | 
| 11 21 | 
             
              end
         | 
| 12 22 | 
             
            end
         | 
| @@ -0,0 +1,31 @@ | |
| 1 | 
            +
            require_dependency "lit/application_controller"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Lit
         | 
| 4 | 
            +
              class LocalesController < ApplicationController
         | 
| 5 | 
            +
                def index
         | 
| 6 | 
            +
                  @locales = Locale.ordered.all
         | 
| 7 | 
            +
              
         | 
| 8 | 
            +
                  respond_to do |format|
         | 
| 9 | 
            +
                    format.html # index.html.erb
         | 
| 10 | 
            +
                    format.json { render json: @locales }
         | 
| 11 | 
            +
                  end
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
                
         | 
| 14 | 
            +
                def hide
         | 
| 15 | 
            +
                  @locale = Locale.find(params[:id])
         | 
| 16 | 
            +
                  @locale.is_hidden = !@locale.is_hidden?
         | 
| 17 | 
            +
                  @locale.save
         | 
| 18 | 
            +
                  respond_to :js
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
              
         | 
| 21 | 
            +
                def destroy
         | 
| 22 | 
            +
                  @locale = Locale.find(params[:id])
         | 
| 23 | 
            +
                  @locale.destroy
         | 
| 24 | 
            +
              
         | 
| 25 | 
            +
                  respond_to do |format|
         | 
| 26 | 
            +
                    format.html { redirect_to locales_url }
         | 
| 27 | 
            +
                    format.json { head :no_content }
         | 
| 28 | 
            +
                  end
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
            end
         | 
    
        data/app/models/lit/locale.rb
    CHANGED
    
    
| @@ -1,5 +1,6 @@ | |
| 1 1 | 
             
            module Lit
         | 
| 2 2 | 
             
              class LocalizationKey < ActiveRecord::Base
         | 
| 3 | 
            +
                attr_accessor :interpolated_key
         | 
| 3 4 |  | 
| 4 5 | 
             
                ## SCOPES
         | 
| 5 6 | 
             
                scope :completed, proc{ where(:is_completed=>true) }
         | 
| @@ -25,11 +26,10 @@ module Lit | |
| 25 26 | 
             
                end
         | 
| 26 27 |  | 
| 27 28 | 
             
                def clone_localizations
         | 
| 28 | 
            -
                  first_localization = self.localizations.first
         | 
| 29 29 | 
             
                  new_created = false
         | 
| 30 30 | 
             
                  Lit::Locale.find_each do |locale|
         | 
| 31 31 | 
             
                    self.localizations.where(:locale_id=>locale.id).first_or_create do |l|
         | 
| 32 | 
            -
                      l.default_value =  | 
| 32 | 
            +
                      l.default_value = interpolated_key
         | 
| 33 33 | 
             
                      new_created = true
         | 
| 34 34 | 
             
                    end
         | 
| 35 35 | 
             
                  end
         | 
| @@ -1,4 +1,13 @@ | |
| 1 1 | 
             
            <%= link_to "Lost in translation", root_path, :class=>'brand' %>
         | 
| 2 2 | 
             
            <ul class="nav">
         | 
| 3 3 | 
             
              <li><%= link_to "Translate!", lit.localization_keys_path %></li>
         | 
| 4 | 
            +
              <li class="dropdown">
         | 
| 5 | 
            +
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
         | 
| 6 | 
            +
                  Settings
         | 
| 7 | 
            +
                  <b class="caret"></b>
         | 
| 8 | 
            +
                </a>
         | 
| 9 | 
            +
                <ul class="dropdown-menu">
         | 
| 10 | 
            +
                  <li><%= link_to "Locales", lit.locales_path %></li>
         | 
| 11 | 
            +
                </ul>
         | 
| 12 | 
            +
              </li>
         | 
| 4 13 | 
             
            </ul>
         | 
| @@ -0,0 +1 @@ | |
| 1 | 
            +
            <%= link_to locale.is_hidden? ? 'Show' : 'Hide', hide_locale_path(locale), :method=>:put, :remote=>true %>
         | 
| @@ -0,0 +1 @@ | |
| 1 | 
            +
            $('#hide_link_<%= @locale.id %>').html("<%= j render(:partial=>"hide_link", :locals=>{:locale=>@locale}) %>");
         | 
| @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            <h1>Listing locales</h1>
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            <table class="table table-stripped">
         | 
| 4 | 
            +
              <tr>
         | 
| 5 | 
            +
                <th>Locale</th>
         | 
| 6 | 
            +
                <th width="100">Options</th>
         | 
| 7 | 
            +
              </tr>
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            <% @locales.each do |locale| %>
         | 
| 10 | 
            +
              <tr>
         | 
| 11 | 
            +
                <td><%= locale %></td>
         | 
| 12 | 
            +
                <td>
         | 
| 13 | 
            +
                  <span id="hide_link_<%= locale.id %>"><%= render :partial=>"hide_link", :locals=>{:locale=>locale} %></span>
         | 
| 14 | 
            +
                  <%= link_to 'Destroy', locale, method: :delete, data: { confirm: 'Are you sure?' } %>
         | 
| 15 | 
            +
                </td>
         | 
| 16 | 
            +
              </tr>
         | 
| 17 | 
            +
            <% end %>
         | 
| 18 | 
            +
            </table>
         | 
| 19 | 
            +
             | 
| 20 | 
            +
             | 
| @@ -5,6 +5,7 @@ | |
| 5 5 | 
             
                <tr class="localization_key_row" data-id="<%= lk.id %>">
         | 
| 6 6 | 
             
                  <td>
         | 
| 7 7 | 
             
                    <strong><%= lk.localization_key %></strong>
         | 
| 8 | 
            +
                    <span class="badge"><%= Lit.init.cache.get_global_hits_counter(lk.localization_key) %></span>
         | 
| 8 9 | 
             
                    <div class="localization_keys_options">
         | 
| 9 10 | 
             
                      <%= link_to lit.star_localization_key_path(lk), :remote=>true, :class=>"star_icon" do %>
         | 
| 10 11 | 
             
                        <%= draw_icon lk.is_starred? ? 'star' : 'star-empty' %>
         | 
| @@ -19,11 +20,11 @@ | |
| 19 20 | 
             
                          <tr>
         | 
| 20 21 | 
             
                            <%- localization = lk.localizations.where(:locale_id=>Lit.init.cache.find_locale(locale).id).first %>
         | 
| 21 22 | 
             
                            <% unless localization %>
         | 
| 22 | 
            -
                               | 
| 23 | 
            +
                              <% Lit.init.cache.refresh_key("#{locale}.#{lk.localization_key}") %>
         | 
| 23 24 | 
             
                              <%- localization = lk.localizations.where(:locale_id=>Lit.init.cache.find_locale(locale).id).first %>
         | 
| 24 25 | 
             
                            <% end %>
         | 
| 25 26 | 
             
                            <td class="localization_row" data-id="<%= localization.id%>" data-edit="<%= edit_localization_key_localization_path(lk, localization) %>" data-editing=0 data-content="<%= ejs(Lit.init.cache["#{locale}.#{lk.localization_key}"])%>">
         | 
| 26 | 
            -
                              <%= Lit.init.cache["#{locale}.#{lk.localization_key}"]  | 
| 27 | 
            +
                              <%= Lit.init.cache["#{locale}.#{lk.localization_key}"] %>
         | 
| 27 28 | 
             
                            </td>
         | 
| 28 29 | 
             
                            <td class="locale_row">
         | 
| 29 30 | 
             
                              <%= image_tag "lit/famfamfam_flags/#{locale[0,2]}.png" %>
         | 
    
        data/config/routes.rb
    CHANGED
    
    
    
        data/lib/lit.rb
    CHANGED
    
    | @@ -5,12 +5,16 @@ module Lit | |
| 5 5 | 
             
              mattr_accessor :authentication_function
         | 
| 6 6 | 
             
              mattr_accessor :key_value_engine
         | 
| 7 7 | 
             
              mattr_accessor :storage_options
         | 
| 8 | 
            +
              mattr_accessor :humanize_key
         | 
| 9 | 
            +
              mattr_accessor :fallback
         | 
| 8 10 | 
             
              class << self
         | 
| 9 11 | 
             
                attr_accessor :loader
         | 
| 10 12 | 
             
              end
         | 
| 11 13 | 
             
              def self.init
         | 
| 12 | 
            -
                 | 
| 14 | 
            +
                @@table_exists ||= Lit::Locale.table_exists?
         | 
| 15 | 
            +
                if self.loader.nil? && @@table_exists
         | 
| 13 16 | 
             
                  self.loader ||= Loader.new
         | 
| 17 | 
            +
                  Lit.humanize_key = true if Lit.humanize_key.nil?
         | 
| 14 18 | 
             
                  #if loading all translations on start, migrations have to be performed
         | 
| 15 19 | 
             
                  #already, fails on first deploy
         | 
| 16 20 | 
             
                  #self.loader.cache.load_all_translations
         | 
| @@ -2,9 +2,8 @@ require 'redis' | |
| 2 2 | 
             
            module Lit
         | 
| 3 3 | 
             
              extend self
         | 
| 4 4 | 
             
              def redis
         | 
| 5 | 
            -
                 | 
| 6 | 
            -
                 | 
| 7 | 
            -
                @redis
         | 
| 5 | 
            +
                $redis = Redis.connect unless $redis
         | 
| 6 | 
            +
                $redis
         | 
| 8 7 | 
             
              end
         | 
| 9 8 | 
             
              class RedisStorage
         | 
| 10 9 | 
             
                def initialize
         | 
| @@ -19,9 +18,12 @@ module Lit | |
| 19 18 | 
             
                  Lit.redis.set(_prefixed_key(k).to_s, v.to_s)
         | 
| 20 19 | 
             
                end
         | 
| 21 20 |  | 
| 21 | 
            +
                def delete(k)
         | 
| 22 | 
            +
                  Lit.redis.del k
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
             | 
| 22 25 | 
             
                def clear
         | 
| 23 | 
            -
                  Lit.redis.del self.keys
         | 
| 24 | 
            -
                  save
         | 
| 26 | 
            +
                  Lit.redis.del(self.keys) if self.keys.length > 0
         | 
| 25 27 | 
             
                end
         | 
| 26 28 |  | 
| 27 29 | 
             
                def keys
         | 
| @@ -32,6 +34,16 @@ module Lit | |
| 32 34 | 
             
                  Lit.redis.exists(_prefixed_key(key))
         | 
| 33 35 | 
             
                end
         | 
| 34 36 |  | 
| 37 | 
            +
                def incr(key)
         | 
| 38 | 
            +
                  Lit.redis.incr(_prefixed_key(key))
         | 
| 39 | 
            +
                end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                def sort
         | 
| 42 | 
            +
                  Lit.redis.keys.sort.map do |k|
         | 
| 43 | 
            +
                    [k, self.[](k)]
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
                end
         | 
| 46 | 
            +
             | 
| 35 47 | 
             
                private
         | 
| 36 48 | 
             
                  def _prefixed_key(key="")
         | 
| 37 49 | 
             
                    prefix = "lit:"
         | 
    
        data/lib/lit/cache.rb
    CHANGED
    
    | @@ -3,10 +3,13 @@ module Lit | |
| 3 3 |  | 
| 4 4 | 
             
                def initialize
         | 
| 5 5 | 
             
                  @localizations = Lit.get_key_value_engine
         | 
| 6 | 
            +
                  @hits_counter = Lit.get_key_value_engine
         | 
| 7 | 
            +
                  @hits_counter_working = true
         | 
| 6 8 | 
             
                end
         | 
| 7 9 |  | 
| 8 10 | 
             
                def [](key)
         | 
| 9 | 
            -
                   | 
| 11 | 
            +
                  update_hits_count(key)
         | 
| 12 | 
            +
                  @localizations[key] 
         | 
| 10 13 | 
             
                end
         | 
| 11 14 |  | 
| 12 15 | 
             
                def []=(key, value)
         | 
| @@ -22,34 +25,44 @@ module Lit | |
| 22 25 | 
             
                end
         | 
| 23 26 |  | 
| 24 27 | 
             
                def update_locale(key, value)
         | 
| 28 | 
            +
                  key = key.to_s
         | 
| 25 29 | 
             
                  locale_key, key_without_locale = split_key(key)
         | 
| 26 | 
            -
                  #Lit.init.logger.info "key: #{key}"
         | 
| 27 | 
            -
                  #Lit.init.logger.info "key_without_locale: #{key_without_locale}"
         | 
| 28 | 
            -
                  #Lit.init.logger.info "value: #{value}"
         | 
| 29 30 | 
             
                  locale = find_locale(locale_key)
         | 
| 30 31 | 
             
                  localization = find_localization(locale, key_without_locale, value)
         | 
| 31 | 
            -
                  @localizations[key] = localization.get_value
         | 
| 32 | 
            +
                  @localizations[key] = localization.get_value if localization
         | 
| 32 33 | 
             
                end
         | 
| 33 34 |  | 
| 34 | 
            -
                def load_all_translations
         | 
| 35 | 
            +
                def load_all_translations(oninit=false)
         | 
| 35 36 | 
             
                  Lit.init.logger.info "loading all translations"
         | 
| 36 | 
            -
                   | 
| 37 | 
            -
             | 
| 37 | 
            +
                  doinit = false
         | 
| 38 | 
            +
                  first = Localization.order('id ASC').first
         | 
| 39 | 
            +
                  last = Localization.order('id DESC').first
         | 
| 40 | 
            +
                  if not first or not last or (not @localizations.has_key?(first.full_key) or
         | 
| 41 | 
            +
                    not @localizations.has_key?(last.full_key))
         | 
| 42 | 
            +
                    doinit = true
         | 
| 43 | 
            +
                  end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                  if oninit==false || doinit==true
         | 
| 46 | 
            +
                    Localization.includes([:locale, :localization_key]).find_each do |l|
         | 
| 47 | 
            +
                      @localizations[l.full_key] = l.get_value
         | 
| 48 | 
            +
                    end
         | 
| 38 49 | 
             
                  end
         | 
| 39 50 | 
             
                end
         | 
| 40 51 |  | 
| 41 52 | 
             
                def refresh_key(key)
         | 
| 53 | 
            +
                  key = key.to_s
         | 
| 42 54 | 
             
                  Lit.init.logger.info "refreshing key: #{key}"
         | 
| 43 55 | 
             
                  locale_key, key_without_locale = split_key(key)
         | 
| 44 56 | 
             
                  locale = find_locale(locale_key)
         | 
| 45 57 | 
             
                  localization = find_localization(locale, key_without_locale)
         | 
| 46 | 
            -
                  @localizations[key] = localization.get_value
         | 
| 58 | 
            +
                  @localizations[key] = localization.get_value if localization
         | 
| 47 59 | 
             
                end
         | 
| 48 60 |  | 
| 49 61 | 
             
                def delete_key(key)
         | 
| 62 | 
            +
                  key = key.to_s
         | 
| 50 63 | 
             
                  Lit.init.logger.info "deleting key: #{key}"
         | 
| 51 64 | 
             
                  @localizations.delete(key)
         | 
| 52 | 
            -
                   | 
| 65 | 
            +
                  key_without_locale = split_key(key).last
         | 
| 53 66 | 
             
                  @localization_keys.delete(key_without_locale)
         | 
| 54 67 | 
             
                  I18n.backend.reload!
         | 
| 55 68 | 
             
                end
         | 
| @@ -57,11 +70,14 @@ module Lit | |
| 57 70 | 
             
                def reset
         | 
| 58 71 | 
             
                  @locale_cache = {}
         | 
| 59 72 | 
             
                  @localizations = Lit.get_key_value_engine
         | 
| 73 | 
            +
                  @localizations.clear
         | 
| 60 74 | 
             
                  @localization_keys = Lit.get_key_value_engine
         | 
| 75 | 
            +
                  @localization_keys.clear
         | 
| 61 76 | 
             
                  load_all_translations
         | 
| 62 77 | 
             
                end
         | 
| 63 78 |  | 
| 64 79 | 
             
                def find_locale(locale_key)
         | 
| 80 | 
            +
                  locale_key = locale_key.to_s
         | 
| 65 81 | 
             
                  @locale_cache ||= {}
         | 
| 66 82 | 
             
                  unless @locale_cache.has_key?(locale_key)
         | 
| 67 83 | 
             
                    #Lit.init.logger.info "looking for locale: #{locale_key}"
         | 
| @@ -76,7 +92,11 @@ module Lit | |
| 76 92 | 
             
                def export
         | 
| 77 93 | 
             
                  keys = {}
         | 
| 78 94 | 
             
                  reset
         | 
| 79 | 
            -
                   | 
| 95 | 
            +
                  db_localizations = {}
         | 
| 96 | 
            +
                  Lit::Localization.find_each do |l|
         | 
| 97 | 
            +
                    db_localizations[l.full_key] = l.get_value
         | 
| 98 | 
            +
                  end
         | 
| 99 | 
            +
                  db_localizations.sort.each do |(l_key, value)|
         | 
| 80 100 | 
             
                    current = keys
         | 
| 81 101 | 
             
                    yaml_keys = l_key.split('.')
         | 
| 82 102 |  | 
| @@ -93,61 +113,93 @@ module Lit | |
| 93 113 | 
             
                  keys.to_yaml
         | 
| 94 114 | 
             
                end
         | 
| 95 115 |  | 
| 116 | 
            +
                def get_global_hits_counter(key)
         | 
| 117 | 
            +
                  @hits_counter['global_hits_counter.'+key]
         | 
| 118 | 
            +
                end
         | 
| 119 | 
            +
             | 
| 120 | 
            +
                def get_hits_counter(key)
         | 
| 121 | 
            +
                  @hits_counter['hits_counter.'+key]
         | 
| 122 | 
            +
                end
         | 
| 123 | 
            +
             | 
| 124 | 
            +
                def stop_hits_counter
         | 
| 125 | 
            +
                  @hits_counter_working = false
         | 
| 126 | 
            +
                end
         | 
| 127 | 
            +
             | 
| 128 | 
            +
                def restore_hits_counter
         | 
| 129 | 
            +
                  @hits_counter_working = true
         | 
| 130 | 
            +
                end
         | 
| 131 | 
            +
             | 
| 96 132 | 
             
                private
         | 
| 97 133 |  | 
| 98 134 | 
             
                  def find_localization(locale, key_without_locale, value=nil)
         | 
| 99 135 | 
             
                    unless value.is_a?(Hash)
         | 
| 100 136 | 
             
                      localization_key = find_localization_key(key_without_locale)
         | 
| 101 | 
            -
                      create = false
         | 
| 102 137 | 
             
                      localization = Lit::Localization.where(:locale_id=>locale.id).
         | 
| 103 138 | 
             
                                        where(:localization_key_id=>localization_key.id).first_or_create do |l|
         | 
| 104 139 | 
             
                        if value.is_a?(Array)
         | 
| 105 | 
            -
                           | 
| 106 | 
            -
             | 
| 107 | 
            -
             | 
| 108 | 
            -
                             | 
| 109 | 
            -
             | 
| 110 | 
            -
             | 
| 111 | 
            -
             | 
| 112 | 
            -
             | 
| 113 | 
            -
             | 
| 114 | 
            -
             | 
| 140 | 
            +
                          new_value = nil
         | 
| 141 | 
            +
                          value_clone = value.dup
         | 
| 142 | 
            +
                          while (v = value_clone.shift) && v.present?
         | 
| 143 | 
            +
                            pv = parse_value(v, locale)
         | 
| 144 | 
            +
                            new_value = pv unless pv.nil?
         | 
| 145 | 
            +
                          end
         | 
| 146 | 
            +
                          value = new_value
         | 
| 147 | 
            +
                        else
         | 
| 148 | 
            +
                          value = parse_value(value, locale) unless value.nil?
         | 
| 149 | 
            +
                        end
         | 
| 150 | 
            +
                        if value.nil?
         | 
| 151 | 
            +
                          if Lit.fallback
         | 
| 152 | 
            +
                            @locale_cache.keys.each do |lc|
         | 
| 153 | 
            +
                              if lc != locale.locale
         | 
| 154 | 
            +
                                nk = "#{lc}.#{key_without_locale}"
         | 
| 155 | 
            +
                                v = @localizations[nk]
         | 
| 156 | 
            +
                                value = v if v.present? and value.nil?
         | 
| 115 157 | 
             
                              end
         | 
| 116 158 | 
             
                            end
         | 
| 117 | 
            -
                            value = new_value.nil? ? value.last : new_value
         | 
| 118 | 
            -
                          else
         | 
| 119 | 
            -
                            value = value.first
         | 
| 120 159 | 
             
                          end
         | 
| 160 | 
            +
                          value = key_without_locale.split('.').last.humanize if value.nil? && 
         | 
| 161 | 
            +
                                                                                Lit.humanize_key
         | 
| 121 162 | 
             
                        end
         | 
| 122 | 
            -
                        l.default_value = value | 
| 123 | 
            -
                        #Lit.init.logger.info "creating new localization: #{key_without_locale}"
         | 
| 124 | 
            -
                        #Lit.init.logger.info "creating new localization with value: #{value}"
         | 
| 125 | 
            -
                        #Lit.init.logger.info "creating new localization with value: #{value.class}"
         | 
| 126 | 
            -
                        create = true
         | 
| 163 | 
            +
                        l.default_value = value
         | 
| 127 164 | 
             
                      end
         | 
| 128 | 
            -
                      localization_key.clone_localizations if create and localization_key.localizations.count(:id)==1
         | 
| 129 165 | 
             
                      localization
         | 
| 130 166 | 
             
                    else
         | 
| 131 167 | 
             
                      Lit.init.logger.info "returning value for hash: #{key_without_locale}: #{value.to_s}"
         | 
| 132 | 
            -
                       | 
| 133 | 
            -
                      l.default_value = value
         | 
| 134 | 
            -
                      l
         | 
| 168 | 
            +
                      nil
         | 
| 135 169 | 
             
                    end
         | 
| 136 170 | 
             
                  end
         | 
| 137 171 |  | 
| 172 | 
            +
                  ## checks parameter type and returns value basing on it
         | 
| 173 | 
            +
                  ## symbols are beeing looked up in db
         | 
| 174 | 
            +
                  ## string are returned directly
         | 
| 175 | 
            +
                  ## procs are beeing called (once)
         | 
| 176 | 
            +
                  ## hashes are converted do string (for now)
         | 
| 177 | 
            +
                  def parse_value(v, locale)
         | 
| 178 | 
            +
                    new_value = nil
         | 
| 179 | 
            +
                    case v
         | 
| 180 | 
            +
                      when Symbol then
         | 
| 181 | 
            +
                        lk = Lit::LocalizationKey.where(:localization_key=>v.to_s).first
         | 
| 182 | 
            +
                        if lk
         | 
| 183 | 
            +
                          loca = Lit::Localization.where(:locale_id=>locale.id).
         | 
| 184 | 
            +
                                      where(:localization_key_id=>lk.id).first
         | 
| 185 | 
            +
                          new_value = loca.get_value if loca and loca.get_value.present?
         | 
| 186 | 
            +
                        end
         | 
| 187 | 
            +
                      when String then
         | 
| 188 | 
            +
                        new_value = v
         | 
| 189 | 
            +
                      when Proc then
         | 
| 190 | 
            +
                        new_value = v.call
         | 
| 191 | 
            +
                      else
         | 
| 192 | 
            +
                        new_value = v.to_s
         | 
| 193 | 
            +
                    end
         | 
| 194 | 
            +
                    new_value
         | 
| 195 | 
            +
                  end
         | 
| 196 | 
            +
             | 
| 138 197 | 
             
                  def find_localization_key(key_without_locale)
         | 
| 139 198 | 
             
                    @localization_keys ||= Lit.get_key_value_engine
         | 
| 140 199 | 
             
                    unless @localization_keys.has_key?(key_without_locale)
         | 
| 141 | 
            -
                       | 
| 142 | 
            -
                      @localization_keys[key_without_locale] = localization_key.id
         | 
| 143 | 
            -
                      #Lit.init.logger.info "creating key: #{key_without_locale} with id #{localization_key.id}"
         | 
| 144 | 
            -
                      localization_key
         | 
| 200 | 
            +
                      find_or_create_localization_key(key_without_locale)
         | 
| 145 201 | 
             
                    else
         | 
| 146 | 
            -
                       | 
| 147 | 
            -
                      #Lit.init.logger.info "And I was looking for key: #{key_without_locale}"
         | 
| 148 | 
            -
                      #Lit.init.logger.info "And store has currently #{@localization_keys[key_without_locale]}"
         | 
| 149 | 
            -
                      #Lit.init.logger.info Lit::LocalizationKey.all
         | 
| 150 | 
            -
                      Lit::LocalizationKey.find(@localization_keys[key_without_locale])
         | 
| 202 | 
            +
                      Lit::LocalizationKey.find_by_id(@localization_keys[key_without_locale]) || find_or_create_localization_key(key_without_locale)
         | 
| 151 203 | 
             
                    end
         | 
| 152 204 | 
             
                  end
         | 
| 153 205 |  | 
| @@ -158,5 +210,20 @@ module Lit | |
| 158 210 | 
             
                    [locale_key, key_without_locale]
         | 
| 159 211 | 
             
                  end
         | 
| 160 212 |  | 
| 213 | 
            +
                  def find_or_create_localization_key(key_without_locale)
         | 
| 214 | 
            +
                    #Lit.init.logger.info "creating key: #{key_without_locale} with id #{localization_key.id}"
         | 
| 215 | 
            +
                    localization_key = Lit::LocalizationKey.where(:localization_key=>key_without_locale).first_or_create! 
         | 
| 216 | 
            +
                    @localization_keys[key_without_locale] = localization_key.id
         | 
| 217 | 
            +
                    localization_key
         | 
| 218 | 
            +
                  end
         | 
| 219 | 
            +
             | 
| 220 | 
            +
                  def update_hits_count(key)
         | 
| 221 | 
            +
                    if @hits_counter_working 
         | 
| 222 | 
            +
                      key_without_locale = split_key(key).last
         | 
| 223 | 
            +
                      @hits_counter.incr('hits_counter.'+key)
         | 
| 224 | 
            +
                      @hits_counter.incr('global_hits_counter.'+key_without_locale)
         | 
| 225 | 
            +
                    end
         | 
| 226 | 
            +
                  end
         | 
| 227 | 
            +
             | 
| 161 228 | 
             
              end
         | 
| 162 229 | 
             
            end
         | 
    
        data/lib/lit/i18n_backend.rb
    CHANGED
    
    | @@ -18,14 +18,13 @@ module Lit | |
| 18 18 | 
             
                end
         | 
| 19 19 |  | 
| 20 20 | 
             
                def available_locales
         | 
| 21 | 
            -
                  Lit::Locale.ordered.all.map{|l| l.locale.to_sym }
         | 
| 21 | 
            +
                  Lit::Locale.ordered.visible.all.map{|l| l.locale.to_sym }
         | 
| 22 22 | 
             
                end
         | 
| 23 23 |  | 
| 24 24 | 
             
                # Stores the given translations.
         | 
| 25 25 | 
             
                #
         | 
| 26 26 | 
             
                # @param [String] locale the locale (ie "en") to store translations for
         | 
| 27 27 | 
             
                # @param [Hash] data nested key-value pairs to be added as blurbs
         | 
| 28 | 
            -
                # @param [Hash] options unused part of the I18n API
         | 
| 29 28 | 
             
                def store_translations(locale, data, options = {})
         | 
| 30 29 | 
             
                  super
         | 
| 31 30 | 
             
                  #Lit.init.logger.info "store translation: #{locale}, data: #{data}, options: #{options}"
         | 
| @@ -36,13 +35,18 @@ module Lit | |
| 36 35 | 
             
                private
         | 
| 37 36 |  | 
| 38 37 | 
             
                def lookup(locale, key, scope = [], options = {})
         | 
| 39 | 
            -
                  #Lit.init.logger.info "lookup translation: #{key}, scope: #{scope}, options: #{options}"
         | 
| 40 38 | 
             
                  parts = I18n.normalize_keys(locale, key, scope, options[:separator])
         | 
| 41 39 | 
             
                  key_with_locale = parts.join('.')
         | 
| 40 | 
            +
                  
         | 
| 41 | 
            +
                  ## check in cache or in simple backend
         | 
| 42 42 | 
             
                  content = @cache[key_with_locale] || super
         | 
| 43 | 
            -
                   | 
| 44 | 
            -
                   | 
| 45 | 
            -
                   | 
| 43 | 
            +
                  
         | 
| 44 | 
            +
                  ## store value if not found
         | 
| 45 | 
            +
                  if content.nil?
         | 
| 46 | 
            +
                    @cache[key_with_locale] = options[:default] 
         | 
| 47 | 
            +
                    content = @cache[key_with_locale] 
         | 
| 48 | 
            +
                  end
         | 
| 49 | 
            +
                  ## return translated content
         | 
| 46 50 | 
             
                  content
         | 
| 47 51 | 
             
                end
         | 
| 48 52 |  | 
    
        data/lib/lit/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: lit
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0.4. | 
| 4 | 
            +
              version: 0.0.4.3
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2013- | 
| 12 | 
            +
            date: 2013-09-18 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: rails
         | 
| @@ -76,7 +76,7 @@ dependencies: | |
| 76 76 | 
             
                  - !ruby/object:Gem::Version
         | 
| 77 77 | 
             
                    version: '2.1'
         | 
| 78 78 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 79 | 
            -
              name:  | 
| 79 | 
            +
              name: pg
         | 
| 80 80 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| 81 81 | 
             
                none: false
         | 
| 82 82 | 
             
                requirements:
         | 
| @@ -374,6 +374,7 @@ files: | |
| 374 374 | 
             
            - app/assets/stylesheets/lit/dashboard.css
         | 
| 375 375 | 
             
            - app/controllers/lit/application_controller.rb
         | 
| 376 376 | 
             
            - app/controllers/lit/dashboard_controller.rb
         | 
| 377 | 
            +
            - app/controllers/lit/locales_controller.rb
         | 
| 377 378 | 
             
            - app/controllers/lit/localization_keys_controller.rb
         | 
| 378 379 | 
             
            - app/controllers/lit/localizations_controller.rb
         | 
| 379 380 | 
             
            - app/helpers/lit/application_helper.rb
         | 
| @@ -393,6 +394,9 @@ files: | |
| 393 394 | 
             
            - app/views/layouts/lit/_navigation.html.erb
         | 
| 394 395 | 
             
            - app/views/layouts/lit/application.html.erb
         | 
| 395 396 | 
             
            - app/views/lit/dashboard/index.html.erb
         | 
| 397 | 
            +
            - app/views/lit/locales/_hide_link.html.erb
         | 
| 398 | 
            +
            - app/views/lit/locales/hide.js.erb
         | 
| 399 | 
            +
            - app/views/lit/locales/index.html.erb
         | 
| 396 400 | 
             
            - app/views/lit/localization_keys/destroy.js.erb
         | 
| 397 401 | 
             
            - app/views/lit/localization_keys/index.html.erb
         | 
| 398 402 | 
             
            - app/views/lit/localization_keys/star.js.erb
         | 
| @@ -404,6 +408,7 @@ files: | |
| 404 408 | 
             
            - db/migrate/20121103174049_create_lit_localization_keys.rb
         | 
| 405 409 | 
             
            - db/migrate/20121103182106_create_lit_localizations.rb
         | 
| 406 410 | 
             
            - db/migrate/20121225112100_add_is_completed_and_is_starred_to_localization_keys.rb
         | 
| 411 | 
            +
            - db/migrate/20130511111904_add_is_hidden_to_locales.rb
         | 
| 407 412 | 
             
            - lib/lit/adapters/hash_storage.rb
         | 
| 408 413 | 
             
            - lib/lit/adapters/redis_storage.rb
         | 
| 409 414 | 
             
            - lib/lit/cache.rb
         | 
| @@ -438,7 +443,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 438 443 | 
             
                  version: '0'
         | 
| 439 444 | 
             
            requirements: []
         | 
| 440 445 | 
             
            rubyforge_project: 
         | 
| 441 | 
            -
            rubygems_version: 1.8. | 
| 446 | 
            +
            rubygems_version: 1.8.23
         | 
| 442 447 | 
             
            signing_key: 
         | 
| 443 448 | 
             
            specification_version: 3
         | 
| 444 449 | 
             
            summary: Database powered i18n backend with web gui
         |