grosser-fast_gettext 0.2.11 → 0.3.0

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.
@@ -56,7 +56,7 @@ small translation file <-> large translation file
56
56
 
57
57
  Thread Safety and Rails
58
58
  =======================
59
- `text_domains` repository are not stored thread-save, so that they can be added inside the `environment.rb`,
59
+ Parsed `text_domains` are not stored thread-save, so that they can be added inside the `environment.rb`,
60
60
  and do not need to be readded for every thread (parsing takes time...).
61
61
 
62
62
  ###Rails
@@ -89,8 +89,13 @@ Updating translations
89
89
  =====================
90
90
  ATM you have to use the [original GetText](http://github.com/mutoh/gettext) to create and manage your po/mo-files.
91
91
 
92
- Plugins
93
- =======
92
+ Advanced features
93
+ =================
94
+ ###Abnormal pluralisation
95
+ Pluralisation rules can be set directly via a lambda (see code/specs), or by using the Gettext
96
+ plural definition (see spec/locale/en/test_plural.po or [GNU Gettext documentation](http://www.gnu.org/software/libtool/manual/libc/Advanced-gettext-functions.html).
97
+
98
+ ###Plugins
94
99
  Want a yml, xml, database version ?
95
100
  Write your own TranslationRepository!
96
101
  #fast_gettext/translation_repository/xxx.rb
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 11
3
2
  :major: 0
4
- :minor: 2
3
+ :minor: 3
4
+ :patch: 0
@@ -19,14 +19,20 @@ module FastGettext
19
19
  @data[key]
20
20
  end
21
21
 
22
- def plural(singular,plural,count)
23
- translations = plural_translations(singular,plural)
22
+ #returns the plural forms or all singlgular translations that where found
23
+ def plural(*msgids)
24
+ translations = plural_translations(msgids)
25
+ return translations unless translations.empty?
26
+ msgids.map{|msgid| self[msgid] || msgid} #try to translate each id
27
+ end
24
28
 
25
- if count == 1
26
- translations[0] || self[singular]
27
- else
28
- translations[1] || self[plural]
29
+ def pluralisation_rule
30
+ #gettext uses 0 as default rule, which would turn off all pluralisation, very clever...
31
+ #additionally parsing fails when directly accessing po files, so this line was taken from gettext/mofile
32
+ (@data['']||'').split("\n").each do |line|
33
+ return lambda{|n|eval($2)} if /^Plural-Forms:\s*nplurals\s*\=\s*(\d*);\s*plural\s*\=\s*([^;]*)\n?/ =~ line
29
34
  end
35
+ nil
30
36
  end
31
37
 
32
38
  def self.empty
@@ -47,12 +53,12 @@ module FastGettext
47
53
  end
48
54
 
49
55
  def split_plurals(singular_plural)
50
- singular_plural.split(PLURAL_SEPERATOR,2)
56
+ singular_plural.split(PLURAL_SEPERATOR)
51
57
  end
52
58
 
53
59
  # Car, Cars => [Auto,Autos] or []
54
- def plural_translations(singular,plural)
55
- plurals = self[singular+PLURAL_SEPERATOR+plural]
60
+ def plural_translations(msgids)
61
+ plurals = self[msgids*PLURAL_SEPERATOR]
56
62
  if plurals then split_plurals(plurals) else [] end
57
63
  end
58
64
  end
@@ -4,6 +4,7 @@ module FastGettext
4
4
  # - direct translation queries to the current repository
5
5
  # - handle untranslated values
6
6
  # - understand / enforce namespaces
7
+ # - decide which plural form is used
7
8
  module Translation
8
9
  extend self
9
10
 
@@ -22,13 +23,15 @@ module FastGettext
22
23
  end
23
24
 
24
25
  #translate pluralized
25
- def n_(singular,plural,count)
26
- if translation = FastGettext.current_repository.plural(singular,plural,count)
27
- translation
28
- else
29
- #TODO remove this repeated logic, e.g. return :plural / :singular or raise an exception ?
30
- count == 1 ? singular : plural
31
- end
26
+ # some languages have up to 4 plural forms...
27
+ # n_(singular, plural, plural form 2, ..., count)
28
+ # n_('apple','apples',3)
29
+ def n_(*msgids)
30
+ count = msgids.pop
31
+
32
+ translations = FastGettext.current_repository.plural(*msgids)
33
+ selected = FastGettext.current_repository.pluralisation_rule.call(count)
34
+ translations[selected] || msgids[selected] || msgids.last
32
35
  end
33
36
 
34
37
  #translate, but discard namespace if nothing was found
@@ -47,8 +50,8 @@ module FastGettext
47
50
  end
48
51
 
49
52
  #tell gettext: this string need translation (will be found during parsing)
50
- def Nn_(singular,plural)
51
- [singular,plural]
53
+ def Nn_(*msgids)
54
+ msgids
52
55
  end
53
56
  end
54
57
  end
@@ -5,11 +5,17 @@ module FastGettext
5
5
  # - fallback as empty repository, that cannot translate anything but does not crash
6
6
  class Base
7
7
  attr_accessor :locale
8
+ attr_writer :pluralisation_rule
9
+
8
10
  def initialize(name,options={})
9
11
  @name = name
10
12
  @options = options
11
13
  end
12
14
 
15
+ def pluralisation_rule
16
+ @pluralisation_rule || lambda{|n| n==1 ? 0 : 1}
17
+ end
18
+
13
19
  def available_locales
14
20
  []
15
21
  end
@@ -18,8 +24,8 @@ module FastGettext
18
24
  current_translations[key]
19
25
  end
20
26
 
21
- def plural(singular,plural,count)
22
- current_translations.plural(singular,plural,count)
27
+ def plural(*msgids)
28
+ current_translations.plural(*msgids)
23
29
  end
24
30
 
25
31
  protected
@@ -6,18 +6,27 @@ module FastGettext
6
6
  # - provide access to translations in mo files
7
7
  class Mo < Base
8
8
  def initialize(name,options={})
9
- # parse all .mo files with the right name, that sit in locale/LC_MESSAGES folders
10
- find_files_in_locale_folders(File.join('LC_MESSAGES',"#{name}.mo"),options[:path]) do |locale,file|
11
- @files[locale] = MoFile.new(file)
12
- end
9
+ find_and_store_files(name,options)
10
+ super
13
11
  end
14
12
 
15
13
  def available_locales
16
14
  @files.keys
17
15
  end
18
16
 
17
+ def pluralisation_rule
18
+ current_translations.pluralisation_rule || super
19
+ end
20
+
19
21
  protected
20
22
 
23
+ def find_and_store_files(name,options)
24
+ # parse all .mo files with the right name, that sit in locale/LC_MESSAGES folders
25
+ find_files_in_locale_folders(File.join('LC_MESSAGES',"#{name}.mo"),options[:path]) do |locale,file|
26
+ @files[locale] = MoFile.new(file)
27
+ end
28
+ end
29
+
21
30
  def current_translations
22
31
  @files[FastGettext.locale] || MoFile.empty
23
32
  end
@@ -6,7 +6,8 @@ module FastGettext
6
6
  # - find and store po files
7
7
  # - provide access to translations in po files
8
8
  class Po < Mo
9
- def initialize(name,options={})
9
+ protected
10
+ def find_and_store_files(name,options)
10
11
  require File.join(File.dirname(__FILE__),'..','..','..','vendor','poparser')
11
12
  require 'fast_gettext/mo_file'
12
13
  find_files_in_locale_folders("#{name}.po",options[:path]) do |locale,file|
@@ -16,8 +16,13 @@ describe MoFile do
16
16
  de['Car|Model'].should == nil
17
17
  end
18
18
  it "finds pluralized values" do
19
- de.plural('Axis','Axis',1).should == 'Achse' #singular
20
- de.plural('Axis','Axis',2).should == 'Achsen' #plurals
19
+ de.plural('Axis','Axis').should == ['Achse','Achsen']
20
+ end
21
+ it "returns singular translations when pluralisation could not be found" do
22
+ de.plural('Axis','Axis','Axis').should == ['Achse','Achse','Achse']
23
+ end
24
+ it "returns ids when not plural and singular translations could not be found" do
25
+ de.plural('Axis','Axis','NOTFOUND').should == ['Achse','Achse','NOTFOUND']
21
26
  end
22
27
  it "can access plurals through []" do
23
28
  de['Axis'].should == 'Achse' #singular
@@ -15,6 +15,6 @@ describe 'FastGettext::TranslationRepository::Base' do
15
15
  end
16
16
  it "cannot pluralize" do
17
17
  @rep.locale = 'de'
18
- @rep.plural('Axis','Axis',2).should == nil
18
+ @rep.plural('Axis','Axis').should == ['Axis','Axis']
19
19
  end
20
20
  end
@@ -16,6 +16,16 @@ describe 'FastGettext::TranslationRepository::Mo' do
16
16
  end
17
17
  it "can pluralize" do
18
18
  FastGettext.locale = 'de'
19
- @rep.plural('Axis','Axis',2).should == 'Achsen'
19
+ @rep.plural('Axis','Axis').should == ['Achse','Achsen']
20
+ end
21
+ it "stores pluralisation rule" do
22
+ @rep.pluralisation_rule = lambda{|n|n+1}
23
+ @rep.pluralisation_rule.call(3).should == 4
24
+ end
25
+ it "has access to the mo repositories pluralisation rule" do
26
+ FastGettext.locale = 'en'
27
+ rep = FastGettext::TranslationRepository.build('plural_test',:path=>File.join(current_folder,'..','..','locale'))
28
+ rep['car'].should == 'Test'#just check it is loaded correctly
29
+ rep.pluralisation_rule.call(2).should == 3
20
30
  end
21
31
  end
@@ -16,6 +16,12 @@ describe 'FastGettext::TranslationRepository::Po' do
16
16
  end
17
17
  it "can pluralize" do
18
18
  FastGettext.locale = 'de'
19
- @rep.plural('Axis','Axis',2).should == 'Achsen'
19
+ @rep.plural('Axis','Axis').should == ['Achse','Achsen']
20
+ end
21
+ it "has access to the mo repositories pluralisation rule" do
22
+ FastGettext.locale = 'en'
23
+ rep = FastGettext::TranslationRepository.build('plural_test',:path=>File.join(current_folder,'..','..','locale'),:type=>:po)
24
+ rep['car'].should == 'Test'#just check it is loaded correctly
25
+ rep.pluralisation_rule.call(2).should == 3
20
26
  end
21
27
  end
@@ -38,10 +38,26 @@ describe FastGettext::Translation do
38
38
  n_('Axis','Axis',2).should == 'Achsen'
39
39
  n_('Axis','Axis',0).should == 'Achsen'
40
40
  end
41
+
42
+ it "supports abstract pluralisation rules" do
43
+ begin
44
+ FastGettext.current_repository.pluralisation_rule = lambda{|n|2}
45
+ n_('a','b','c','d',4).should == 'c'
46
+ ensure
47
+ #restore default
48
+ FastGettext.current_repository.pluralisation_rule = lambda{|n|n==1?0:1}
49
+ end
50
+ end
51
+
41
52
  it "returns the appropriate msgid if no translation was found" do
42
53
  n_('NOTFOUND','NOTFOUNDs',1).should == 'NOTFOUND'
43
54
  n_('NOTFOUND','NOTFOUNDs',2).should == 'NOTFOUNDs'
44
55
  end
56
+
57
+ it "returns the last msgid when no translation was found and msgids where to short" do
58
+ FastGettext.current_repository.pluralisation_rule = lambda{|x|4}
59
+ n_('Apple','Apples',2).should == 'Apples'
60
+ end
45
61
  end
46
62
 
47
63
  describe :s_ do
@@ -0,0 +1,20 @@
1
+ # SOME DESCRIPTIVE TITLE.
2
+ # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
3
+ # This file is distributed under the same license as the PACKAGE package.
4
+ # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
5
+ #
6
+ #, fuzzy
7
+ msgid ""
8
+ msgstr ""
9
+ "Project-Id-Version: version 0.0.1\n"
10
+ "POT-Creation-Date: 2009-02-26 19:50+0100\n"
11
+ "PO-Revision-Date: 2009-02-18 15:42+0100\n"
12
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
13
+ "Language-Team: LANGUAGE <LL@li.org>\n"
14
+ "MIME-Version: 1.0\n"
15
+ "Content-Type: text/plain; charset=UTF-8\n"
16
+ "Content-Transfer-Encoding: 8bit\n"
17
+ "Plural-Forms: nplurals=2; plural=n==2?3:4;\n"
18
+
19
+ msgid "car"
20
+ msgstr "Test"
@@ -0,0 +1 @@
1
+ 0.3 -- pluralisation methods accept/return n plural forms, contrary to singular/plural before
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grosser-fast_gettext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.11
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-28 00:00:00 -08:00
12
+ date: 2009-03-09 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -51,7 +51,9 @@ files:
51
51
  - spec/locale/de/test.po
52
52
  - spec/locale/en
53
53
  - spec/locale/en/LC_MESSAGES
54
+ - spec/locale/en/LC_MESSAGES/plural_test.mo
54
55
  - spec/locale/en/LC_MESSAGES/test.mo
56
+ - spec/locale/en/plural_test.po
55
57
  - spec/locale/en/test.po
56
58
  - spec/spec_helper.rb
57
59
  - spec/vendor
@@ -59,6 +61,7 @@ files:
59
61
  - spec/vendor/fake_load_path/iconv.rb
60
62
  - spec/vendor/iconv_spec.rb
61
63
  - spec/vendor/string_spec.rb
64
+ - vendor/CHANGELOG
62
65
  - vendor/README.rdoc
63
66
  - vendor/empty.mo
64
67
  - vendor/iconv.rb