Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit ced37c7131a5b3a0b91d43a0c7cd6438e686e5b8 0 parents
Paul Engel authored
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Paul Engel
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
56 README
@@ -0,0 +1,56 @@
+= e9s
+
+Enrichments (e9s) for Ruby Internationalization (i18n)
+
+== Introduction
+
+E9s was created due to the need of simply implementing i18n within a Rails application. It enriches I18n, Formastic, the String and Symbol classes to make life easier for the developer.
+
+== How to use
+
+1. Install the e9s plugin
+ script/plugin install git://github.com/archan937/e9s.git
+
+2. Install the formtastic plugin (optional)
+ script/plugin install git://github.com/justinfrench/formtastic.git
+
+3. Place predefined YML files (for the used locales) in /config/locals/ (optional)
+ download from http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
+
+4. Add translations within local YML files in /config/locals/
+
+5. Start translating within controllers, models and/or views
+
+6. Enjoy! :D
+
+== Documentation
+
+Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
+Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+
+== Credit
+
+This Rails plugin depends on:
+
+i18n
+http://github.com/svenfuchs/i18n
+
+Formtastic (optional)
+http://github.com/justinfrench/formtastic
+
+== License
+Copyright (c) 2010 Paul Engel, released under the MIT license
+http://holder.nl - http://gettopup.com - paul.engel@holder.nl
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
+persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
+Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 Rakefile
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the e9s plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the e9s plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'E9s'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
7 init.rb
@@ -0,0 +1,7 @@
+
+Dir.glob(File.join("**", "lib", "**", "*.rb")).each do |file|
+ require file
+end
+require "e9s"
+
+E9s::Plugin.init
1  install.rb
@@ -0,0 +1 @@
+# Install hook code here
165 lib/e9s.rb
@@ -0,0 +1,165 @@
+
+module E9s
+ module Plugin
+ extend self
+
+ def init
+ AVAILABLE_LOCALES.each do |locale|
+ I18n.locale = locale
+
+ (I18n.t! "e9s" rescue []).each do |type, inflections|
+ inflections.each do |inflection|
+ E9s::Inflector.inflections.send type, inflection
+ end
+ end
+ end
+
+ I18n.locale = I18n.default_locale
+ end
+
+ end
+
+ module Inflector
+ extend self
+
+ def inflections(locale = nil)
+ Inflections.instance.locale = locale
+ if block_given?
+ yield Inflections.instance
+ else
+ Inflections.instance
+ end
+ end
+
+ def singularize(word)
+ inflect :singulars, word
+ end
+
+ def pluralize(word, count = nil)
+ count == 1 ? singularize(word) : inflect(:plurals, word)
+
+ # result = begin
+ # if count == 1
+ # s
+ # elsif ends_with?("s")
+ # self
+ # else
+ # {
+ # "ja" => "ja",
+ # "vraag" => "vragen",
+ # "bod" => "biedingen",
+ # "aanvraag" => "aanvragen",
+ # "organisatie" => "organisaties",
+ # "aanbieder" => "aanbieders",
+ # "vraag & antwoord" => "vragen & antwoorden",
+ # "anoniem" => "anoniem"
+ # }[self] || "#{self}en"
+ # end
+ # end
+ # [count, result].compact.join " "
+
+ end
+
+ class Inflections
+ include Singleton
+
+ attr_reader :singulars, :plurals, :irregulars, :uncountables
+ attr_writer :locale
+
+ def initialize
+ %w(singulars plurals irregulars uncountables).each do |x|
+ instance_variable_set :"@#{x}", {}
+ end
+ @locale = I18n.default_locale
+ end
+
+ def singular(*args)
+ add_inflection :singulars , *args
+ end
+
+ def plural(*args)
+ add_inflection :plurals , *args
+ end
+
+ def irregular(*args)
+ add_inflection :irregulars, *args
+ end
+
+ # def uncountable(*args)
+ # (@uncountables[locale_for args.last] ||= []).concat words
+ # end
+
+ def dump
+ puts "SINGULARS : #{@singulars .inspect}"
+ puts "PLURAL : #{@plurals .inspect}"
+ puts "IRREGULARS : #{@irregulars .inspect}"
+ puts "UNCOUNTABLES: #{@uncountables.inspect}"
+ puts "LOCALE : #{@locale .inspect}"
+ end
+
+ private
+
+ def add_inflection(type, *args)
+ inflections = instance_variable_get(:"@#{type}")
+ locale = locale_for *args
+ inflection = Inflection.build *args
+
+ (inflections[locale] ||= []).push inflection
+ end
+
+ def locale_for(*args)
+ locale = args.first.is_a?(Hash) ? args.first[:locale] : args[3]
+
+ [locale, @locale].detect{|x| x.is_a?(Symbol)} || I18n.default_locale
+ end
+
+ Inflection = Struct.new(:rule, :replacement, :exceptions) do
+
+ def self.build(*args)
+ self.new *(args.first.is_a?(Hash) ? args.first.values_at(*%w(rule replacement exceptions)) : args)
+ end
+
+ def exceptions
+ (self[:exceptions] || "").downcase.split(",").collect{|x| x.strip}
+ end
+
+ def rule
+ Regexp.new self[:rule], Regexp::IGNORECASE
+ end
+
+ def inflect(word)
+ if exceptions.include?(word.downcase)
+ word
+ else
+ word.dup.gsub!(rule, self[:replacement]) || word
+ end
+ end
+
+ def inflect!(word)
+ (result = inflect(word)) == word ? nil : result
+ end
+
+ def inspect
+ to_hash.inspect
+ end
+
+ def to_hash
+ Hash[*members.zip(values).flatten]
+ end
+
+ end
+ end
+
+ private
+
+ def inflect(type, word)
+ (Inflections.instance.send(type)[I18n.locale] || []).each do |inflection|
+ if result = inflection.inflect!(word)
+ return result
+ end
+ end
+ word
+ end
+
+ end
+end
50 lib/e9s/formtastic.rb
@@ -0,0 +1,50 @@
+
+module Formtastic
+ module Enrichments
+
+ def self.included(base)
+ base.alias_method_chain :input, :enrichments
+ end
+
+ def input_with_enrichments(*args)
+ object = @object.class.name.underscore
+ method = args.shift
+ options = args.extract_options!
+ wrapper_tag = options.delete(:wrapper_tag)
+
+ unless options.include?(:label)
+ keys = [:"label.#{object}.#{method.to_s.capitalize}", :"label.#{method}", :"word.#{method}", method.to_s.humanize]
+
+ if @options.include?(:name)
+ keys.unshift :"label.forms.#{@options[:name]}.#{object}.#{method.to_s.capitalize}"
+ keys.unshift :"label.forms.#{@options[:name]}.#{method.to_s.capitalize}"
+ end
+
+ options[:label] = keys.shift.t :default => keys
+ end
+
+ unless options.include?(:seatholder)
+ keys = [:"seatholder.#{object}.#{method}", :"seatholder.#{method}", method, ""]
+
+ if @options.include?(:name)
+ keys.unshift :"seatholder.forms.#{@options[:name]}.#{object}.#{method.to_s.capitalize}"
+ keys.unshift :"seatholder.forms.#{@options[:name]}.#{method.to_s.capitalize}"
+ end
+
+ seatholder = keys.shift.t :default => keys
+ options[:seatholder] = seatholder unless seatholder.empty?
+ end
+
+ (options[:input_html] ||= {}).store :seatholder, options.delete(:seatholder) unless @object && @object.respond_to?(:errors) && !@object.errors[method.to_sym].blank?
+
+ if (output = input_without_enrichments method, options) and wrapper_tag
+ output.gsub(/^\<li/, "<#{wrapper_tag}").gsub(/\<\/li\>$/, "</#{wrapper_tag}>")
+ else
+ output
+ end
+ end
+
+ end
+end
+
+Formtastic::SemanticFormBuilder.send :include, Formtastic::Enrichments if defined?(Formtastic::SemanticFormBuilder)
15 lib/e9s/i18n.rb
@@ -0,0 +1,15 @@
+
+module I18n
+ class << self
+ def available_locales; backend.available_locales; end
+ end
+
+ module Backend
+ class Simple
+ def available_locales; translations.keys.sort{|a, b| a.to_s <=> b.to_s}; end
+ end
+ end
+end
+
+I18n.backend.send(:init_translations)
+AVAILABLE_LOCALES = I18n.backend.available_locales
59 lib/e9s/string.rb
@@ -0,0 +1,59 @@
+
+class String
+ def upcase_first
+ self[0].chr.capitalize + self[1, size]
+ end
+
+ def upcase_first!
+ self == (result = upcase_first) ? nil : replace(result)
+ end
+
+ def pluralize!
+ downcase == (result = pluralize).downcase ? nil : replace(result)
+ end
+
+ def cp_case(s)
+ send((:downcase unless s.dup.downcase!) || (:upcase_first unless s.dup.upcase_first!) || (:upcase unless s.dup.upcase!) || :to_s)
+ end
+
+ def t(options = {})
+ key = self.include?(".") ? self : "word.#{self}"
+ default = key.split(".").last
+ translating_word = key.starts_with?("word.")
+ opts = {:pluralize => true}.merge(options)
+
+ key.downcase!
+
+ if options.include? :default
+ puts "INFO: I18n.t #{key.inspect}, #{options.reject{|k, v| k == :pluralize}.inspect}" if RAILS_ENV == "development"
+ s = I18n.t key, options.reject{|k, v| k == :pluralize}
+ s = s[:_base] if s.is_a?(Hash)
+ else
+ puts "INFO: I18n.t #{key.inspect}, #{opts.merge({:default => translating_word ? "" : default.humanize}).reject{|k, v| k == :pluralize}.inspect}" if RAILS_ENV == "development"
+ s = I18n.t key, opts.merge({:default => translating_word ? "" : default.humanize}).reject{|k, v| k == :pluralize}
+
+ if translating_word
+ unless translated = !s.empty?
+ puts "INFO: I18n.t #{key.singularize.inspect}, #{opts.merge({:default => ""}).reject{|k, v| k == :pluralize}.inspect}" if RAILS_ENV == "development"
+ s = I18n.t key.singularize, opts.merge({:default => ""}).reject{|k, v| k == :pluralize}
+ end
+
+ if s.empty?
+ s = default.humanize
+ else
+ s = s.pl(opts[:count]) unless !opts[:pluralize] or (opts[:count].nil? and default.dup.pluralize!)
+ end
+ end
+ end
+
+ s.gsub!(/^=\s+/, "") ? s : s.cp_case(default)
+ end
+
+ def s
+ E9s::Inflector.singularize self
+ end
+
+ def pl(count = nil)
+ E9s::Inflector.pluralize self, count
+ end
+end
6 lib/e9s/symbol.rb
@@ -0,0 +1,6 @@
+
+class Symbol
+ def t(options = {})
+ to_s.t(options)
+ end
+end
185 locales/nl.yml
@@ -0,0 +1,185 @@
+---
+nl:
+
+ e9s:
+ singular:
+ - rule: en$
+ replacement: ""
+
+ plural:
+ - rule: | ^([^aeiou]+[aeiou])([^aeiou])$
+ replacement: \1\2\2en
+ exceptions: vat, dag, dak, pad, dal, slot, weg
+
+ - rule: | (a|e|o|u)\1([^aeiou])$
+ replacement: \1\2en
+
+ - rule: $
+ replacement: en
+
+ number:
+ format:
+ separator: ","
+ precision: 2
+ delimiter: .
+ human:
+ storage_units:
+ format: "%n %u"
+ units:
+ kb: KB
+ tb: TB
+ gb: GB
+ byte:
+ one: Byte
+ other: Bytes
+ mb: MB
+ currency:
+ format:
+ format: "%u %n"
+ unit: !binary |
+ 4oKs
+
+ separator: ","
+ precision: 2
+ delimiter: .
+ activerecord:
+ errors:
+ messages:
+ greater_than_or_equal_to: moet groter of gelijk zijn aan {{count}}
+ record_invalid: is ongeldig
+ confirmation: komt niet met de bevestiging overeen
+ less_than_or_equal_to: moet minder of gelijk zijn aan {{count}}
+ blank: moet opgegeven zijn
+ exclusion: is niet beschikbaar
+ invalid: is ongeldig
+ odd: moet oneven zijn
+ even: moet even zijn
+ too_short: is te kort (niet minder dan {{count}} tekens)
+ wrong_length: heeft niet de juiste lengte (moet {{count}} tekens lang zijn)
+ empty: moet opgegeven zijn
+ less_than: moet minder zijn dan {{count}}
+ equal_to: moet gelijk zijn aan {{count}}
+ greater_than: moet groter zijn dan {{count}}
+ too_long: is te lang (niet meer dan {{count}} tekens)
+ taken: is niet beschikbaar
+ accepted: moet worden geaccepteerd
+ inclusion: is niet in de lijst opgenomen
+ not_a_number: is geen getal
+ template:
+ body: "Controleer alstublieft de volgende velden:"
+ header:
+ one: "Kon dit {{model}} object niet opslaan: 1 fout."
+ other: "Kon dit {{model}} niet opslaan: {{count}} fouten."
+ time:
+ am: "'s ochtends"
+ formats:
+ default: "%a %d %b %Y %H:%M:%S %Z"
+ time: "%H:%M"
+ short: "%d %b %H:%M"
+ datetime:
+ formats:
+ default: "%d-%m-%YT%H:%M:%S%Z"
+ only_second: "%S"
+ long: "%d %B %Y %H:%M"
+ pm: "'s middags"
+ date:
+ month_names:
+ -
+ - januari
+ - februari
+ - maart
+ - april
+ - mei
+ - juni
+ - juli
+ - augustus
+ - september
+ - oktober
+ - november
+ - december
+ abbr_day_names:
+ - zon
+ - maa
+ - din
+ - woe
+ - don
+ - vri
+ - zat
+ order:
+ - :day
+ - :month
+ - :year
+ formats:
+ only_day: "%e"
+ default: "%d/%m/%Y"
+ short: "%e %b"
+ long: "%e %B %Y"
+ day_names:
+ - zondag
+ - maandag
+ - dinsdag
+ - woensdag
+ - donderdag
+ - vrijdag
+ - zaterdag
+ abbr_month_names:
+ -
+ - jan
+ - feb
+ - mar
+ - apr
+ - mei
+ - jun
+ - jul
+ - aug
+ - sep
+ - okt
+ - nov
+ - dec
+ support:
+ array:
+ words_connector: ","
+ last_word_connector: ", en"
+ two_words_connector: en
+ datetime:
+ format:
+ default: "%Y-%m-%dT%H:%M:%S%Z"
+ prompts:
+ minute: minuut
+ second: seconden
+ month: maand
+ hour: uur
+ day: dag
+ year: jaar
+ distance_in_words:
+ less_than_x_minutes:
+ one: "minder dan \xC3\xA9\xC3\xA9n minuut"
+ other: minder dan {{count}} minuten
+ x_days:
+ one: 1 dag
+ other: "{{count}} dagen"
+ x_seconds:
+ one: 1 seconde
+ other: "{{count}} seconden"
+ about_x_hours:
+ one: "ongeveer \xC3\xA9\xC3\xA9n uur"
+ other: ongeveer {{count}} uur
+ less_than_x_seconds:
+ one: "minder dan \xC3\xA9\xC3\xA9n seconde"
+ other: minder dan {{count}} seconden
+ x_months:
+ one: 1 maand
+ other: "{{count}} maanden"
+ x_minutes:
+ one: 1 minuut
+ other: "{{count}} minuten"
+ about_x_years:
+ one: "ongeveer \xC3\xA9\xC3\xA9n jaar"
+ other: ongeveer {{count}} jaren
+ about_x_months:
+ one: "ongeveer \xC3\xA9\xC3\xA9n maand"
+ other: ongeveer {{count}} maanden
+ over_x_years:
+ one: "langer dan \xC3\xA9\xC3\xA9n jaar"
+ other: langer {{count}} jaar
+ half_a_minute: halve minuut
4 tasks/e9s_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :e9s do
+# # Task goes here
+# end
8 test/e9s_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class E9sTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
3  test/test_helper.rb
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
1  uninstall.rb
@@ -0,0 +1 @@
+# Uninstall hook code here
Please sign in to comment.
Something went wrong with that request. Please try again.