merb_global 0.0.4.2 → 0.0.5
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/HISTORY +6 -0
- data/Rakefile +14 -1
- data/activerecord_generators/translations_migration/templates/translations_migration.erb +1 -1
- data/lib/merb_global/base.rb +45 -14
- data/lib/merb_global/controller.rb +20 -5
- data/lib/merb_global/date_providers/fork.rb +35 -0
- data/lib/merb_global/date_providers.rb +47 -0
- data/lib/merb_global/merbrake.rb +10 -10
- data/lib/merb_global/{providers → message_providers}/active_record.rb +43 -36
- data/lib/merb_global/{providers → message_providers}/data_mapper.rb +44 -46
- data/lib/merb_global/message_providers/gettext.rb +138 -0
- data/lib/merb_global/message_providers/gettext.treetop +60 -0
- data/lib/merb_global/{providers → message_providers}/mock.rb +4 -4
- data/lib/merb_global/message_providers/sequel.rb +105 -0
- data/lib/merb_global/message_providers/yaml.rb +101 -0
- data/lib/merb_global/message_providers.rb +175 -0
- data/lib/merb_global/numeric_providers/fork.rb +35 -0
- data/lib/merb_global/numeric_providers.rb +48 -0
- data/lib/merb_global/providers.rb +32 -44
- data/sequel_generators/translations_migration/templates/translations_migration.erb +1 -1
- metadata +21 -14
- data/lib/merb_global/provider.rb +0 -73
- data/lib/merb_global/providers/gettext.rb +0 -90
- data/lib/merb_global/providers/gettext.treetop +0 -74
- data/lib/merb_global/providers/sequel.rb +0 -100
- data/lib/merb_global/providers/yaml.rb +0 -114
- data/lib/merb_global/transfer.rb +0 -79
@@ -1,51 +1,39 @@
|
|
1
1
|
module Merb
|
2
2
|
module Global
|
3
3
|
module Providers
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
#
|
28
|
-
# ==== Returns
|
29
|
-
# localedir<String>>:: Directory where the locales are stored
|
30
|
-
def self.localedir
|
31
|
-
localedir =
|
32
|
-
if Merb::Global.config :flat
|
33
|
-
'locale'
|
34
|
-
else
|
35
|
-
Merb::Global.config :localedir, File.join('app', 'locale')
|
4
|
+
def self.included(mod) #:nodoc:
|
5
|
+
mod.module_eval do
|
6
|
+
@@providers = {}
|
7
|
+
@@providers_classes = {}
|
8
|
+
##
|
9
|
+
# Creates a provider and/or returns already created one
|
10
|
+
#
|
11
|
+
# ==== Parames
|
12
|
+
# provider<~to_s,~to_sym>:: A name of provider
|
13
|
+
#
|
14
|
+
# ==== Returns
|
15
|
+
# provider<Provider>:: A new provider
|
16
|
+
def self.[](provider)
|
17
|
+
unless @@providers.include? provider.to_sym
|
18
|
+
if @@providers_classes[provider.to_sym]
|
19
|
+
@@providers[provider.to_sym] =
|
20
|
+
@@providers_classes[provider.to_sym].new
|
21
|
+
else
|
22
|
+
require "merb_global/#{self.name.split("::").last.snake_case}/#{provider.to_s}"
|
23
|
+
@@providers[provider.to_sym] = eval "#{self.name}::#{provider.camel_case}.new"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
@@providers[provider.to_sym]
|
36
27
|
end
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
# provider<Provider>:: Returns provider
|
47
|
-
def self.provider
|
48
|
-
@@provider ||= self[Merb::Global.config(:provider, 'gettext')]
|
28
|
+
# Registers the class under the given name
|
29
|
+
#
|
30
|
+
# ==== Parameters
|
31
|
+
# name<~to_sym>:: Name under which it is registered
|
32
|
+
# klass<Class>:: Class of the provider
|
33
|
+
def self.register(name, klass)
|
34
|
+
@@providers_classes[name.to_sym] = klass
|
35
|
+
end
|
36
|
+
end
|
49
37
|
end
|
50
38
|
end
|
51
39
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: merb_global
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Coles
|
@@ -10,11 +10,12 @@ autorequire: merb_global
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2008-
|
13
|
+
date: 2008-07-29 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: merb-core
|
18
|
+
type: :runtime
|
18
19
|
version_requirement:
|
19
20
|
version_requirements: !ruby/object:Gem::Requirement
|
20
21
|
requirements:
|
@@ -24,6 +25,7 @@ dependencies:
|
|
24
25
|
version:
|
25
26
|
- !ruby/object:Gem::Dependency
|
26
27
|
name: treetop
|
28
|
+
type: :runtime
|
27
29
|
version_requirement:
|
28
30
|
version_requirements: !ruby/object:Gem::Requirement
|
29
31
|
requirements:
|
@@ -49,23 +51,28 @@ files:
|
|
49
51
|
- TODO
|
50
52
|
- HISTORY
|
51
53
|
- lib/merb_global
|
54
|
+
- lib/merb_global/message_providers
|
55
|
+
- lib/merb_global/message_providers/yaml.rb
|
56
|
+
- lib/merb_global/message_providers/sequel.rb
|
57
|
+
- lib/merb_global/message_providers/gettext.rb
|
58
|
+
- lib/merb_global/message_providers/data_mapper.rb
|
59
|
+
- lib/merb_global/message_providers/mock.rb
|
60
|
+
- lib/merb_global/message_providers/active_record.rb
|
61
|
+
- lib/merb_global/message_providers/gettext.treetop
|
62
|
+
- lib/merb_global/message_providers.rb
|
52
63
|
- lib/merb_global/plural.rb
|
53
64
|
- lib/merb_global/providers.rb
|
54
|
-
- lib/merb_global/provider.rb
|
55
65
|
- lib/merb_global/plural.treetop
|
66
|
+
- lib/merb_global/numeric_providers.rb
|
56
67
|
- lib/merb_global/config.rb
|
68
|
+
- lib/merb_global/date_providers
|
69
|
+
- lib/merb_global/date_providers/fork.rb
|
57
70
|
- lib/merb_global/controller.rb
|
58
|
-
- lib/merb_global/transfer.rb
|
59
71
|
- lib/merb_global/base.rb
|
72
|
+
- lib/merb_global/numeric_providers
|
73
|
+
- lib/merb_global/numeric_providers/fork.rb
|
60
74
|
- lib/merb_global/merbrake.rb
|
61
|
-
- lib/merb_global/
|
62
|
-
- lib/merb_global/providers/yaml.rb
|
63
|
-
- lib/merb_global/providers/sequel.rb
|
64
|
-
- lib/merb_global/providers/gettext.rb
|
65
|
-
- lib/merb_global/providers/data_mapper.rb
|
66
|
-
- lib/merb_global/providers/mock.rb
|
67
|
-
- lib/merb_global/providers/active_record.rb
|
68
|
-
- lib/merb_global/providers/gettext.treetop
|
75
|
+
- lib/merb_global/date_providers.rb
|
69
76
|
- lib/merb_global.rb
|
70
77
|
- sequel_generators/translations_migration
|
71
78
|
- sequel_generators/translations_migration/USAGE
|
@@ -98,8 +105,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
105
|
version:
|
99
106
|
requirements: []
|
100
107
|
|
101
|
-
rubyforge_project:
|
102
|
-
rubygems_version: 1.
|
108
|
+
rubyforge_project: merb-global
|
109
|
+
rubygems_version: 1.2.0
|
103
110
|
signing_key:
|
104
111
|
specification_version: 2
|
105
112
|
summary: Localization (L10n) and Internationalization (i18n) support for the Merb MVC Framework
|
data/lib/merb_global/provider.rb
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
module Merb
|
2
|
-
module Global
|
3
|
-
# Merb-global is able to store the translations in different types of
|
4
|
-
# storage. An interface betwean merb-global and those storages are
|
5
|
-
# providers.
|
6
|
-
#
|
7
|
-
# Please note that it is not required to include this module - despite it
|
8
|
-
# is recomended both as a documentation part and the more customized
|
9
|
-
# error messages.
|
10
|
-
module Provider
|
11
|
-
# call-seq:
|
12
|
-
# translate_to(singular, plural, opts) => translated
|
13
|
-
#
|
14
|
-
# Translate string using specific provider.
|
15
|
-
# It should be overloaded by the implementator.
|
16
|
-
#
|
17
|
-
# Do not use this method directly - use Merb::Global._ instead
|
18
|
-
#
|
19
|
-
# ==== Parameters
|
20
|
-
# singular<String>:: A string to translate
|
21
|
-
# plural<String>:: A plural form of string (nil if only singular)
|
22
|
-
# opts<Hash>:: An options hash (see below)
|
23
|
-
#
|
24
|
-
# ==== Options (opts)
|
25
|
-
# :lang<String>:: A language to translate on
|
26
|
-
# :n<Fixnum>:: A number of objects
|
27
|
-
#
|
28
|
-
# ==== Returns
|
29
|
-
# translated<String>:: A translated string
|
30
|
-
#
|
31
|
-
# ==== Raises
|
32
|
-
# NoMethodError:: Raised by default implementation. Should not be thrown.
|
33
|
-
def translate_to(singular, plural, opts)
|
34
|
-
raise NoMethodError.new('method translate_to has not been implemented')
|
35
|
-
end
|
36
|
-
|
37
|
-
# call-seq:
|
38
|
-
# support?(lang) => supported
|
39
|
-
#
|
40
|
-
# Checks if the language is supported (i.e. if the translation exists).
|
41
|
-
#
|
42
|
-
# In normal merb app the language is checked automatically in controller
|
43
|
-
# so probably you don't have to use this method
|
44
|
-
#
|
45
|
-
# ==== Parameters
|
46
|
-
# lang<String>:: A code of language
|
47
|
-
#
|
48
|
-
# ==== Returns
|
49
|
-
# supported<Boolean>:: Is a program translated to this language
|
50
|
-
#
|
51
|
-
# ==== Raises
|
52
|
-
# NoMethodError:: Raised by default implementation. Should not be thrown.
|
53
|
-
def support?(lang)
|
54
|
-
raise NoMethodError.new('method support? has not been implemented')
|
55
|
-
end
|
56
|
-
|
57
|
-
# This method creates basic files and/or directory structures
|
58
|
-
# (for example it adds migration) needed for provider to work.
|
59
|
-
#
|
60
|
-
# It is called from Rakefile.
|
61
|
-
def create!
|
62
|
-
raise NoMethodError.new('method create! has not been implemented')
|
63
|
-
end
|
64
|
-
|
65
|
-
# This method choos an supported language except those form the list
|
66
|
-
# given. It may fallback to english if none language can be found
|
67
|
-
# which agree with those criteria
|
68
|
-
def choose(except)
|
69
|
-
raise NoMethodError.new('method choose has not been implemented')
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
@@ -1,90 +0,0 @@
|
|
1
|
-
require 'gettext'
|
2
|
-
require 'treetop'
|
3
|
-
require 'pathname'
|
4
|
-
|
5
|
-
# I'm not sure if it is the correct way of doing it.
|
6
|
-
# As far it seems to be simpler.
|
7
|
-
class Thread #:nodoc:
|
8
|
-
def gettext_context
|
9
|
-
@gettext_context ||= Merb::Global::Providers::Gettext::GettextContext.new
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
module Merb
|
14
|
-
module Global
|
15
|
-
module Providers
|
16
|
-
class Gettext #:nodoc: all
|
17
|
-
include Merb::Global::Provider
|
18
|
-
include Merb::Global::Provider::Importer
|
19
|
-
|
20
|
-
def translate_to(singular, plural, opts)
|
21
|
-
context = Thread.current.gettext_context
|
22
|
-
context.set_locale opts[:lang], true
|
23
|
-
unless plural.nil?
|
24
|
-
context.ngettext singular, plural, opts[:n]
|
25
|
-
else
|
26
|
-
context.gettext singular
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def support?(lang)
|
31
|
-
lang == 'en' ||
|
32
|
-
File.exist?(File.join(Merb::Global::Providers.localedir, lang))
|
33
|
-
end
|
34
|
-
|
35
|
-
def create!
|
36
|
-
File.mkdirs Merb::Global::Providers.localedir
|
37
|
-
end
|
38
|
-
|
39
|
-
def choose(except)
|
40
|
-
dir = Dir[Merb::Global::Providers.localedir + '/*/']
|
41
|
-
dir.collect! {|p| File.basename p}
|
42
|
-
dir << 'en'
|
43
|
-
dir.reject! {|lang| except.include? lang}
|
44
|
-
dir.first
|
45
|
-
end
|
46
|
-
|
47
|
-
def import(exporter, export_data)
|
48
|
-
Treetop.load(Pathname(__FILE__).dirname.expand_path.to_s +
|
49
|
-
'/gettext')
|
50
|
-
parser = Merb::Global::Providers::GetTextParser.new
|
51
|
-
Dir[Merb::Global::Providers.localedir + '/*.po'].each do |file|
|
52
|
-
lang_name = File.basename file, '.po'
|
53
|
-
lang_tree = nil
|
54
|
-
open file do |data|
|
55
|
-
lang_tree = parser.parse data.read
|
56
|
-
end
|
57
|
-
opts = (lang_tree.to_hash)[''].split("\n")
|
58
|
-
plural_line = nil
|
59
|
-
for l in opts
|
60
|
-
if l[0..."Plural-Forms:".length] == "Plural-Forms:"
|
61
|
-
plural_line = l
|
62
|
-
break
|
63
|
-
end
|
64
|
-
end
|
65
|
-
plural_line =
|
66
|
-
plural_line["Plural-Forms:".length...plural_line.length]
|
67
|
-
plural_line = plural_line[0...plural_line.length-1]
|
68
|
-
plural_line = plural_line.gsub(/[[:space:]]/, '').split(/[=;]/, 4)
|
69
|
-
plural_line = Hash[*plural_line]
|
70
|
-
exporter.export_language export_data, lang_name,
|
71
|
-
plural_line['nplurals'].to_i,
|
72
|
-
plural_line['plural'] do |lang_data|
|
73
|
-
lang_tree.visit do |msgid, msgid_plural, msgstr, index|
|
74
|
-
exporter.export_string lang_data, msgid, msgid_plural,
|
75
|
-
index, msgstr
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
class GettextContext
|
82
|
-
include ::GetText
|
83
|
-
bindtextdomain Merb::Global.config([:gettext, :domain], 'merbapp'),
|
84
|
-
Merb::Global::Providers.localedir
|
85
|
-
public :set_locale, :ngettext, :gettext
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
@@ -1,74 +0,0 @@
|
|
1
|
-
module Merb
|
2
|
-
module Global
|
3
|
-
module Providers
|
4
|
-
grammar GetText
|
5
|
-
rule po_file
|
6
|
-
entry* {
|
7
|
-
def to_hash
|
8
|
-
hash = {}
|
9
|
-
elements.each {|entry| hash.merge! entry.to_hash}
|
10
|
-
hash
|
11
|
-
end
|
12
|
-
def visit &block
|
13
|
-
elements.each {|elem| elem.visit &block}
|
14
|
-
end
|
15
|
-
}
|
16
|
-
end
|
17
|
-
|
18
|
-
rule entry
|
19
|
-
whitespaces*
|
20
|
-
"msgid" whitespaces msgid:strings
|
21
|
-
"msgstr" whitespaces msgstr:strings {
|
22
|
-
def to_hash
|
23
|
-
{msgid.to_string => msgstr.to_string}
|
24
|
-
end
|
25
|
-
def visit &block
|
26
|
-
unless msgid.to_string.empty? or msgstr.to_string.empty?
|
27
|
-
yield msgid.to_string, nil, msgstr.to_string, nil
|
28
|
-
end
|
29
|
-
end
|
30
|
-
}
|
31
|
-
/
|
32
|
-
whitespaces*
|
33
|
-
"msgid" whitespaces msgid:strings
|
34
|
-
"msgid_plural" whitespaces msgid_plural:strings
|
35
|
-
msgstrs:("msgstr[" number:[0-9]+ "]" whitespaces strings)+ {
|
36
|
-
def to_hash
|
37
|
-
hash = {:plural => msgid_plural.to_string}
|
38
|
-
msgstrs.elements.each do |msgstr|
|
39
|
-
hash[msgstr.number.text_value.to_i] = msgstr.strings.to_string
|
40
|
-
end
|
41
|
-
{msgid.to_string => hash}
|
42
|
-
end
|
43
|
-
def visit &block
|
44
|
-
msgstrs.elements.each do |msgstr|
|
45
|
-
yield msgid.to_string, msgid_plural.to_string,
|
46
|
-
msgstr.strings.to_string, msgstr.number.text_value.to_i
|
47
|
-
end
|
48
|
-
end
|
49
|
-
}
|
50
|
-
end
|
51
|
-
|
52
|
-
rule strings
|
53
|
-
(string whitespaces?)+ {
|
54
|
-
def to_string
|
55
|
-
elements.collect {|element| element.string.to_string}.join
|
56
|
-
end
|
57
|
-
}
|
58
|
-
end
|
59
|
-
|
60
|
-
rule string
|
61
|
-
'"' content:((!'"' ('\"' / .))*) '"' {
|
62
|
-
def to_string
|
63
|
-
content.text_value.gsub(/\\n/, "\n")
|
64
|
-
end
|
65
|
-
}
|
66
|
-
end
|
67
|
-
|
68
|
-
rule whitespaces
|
69
|
-
(" " / "\t" / "\n" / ('#' (!"\n" .)* "\n") )+
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
@@ -1,100 +0,0 @@
|
|
1
|
-
require 'sequel'
|
2
|
-
require 'merb_global/plural'
|
3
|
-
|
4
|
-
module Merb
|
5
|
-
module Global
|
6
|
-
module Providers
|
7
|
-
class Sequel #:nodoc: all
|
8
|
-
include Merb::Global::Provider
|
9
|
-
include Merb::Global::Provider::Importer
|
10
|
-
include Merb::Global::Provider::Exporter
|
11
|
-
|
12
|
-
def translate_to(singular, plural, opts)
|
13
|
-
language = Language[:name => opts[:lang]] # I hope it's from MemCache
|
14
|
-
unless language.nil?
|
15
|
-
unless plural.nil?
|
16
|
-
n = Plural.which_form opts[:n], language[:plural]
|
17
|
-
translation = Translation[language.pk, singular, n]
|
18
|
-
else
|
19
|
-
translation = Translation[language.pk, singular, nil]
|
20
|
-
end
|
21
|
-
return translation[:msgstr] unless translation.nil?
|
22
|
-
end
|
23
|
-
return opts[:n] > 1 ? plural : singular # Fallback if not in database
|
24
|
-
end
|
25
|
-
|
26
|
-
def support?(lang)
|
27
|
-
Language.filter(:name => lang).count != 0
|
28
|
-
end
|
29
|
-
|
30
|
-
def create!
|
31
|
-
migration_exists = Dir[File.join(Merb.root, 'schema',
|
32
|
-
'migrations', "*.rb")].detect do |f|
|
33
|
-
f =~ /translations\.rb/
|
34
|
-
end
|
35
|
-
if migration_exists
|
36
|
-
puts "\nThe Translation Migration File already exists\n\n"
|
37
|
-
else
|
38
|
-
sh %{merb-gen translations_migration}
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def choose(except)
|
43
|
-
Language.filter(~{:name => except}).first[:name]
|
44
|
-
end
|
45
|
-
|
46
|
-
def import(exporter, export_data)
|
47
|
-
DB.transaction do
|
48
|
-
Language.each do |language|
|
49
|
-
exporter.export_language export_data, language[:name],
|
50
|
-
language[:nplural],
|
51
|
-
language[:plural] do |lang|
|
52
|
-
language.translations.each do |translation|
|
53
|
-
exporter.export_string lang,
|
54
|
-
translation[:msgid],
|
55
|
-
translation[:msgid_plural],
|
56
|
-
translation[:msgstr_index],
|
57
|
-
translation[:msgstr]
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def export
|
65
|
-
DB.transaction do
|
66
|
-
Language.delete_all
|
67
|
-
Translation.delete_all
|
68
|
-
yield nil
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def export_language(export_data, language, nplural, plural)
|
73
|
-
lang = Language.create :name => language, :nplural => nplural,
|
74
|
-
:plural => plural
|
75
|
-
raise unless lang
|
76
|
-
yield lang
|
77
|
-
end
|
78
|
-
|
79
|
-
def export_string(language_id, msgid, msgid_plural,
|
80
|
-
msgstr, msgstr_index)
|
81
|
-
Translation.create(:language_id => language_id,
|
82
|
-
:msgid => msgid,
|
83
|
-
:msgid_plural => msgid_plural,
|
84
|
-
:msgstr => msgstr,
|
85
|
-
:msgstr_index => msgstr_index) or raise
|
86
|
-
end
|
87
|
-
|
88
|
-
class Language < ::Sequel::Model(:merb_global_languages)
|
89
|
-
has_many :translations,
|
90
|
-
:class => "Merb::Global::Providers::Sequel::Translation",
|
91
|
-
:key => :language_id
|
92
|
-
end
|
93
|
-
|
94
|
-
class Translation < ::Sequel::Model(:merb_global_translations)
|
95
|
-
set_primary_key :language_id, :msgid, :msgstr_index
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
@@ -1,114 +0,0 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
require 'fileutils'
|
3
|
-
|
4
|
-
module Merb
|
5
|
-
module Global
|
6
|
-
module Providers
|
7
|
-
class Yaml #:nodoc:
|
8
|
-
include Merb::Global::Provider
|
9
|
-
include Merb::Global::Provider::Importer
|
10
|
-
include Merb::Global::Provider::Exporter
|
11
|
-
|
12
|
-
def initialize
|
13
|
-
# Not synchronized - make GC do it's work (may be not optimal
|
14
|
-
# but I don't think that some problem will occure).
|
15
|
-
# Shouldn't it be sort of cache with some expiration limit?
|
16
|
-
@lang = Hash.new
|
17
|
-
end
|
18
|
-
|
19
|
-
def translate_to(singular, plural, opts)
|
20
|
-
unless @lang.include? opts[:lang]
|
21
|
-
file = File.join Merb::Global::Providers.localedir,
|
22
|
-
opts[:lang] + '.yaml'
|
23
|
-
if File.exist? file
|
24
|
-
@lang[opts[:lang]] = YAML.load_file file
|
25
|
-
else
|
26
|
-
@lang[opts[:lang]] = nil
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
unless @lang[opts[:lang]].nil?
|
31
|
-
lang = @lang[opts[:lang]]
|
32
|
-
unless lang[singular].nil?
|
33
|
-
unless plural.nil?
|
34
|
-
n = Merb::Global::Plural.which_form opts[:n], lang[:plural]
|
35
|
-
return lang[singular][n] unless lang[singular][n].nil?
|
36
|
-
else
|
37
|
-
return lang[singular] unless lang[singular].nil?
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
return opts[:n] > 1 ? plural : singular
|
42
|
-
end
|
43
|
-
|
44
|
-
def support?(lang)
|
45
|
-
unless @lang.include? lang
|
46
|
-
file = File.join Merb::Global::Providers.localedir, lang + '.yaml'
|
47
|
-
@lang[lang] = YAML.load_file file if File.exist? file
|
48
|
-
end
|
49
|
-
not @lang[lang].nil?
|
50
|
-
end
|
51
|
-
|
52
|
-
def create!
|
53
|
-
FileUtils.mkdir_p Merb::Global::Providers.localedir
|
54
|
-
end
|
55
|
-
|
56
|
-
def choose(except)
|
57
|
-
dir = Dir[Merb::Global::Providers.localedir + '/*.yaml']
|
58
|
-
dir.collect! {|p| File.basename p, '.yaml'}
|
59
|
-
dir.reject! {|lang| except.include? lang}
|
60
|
-
dir.first
|
61
|
-
end
|
62
|
-
|
63
|
-
def import(exporter, export_data)
|
64
|
-
Dir[Merb::Global::Providers.localedir + '/*.yaml'].each do |file|
|
65
|
-
lang_name = File.basename file, '.yaml'
|
66
|
-
lang = YAML.load_file file
|
67
|
-
exporter.export_language export_data,
|
68
|
-
lang_name,
|
69
|
-
lang[:nplural],
|
70
|
-
lang[:plural] do |lang_data|
|
71
|
-
lang.each do |msgid, msgstr|
|
72
|
-
if msgid.is_a? String
|
73
|
-
if msgstr.is_a? Hash
|
74
|
-
msgid_plural = msgstr[:plural]
|
75
|
-
msgstr.each do |msgstr_index, msgstr|
|
76
|
-
if msgstr_index.is_a? Fixnum
|
77
|
-
exporter.export_string lang_data, msgid, msgid_plural,
|
78
|
-
msgstr_index, msgstr
|
79
|
-
end
|
80
|
-
end
|
81
|
-
else
|
82
|
-
exporter.export_string lang_data, msgid, nil, nil, msgstr
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def export
|
91
|
-
yield nil
|
92
|
-
end
|
93
|
-
|
94
|
-
def export_language(export_data, language, nplural, plural)
|
95
|
-
lang = {:nplural => nplural, :plural => plural}
|
96
|
-
yield lang
|
97
|
-
file = "#{Merb::Global::Providers.localedir}/#{language}.yaml"
|
98
|
-
open file, 'w+' do |out|
|
99
|
-
YAML.dump lang, out
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
def export_string(language, msgid, msgstr, msgstr_index)
|
104
|
-
if no.nil?
|
105
|
-
language[msgid] = msgstr
|
106
|
-
else
|
107
|
-
language[msgid] ||= {}
|
108
|
-
language[msgid][no] = msgstr
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|