Mono
出自DebianWiki
目錄 |
[編輯] 開放原始碼 .NET 平台
Mono 計畫包含 Runtime、 Classes、 Gtk# 、ASP.NET、ADO.NET、C# Compiler 與 VB Compiler。DebianPackages:mono 已經正式進入官方軟體倉儲中。 相關的軟體有
- DebianPackages:asp.net-examples - demo pages for ASP.NET infrastructure
- DebianPackages:libmono0 - libraries for the Mono interpreter
- DebianPackages:libmono-dev - libraries for the Mono interpreter - Development files
- DebianPackages:mono-assemblies-arch - architecture specific files for Mono/.NET assemblies
- DebianPackages:mono-assemblies-base - the Mono .NET class libraries
- DebianPackages:mono-common - common files for the Mono CLI runtimes
- DebianPackages:monodoc-base - shared MonoDoc binaries
- DebianPackages:monodoc-browser - MonoDoc GTK+ based viewer
- DebianPackages:monodoc-http - MonoDoc http based viewer
- DebianPackages:monodoc-manual - compiled XML documentation from the MONO project
- DebianPackages:monodoc - The MONO documentation viewer
- DebianPackages:mono-jay - LALR(1) parser generator oriented to Java/.NET
- DebianPackages:mono-jit - fast CLI (.NET/Mono) JIT compiler
- DebianPackages:mono-mcs - the Mono C# compiler
- DebianPackages:mono-mint - generic CLI (.NET/Mono) code interpreter
- DebianPackages:mono - The Mono .NET development environment
- DebianPackages:mono-utils - Mono utilities
- DebianPackages:mono-xsp - simple web server to run ASP.NET applications
此外,部份尚未進到官方檔案庫的軟體可以在 [Debian Mono Packages] 取得。
[編輯] C#
以 C# 為例,編譯與執行
- 註: .exe 檔案可被直接執行是由於支援 binfmt-support。
user@debian:/tmp$ cat > hello.cs
using System;
class Hello {
static void Main() {
Console.WriteLine ("Hello, World!");
}
}
user@debian:/tmp$ mcs /target:exe hello.cs
Compilation succeeded
user@debian:/tmp$ file hello.exe
hello.exe: MS Windows PE 32-bit Intel 80386 console executable
user@debian:/tmp$ ./hello.exe
Hello, World!
[編輯] monodis
.NET 編譯器會將程式碼編譯為 CIL (Common Intermediate Language),可用 monodis 將 CIL 反組譯
user@debian:/tmp$ monodis hello.exe
.assembly extern mscorlib
{
.ver 1:0:3300:0
}
.assembly 'hello'
{
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module 'hello.exe' // GUID = {C89E73D0-F21B-4776-A15B-E88384484240}
.class private auto ansi beforefieldinit 'Hello'
extends [mscorlib]System.Object
{
// method line 1
.method public hidebysig specialname rtspecialname
instance default void .ctor () cil managed
{
// Method begins at RVA 0x20ec
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void valuetype [mscorlib]'System.Object'::.ctor()
IL_0006: ret
} // end of method Hello::instance default void .ctor ()
// method line 2
.method private static
default void 'Main' () cil managed
{
// Method begins at RVA 0x20f4
.entrypoint
// Code size 11 (0xb)
.maxstack 8
IL_0000: ldstr "Hello, World!"
IL_0005: call void class [mscorlib]'System.Console'::'WriteLine'(string)
IL_000a: ret
} // end of method Hello::default void 'Main' ()
} // end of type Hello
[編輯] ilasm
再以 ilasm 將檔案組譯回去
user@debian:/tmp$ monodis --output=hello.il hello.exe user@debian:/tmp$ ilasm hello.il Assembling 'hello.il' , no listing file, to exe --> 'hello.exe' Compilation succeeded user@debian:/tmp$ ./hello.exe Hello, world!
[編輯] ASP.NET
如果要使用 ASP.NET,必須安裝 DebianPackages:mono-xsp,目前有版本衝突問題,因此可能無法正常安裝 簡單的 HelloWorld.aspx
<%@ Page Language="C#" %>
<html>
<body>
<h2><%Response.Write("Hello World!")%></h2>
<p><%Response.Write(now(]]%></p>
</body>
</html>
[編輯] VB
user@debian:~$ cat HelloVB.vb
' Allow easy reference to the System namespace classes.
Imports System
' This module houses the application's entry point.
Public Module modmain
' Main is the application's entry point.
Sub Main()
' Write text to the console.
Console.WriteLine ("Hello World using Visual Basic!")
End Sub
End Module
$ mbas HelloVB.vb
Compilation succeeded
$ ./HelloVB.exe
Hello World using Visual Basic!
[編輯] 技術文件
請安裝 DebianPackages:monodoc-manual 以及 DebianPackages:monodoc-browser,monodoc-browser 是一個整合妥當的文件瀏覽器。除了瀏覽文件外,你可以於文件中撰寫註解,並利用內建的上傳功能貢獻內容。
[線上版本]。
[編輯] I18N
[編輯] CultureInfo
[編輯] GNU Gettext
[GNU Gettext] 已經於 0.14 新版支援 C Sharp。開發者可以藉由 xgettext 搜尋 C# 程式中需要翻譯的字串,並藉由 msgfmt 將翻譯結果轉為適用的資源動態連結檔(.NET Resource DLL)或資源檔案(.NET .resources file)。
目前 Debian 上之 DebianPackages:gettext 套件尚未提供 C# 執行環境(GNU.Gettext.dll),因此開發者必須自行從 gettext 中編譯 GNU.Gettext.dll 或從網路下載。此外,Mono 多國語系支援([System.Globalization.CultureInfo]) 依賴於 IBM ICU 套件。目前 Debian 之 ICU 還留在 experimental 中,因此於現有版本(mono-0.31)中宣告 GultureInfo 將引起例外錯誤(System.ArgumentException)。唯一的辦法是自行編譯 Mono。
以以下例子為例,由於 Mono 不會自動抓取參考 LANG/LC_ALL 變數,因此必須自行設定現行 Culture。文件中自行宣告 GettextResourceManager,並藉由 GettextResourceManager 類別抓取翻譯字串。例子中也示範如何利用 C# String.Format 來翻譯程式用字串。
// Example for use of GNU gettext.
using GNU.Gettext;
using System;
using System.Diagnostics;
class GetTextSample {
public static void Main() {
String locale = System.Environment.GetEnvironmentVariable("LC_ALL");
if (locale == null || locale == "")
locale = System.Environment.GetEnvironmentVariable("LANG");
if (!(locale == null || locale == "")) {
if (locale.IndexOf('.') >= 0)
locale = locale.Substring(0,locale.IndexOf('.'));
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Threading.Thread.CurrentThread.CurrentUICulture =
new System.Globalization.CultureInfo(locale.Replace('_','-'));
}
GettextResourceManager catalog =
new GettextResourceManager("l10n");
Console.WriteLine(catalog.GetString ("Mono, C#!"));
Console.WriteLine(
String.Format(
catalog.GetString("This program is running as process number {0}."),
Process.GetCurrentProcess().Id));
}
}
藉由 mcs 編譯之
$ mcs -r:GNU.Gettext GetTextSample.cs Compilation succeeded
之後必須從程式碼中取出待翻譯字串,使用 xgettext,並存為 l10n.po
$ xgettext GetTextSample.cs -o l10n.po
$ cat l10n.po
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2004-05-08 01:09+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
#: GetTextSample.cs:22
msgid "Mono, C#!"
msgstr ""
#: GetTextSample.cs:25
#, csharp-format
msgid "This program is running as process number {0}."
msgstr ""
將 l10n.po 翻譯之,將檔名拷貝為 zh_TW.po,並修改編碼為 UTF-8 後翻譯
cat zh_TW.po
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2004-05-07 12:22+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: GetTextSample.cs:9
msgid "Mono, C#!"
msgstr "魔諾, 嬉嚇普!"
#: GetTextSample.cs:28
#, csharp-format
msgid "This program is running as process number {0}."
msgstr "此執行程式的程序編號為 {0}."
這裡以生成 .NET Resources DLL 為例子,將翻譯後的檔案生成 DLL,若正確無誤可於目錄下產生 zh-TW/l10n.resources.dll。
$ msgfmt --csharp -r l10n -l zh_TW zh_TW.po -d . $ ls zh-TW/ l10n.resources.dll
基本的翻譯與檔案已經完成,若圖形介面使用 GTK/Glade 開發,gettext 也支援直接從 Glade XML 檔案中取出待譯字串。
$ LANG=en_US ./GetTextSample.exe Mono, C#! This program is running as process number 31337. $ LANG=zh_TW.Big5 ./GetTextSample.exe 魔諾, 嬉嚇普! 此執行程式的程序編號為 31337.
曾經利用 gettext 開發過軟體的開發者,大概已經熟悉使用類似 _("字串") 的語法,相較之下,catalog.GetString 顯得恿餘。開發者可以使用 delegate method 來實踐這個慣例。這裡展示利用動態 Instance,也可設計為使用 Static Method,端視開發者造化。
using GNU.Gettext;
using System;
using System.Diagnostics;
class GetTextSample {
public delegate string DelegatedGettextMethod(string str);
public static void Main() {
String locale = System.Environment.GetEnvironmentVariable("LC_ALL");
if (locale == null || locale == "")
locale = System.Environment.GetEnvironmentVariable("LANG");
if (!(locale == null || locale == "")) {
if (locale.IndexOf('.') >= 0)
locale = locale.Substring(0,locale.IndexOf('.'));
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Threading.Thread.CurrentThread.CurrentUICulture =
new System.Globalization.CultureInfo(locale.Replace('_','-'));
}
GettextResourceManager catalog =
new GettextResourceManager("l10n");
DelegatedGettextMethod _ = new DelegatedGettextMethod(catalog.GetString);
Console.WriteLine(_("Mono, C#!"));
Console.WriteLine(
String.Format(
_("This program is running as process number {0}."),
Process.GetCurrentProcess().Id));
}
}
若以 _("string") 為慣例,你應該要特別注意 xgettext 要多設定一個關鍵字。
$ xgettext --keyword=_ -o l10n.p-o l10n.po *cs
[編輯] Generics
gmcs 支援 Generics 語法,然而目前 Debian 官方套件亦尚未納入 gmcs。
[編輯] 參考資訊/延伸閱讀
- 計畫首頁: http://www.go-mono.com/
- 開發手冊: http://www.gotmono.com/docs/
- http://www.mono-project.com/Documentation
[編輯] 相關文章
[編輯] 社群網站
[編輯] The C# language
- ECMA-334: C# Language Specification http://www.ecma-international.org/publications/standards/ECMA-334.HTM
- Microsoft C# Language Specification http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/CSharpSpecStart.asp
- MSDN C# Tutorials http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcoricsharptutorials.asp
[編輯] 書籍
- Mono: A Developer's Notebook http://www.oreilly.com/catalog/monoadn/
![[Main Page]](/upload/4/49/Debian_taiwan_out.png)