Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: http://svn.coderepos.org/share/lang/ruby/as3gettext/trunk@4626 d0d07461-0603-4401-acd4-de1884942a52
- Loading branch information
secondlife
committed
Jan 15, 2008
0 parents
commit 84daf6a
Showing
13 changed files
with
395 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
== 0.0.1 / 2008-01-15 | ||
|
||
* initial release | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
= as3gettext | ||
|
||
|
||
== Description | ||
|
||
|
||
== Installation | ||
|
||
=== Archive Installation | ||
|
||
rake install | ||
|
||
=== Gem Installation | ||
|
||
gem install as3gettext | ||
|
||
|
||
== Features/Problems | ||
|
||
|
||
== Synopsis | ||
|
||
|
||
== Copyright | ||
|
||
Author:: Yuichi Tateno <hotchpotch@no.spam@gmail.com> | ||
Copyright:: Copyright (c) 2008 Yuichi Tateno | ||
License:: MIT License |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
require 'rubygems' | ||
require 'rake' | ||
require 'rake/clean' | ||
require 'rake/testtask' | ||
require 'rake/packagetask' | ||
require 'rake/gempackagetask' | ||
require 'rake/rdoctask' | ||
require 'rake/contrib/rubyforgepublisher' | ||
require 'rake/contrib/sshpublisher' | ||
require 'fileutils' | ||
include FileUtils | ||
|
||
NAME = "as3gettext" | ||
AUTHOR = "Yuichi Tateno" | ||
EMAIL = "hotchpotch@remove@gmail.com" | ||
DESCRIPTION = "gettext for actionscript" | ||
RUBYFORGE_PROJECT = "hotchpotch" | ||
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org/rascut/" | ||
BIN_FILES = %w(as3gettext) | ||
VERS = "0.0.1" | ||
|
||
REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil | ||
CLEAN.include ['**/.*.sw?', '*.gem', '.config'] | ||
RDOC_OPTS = [ | ||
'--title', "#{NAME} documentation", | ||
"--charset", "utf-8", | ||
"--opname", "index.html", | ||
"--line-numbers", | ||
"--main", "README", | ||
"--inline-source", | ||
] | ||
|
||
task :default => [:test] | ||
task :package => [:clean] | ||
|
||
Rake::TestTask.new("test") do |t| | ||
t.libs << "test" | ||
t.pattern = "test/**/*_test.rb" | ||
t.verbose = true | ||
end | ||
|
||
spec = Gem::Specification.new do |s| | ||
s.name = NAME | ||
s.version = VERS | ||
s.platform = Gem::Platform::RUBY | ||
s.has_rdoc = true | ||
s.extra_rdoc_files = ["README", "ChangeLog"] | ||
s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)/'] | ||
s.summary = DESCRIPTION | ||
s.description = DESCRIPTION | ||
s.author = AUTHOR | ||
s.email = EMAIL | ||
s.homepage = HOMEPATH | ||
s.executables = BIN_FILES | ||
s.rubyforge_project = RUBYFORGE_PROJECT | ||
s.bindir = "bin" | ||
s.require_path = "lib" | ||
s.autorequire = "" | ||
s.test_files = Dir["test/test_*.rb"] | ||
|
||
s.add_dependency('gettext') | ||
s.add_dependency('builder') | ||
#s.add_dependency('activesupport', '>=1.3.1') | ||
#s.required_ruby_version = '>= 1.8.2' | ||
|
||
s.files = %w(README ChangeLog Rakefile) + | ||
Dir.glob("{bin,doc,test,lib,templates,generator,extras,website,script}/**/*") + | ||
Dir.glob("ext/**/*.{h,c,rb}") + | ||
Dir.glob("as3lib/**/*.as") + | ||
Dir.glob("examples/**/*.rb") + | ||
Dir.glob("tools/*.rb") | ||
|
||
s.extensions = FileList["ext/**/extconf.rb"].to_a | ||
end | ||
|
||
Rake::GemPackageTask.new(spec) do |p| | ||
p.need_tar = true | ||
p.gem_spec = spec | ||
end | ||
|
||
task :install do | ||
name = "#{NAME}-#{VERS}.gem" | ||
sh %{rake package} | ||
sh %{sudo gem install pkg/#{name}} | ||
end | ||
|
||
task :uninstall => [:clean] do | ||
sh %{sudo gem uninstall #{NAME}} | ||
end | ||
|
||
|
||
Rake::RDocTask.new do |rdoc| | ||
rdoc.rdoc_dir = 'html' | ||
rdoc.options += RDOC_OPTS | ||
rdoc.template = "resh" | ||
#rdoc.template = "#{ENV['template']}.rb" if ENV['template'] | ||
if ENV['DOC_FILES'] | ||
rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/)) | ||
else | ||
rdoc.rdoc_files.include('README', 'ChangeLog') | ||
rdoc.rdoc_files.include('lib/**/*.rb') | ||
rdoc.rdoc_files.include('ext/**/*.c') | ||
end | ||
end | ||
|
||
desc "Publish to RubyForge" | ||
task :rubyforge => [:rdoc, :package] do | ||
require 'rubyforge' | ||
Rake::RubyForgePublisher.new(RUBYFORGE_PROJECT, 'gorou').upload | ||
end | ||
|
||
desc 'Package and upload the release to rubyforge.' | ||
task :release => [:clean, :package] do |t| | ||
v = ENV["VERSION"] or abort "Must supply VERSION=x.y.z" | ||
abort "Versions don't match #{v} vs #{VERS}" unless v == VERS | ||
pkg = "pkg/#{NAME}-#{VERS}" | ||
|
||
rf = RubyForge.new | ||
puts "Logging in" | ||
rf.login | ||
|
||
c = rf.userconfig | ||
# c["release_notes"] = description if description | ||
# c["release_changes"] = changes if changes | ||
c["preformatted"] = true | ||
|
||
files = [ | ||
"#{pkg}.tgz", | ||
"#{pkg}.gem" | ||
].compact | ||
|
||
puts "Releasing #{NAME} v. #{VERS}" | ||
rf.add_release RUBYFORGE_PROJECT, NAME, VERS, *files | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package { | ||
import com.rails2u.gettext.GetText; | ||
|
||
public function _(str:String, ... args):* { | ||
return GetText._(str, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.rails2u.gettext { | ||
import flash.system.Capabilities; | ||
|
||
public class GetText { | ||
public static var locale:String = Capabilities.language; | ||
public static var defaultLocale:String = 'en'; | ||
|
||
public static function _(str:String, args:Array = null):String { | ||
var res:String; | ||
if (cacheLangStrings[locale] && cacheLangStrings[locale][str]) { | ||
res = cacheLangStrings[locale][str]; | ||
} else if (cacheLangStrings[defaultLocale] && cacheLangStrings[defaultLocale][str]) { | ||
res = cacheLangStrings[defaultLocale][str]; | ||
} else { | ||
res = str; | ||
} | ||
if (args) res = sprintf(res, args); | ||
return res; | ||
} | ||
|
||
public static function sprintf(str:String, args:Array = null):String { | ||
args = args.slice(); | ||
return str.replace(/%s/g, function():String { | ||
return args.shift(); | ||
}); | ||
} | ||
|
||
private static var cacheLangStrings:Object = {}; | ||
public static function initLangFile(xml:XML):void { | ||
var origIgnoreWhiteSpace:Boolean = XML.ignoreWhitespace; | ||
XML.ignoreWhitespace = false; | ||
try { | ||
for each (var lang:XML in xml.lang) { | ||
var langname:String = lang.@lang.toString(); | ||
var res:Object = {}; | ||
for each (var message:XML in lang.message) { | ||
res[message.msgid.toString()] = message.msgstr.toString(); | ||
} | ||
cacheLangStrings[langname] = res; | ||
} | ||
} finally { | ||
XML.ignoreWhitespace = origIgnoreWhiteSpace; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.hatena.world.utils { | ||
public class GetTextText { | ||
public var text:String; | ||
public function GetTextText(str:String) { | ||
text = str; | ||
} | ||
|
||
public function toString():String { | ||
return text; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env ruby | ||
|
||
begin | ||
require 'as3gettext/command' | ||
rescue LoadError | ||
require 'rubygems' | ||
require 'as3gettext/command' | ||
end | ||
|
||
As3gettext::Command.new.run ARGV |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
module As3gettext | ||
VERSION = '0.0.1' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'gettext' | ||
require 'gettext/rgettext' | ||
|
||
module As3gettext | ||
module As3Parser | ||
module_function | ||
def parse(file, targets = []) # :nodoc: | ||
lines = IO.readlines(file) | ||
parse_lines(file, lines, targets) | ||
end | ||
|
||
def parse_lines(file_name, lines, targets) # :nodoc: | ||
begin | ||
lines.each_with_index do |line, i| | ||
if m = (line.match(/_\('([^']+)'\)/) || line.match(/_\("([^"]+)"\)/) ) | ||
name = m[1].gsub(/\n/, '\n') | ||
if t = targets.detect{|a| a[0] == name} | ||
t[1] << ', ' + file_name + ":" + (i+1).to_s | ||
else | ||
targets << [name, file_name + ":" + (i+1).to_s] | ||
end | ||
end | ||
end | ||
rescue | ||
$stderr.print "\n\nError: #{$!.inspect} " | ||
$stderr.print " in #{file_name}:#{tk.line_no}\n\t #{lines[tk.line_no - 1]}" if tk | ||
$stderr.print "\n" | ||
exit | ||
end | ||
targets | ||
end | ||
|
||
def target?(file) | ||
['.as', '.mxml'].include? File.extname(file) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require 'optparse' | ||
require 'gettext' | ||
require 'gettext/rgettext' | ||
require 'as3gettext/as3_parser' | ||
require 'as3gettext/generate_xml' | ||
|
||
module As3gettext | ||
class Command | ||
include GetText | ||
def initialize | ||
@config = { | ||
:mode => :gettext | ||
} | ||
end | ||
attr_accessor :config | ||
|
||
def run(argv) | ||
parse argv | ||
send @config[:mode] | ||
end | ||
|
||
def gettext | ||
RGetText.add_parser As3gettext::As3Parser | ||
GetText.rgettext config[:targets], config[:output] || STDOUT | ||
end | ||
|
||
def xml | ||
xml_string = GenerateXml.new.generate config[:targets] | ||
if config[:output] | ||
File.open(config[:output], 'w') do |f| | ||
f.puts xml_string | ||
end | ||
else | ||
puts xml_string | ||
end | ||
end | ||
|
||
def parse(argv) | ||
op = OptionParser.new | ||
op.banner = <<-EOF.gsub(/^\s+/, '') | ||
Usage: | ||
$ as3gettext src/HelloWrold.as src/**/*.mxml -o template.pot | ||
$ as3gettext i18n/**.po -x -o langs.xml | ||
EOF | ||
op.on('-h', '--help', 'show this message') { puts op; Kernel::exit 1 } | ||
op.on('-x', 'export XML') { config[:mode] = :xml } | ||
op.on('-o=VAL', 'output file') {|v| config[:output] = v } | ||
op.parse! argv | ||
if argv.length == 0 | ||
puts op | ||
exit 1 | ||
end | ||
config[:targets] = argv | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'gettext' | ||
require 'gettext/poparser' | ||
require 'gettext/mo' | ||
require 'builder' | ||
require 'pathname' | ||
|
||
$KCODE = 'u' | ||
$DEBUG = false | ||
|
||
module As3gettext | ||
class GenerateXml | ||
include GetText | ||
include Builder | ||
|
||
def initialize | ||
@parser = PoParser.new | ||
@xml_declaration = false | ||
end | ||
attr_accessor :xml_declaration | ||
|
||
def generate(files) | ||
files.map! {|f| Pathname.new(f) } | ||
res = '' | ||
xml = XmlMarkup.new(:indent => 2, :target => res) | ||
xml.instruct! :xml, :version=>"1.0", :encoding=> "UTF-8" if xml_declaration | ||
xml.langs do | ||
files.each do |file| | ||
langname = file.basename('.po').to_s | ||
unless langname.empty? | ||
data = MOFile.new | ||
@parser.parse(file.read, data) | ||
xml.lang(:lang => langname) { | ||
data.each do |msgid, msgstr| | ||
next if msgid.length == 0 | ||
xml.message { | ||
xml.msgid(msgid) | ||
xml.msgstr(msgstr) | ||
} | ||
end | ||
} | ||
end | ||
end | ||
end | ||
|
||
res.gsub(/&#([\d+]+);/){[$1.to_i].pack("U")} | ||
end | ||
end | ||
end |
Oops, something went wrong.