-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
168 additions
and
2 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,62 @@ | ||
module I18n { | ||
def strings: Map[String, String]; | ||
|
||
func initialize(buf: CharsPtr) { | ||
strings.clear(); | ||
strings = parsePo(buf); | ||
} | ||
|
||
func string(val: CharsPtr): String { | ||
def input: String(val); | ||
def res: String = strings(input); | ||
if res == "" return input else return res; | ||
} | ||
|
||
func parsePo(buf: CharsPtr): Map[String, String] { | ||
def result: Map[String, String]; | ||
def lastId: String; | ||
def isMsgid: Bool = false; | ||
def isMsgstr: Bool = false; | ||
while buf~cnt(0) != 0 { | ||
if String.compare(buf, "msgid", 5) == 0 { | ||
buf += 5; | ||
lastId = parseString(buf); | ||
} else if String.compare(buf, "msgstr", 6) == 0 { | ||
buf += 6; | ||
result(lastId) = parseString(buf); | ||
} else if buf~cnt(0) == '#' { | ||
while buf~cnt(0) != '\n' buf += 1; | ||
} else { | ||
buf += 1; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
func parseString(buf: ref[CharsPtr]): String { | ||
def result: String; | ||
def len: ArchInt = 0; | ||
while true { | ||
while buf~cnt(0) == ' ' or buf~cnt(0) == '\n' buf += 1; | ||
if buf~cnt(0) == '"' { | ||
buf += 1; | ||
def end: CharsPtr = String.find(buf, '\n')~cast[CharsPtr]; | ||
while end~cnt(0) != '"' end -= 1; | ||
result.realloc(len + end~cast[ArchInt] - buf~cast[ArchInt]); | ||
while buf~cast[ArchInt] < end~cast[ArchInt] { | ||
if buf~cnt(0) == '\\' { | ||
buf += 1; | ||
if buf~cnt(0) == 'n' result.buf~cnt(len++) = '\n' | ||
else result.buf~cnt(len++) = buf~cnt(0); | ||
} else result.buf~cnt(len++) = buf~cnt(0); | ||
buf += 1; | ||
} | ||
result.buf~cnt(len) = 0; | ||
buf += 1; | ||
} else { | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
} |
File renamed without changes.
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,65 @@ | ||
# I18n | ||
[[English]](readme.md) | ||
|
||
مكتبة لتمكين ترجمة النصوص المستخدمة في واجهات المستخدم. هذه المكتبة تحمل الترجمات من ملفات PO. | ||
|
||
## الاستخدام | ||
|
||
* أضف المكتبة إلى المشروع باستخدام مدير حزم الأسس: | ||
|
||
<div dir=rtl> | ||
|
||
``` | ||
اشمل "مـحا"؛ | ||
مـحا.اشمل_ملف("Alusus/I18n"، "تـرجمة.أسس")؛ | ||
``` | ||
|
||
</div> | ||
|
||
``` | ||
import "Apm"; | ||
Apm.importFile("Alusus/I18n"); | ||
``` | ||
|
||
* هيئ المكتبة من ملف PO: | ||
|
||
<div dir=rtl> | ||
|
||
``` | ||
تـرجمة.هيئ(محتوى_po)؛ | ||
``` | ||
|
||
</div> | ||
|
||
``` | ||
I18n.initialize(poContent); | ||
``` | ||
|
||
أو | ||
|
||
<div dir=rtl> | ||
|
||
``` | ||
تـرجمة.هيئ(مـتم.نـم.اقرأ_ملف("en.po"))؛ | ||
``` | ||
|
||
</div> | ||
|
||
``` | ||
I18n.initialize(Srl.Fs.readFile("lang.po")); | ||
``` | ||
|
||
* استبدل سلاسل المحارف المضمنة بالدالة `تـرجمة.نص`: | ||
|
||
<div dir=rtl> | ||
|
||
``` | ||
طـرفية.اطبع(تـرجمة.نص("نصي المترجم"))؛ | ||
``` | ||
|
||
</div> | ||
|
||
``` | ||
Console.print(I18n.string("My localized string")); | ||
``` | ||
|
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,33 @@ | ||
# I18n | ||
[[عربي]](readme.ar.md) | ||
|
||
Internationalization functionality for Alusus Language. This library can load translations from PO | ||
files. | ||
|
||
## Usage | ||
|
||
* Add the library to the project using Alusus Package Manager: | ||
|
||
``` | ||
import "Apm"; | ||
Apm.importFile("Alusus/I18n"); | ||
``` | ||
|
||
* Initialize the library from PO file: | ||
|
||
``` | ||
I18n.initialize(poContent); | ||
``` | ||
|
||
or | ||
|
||
``` | ||
I18n.initialize(Srl.Fs.readFile("lang.po")); | ||
``` | ||
|
||
* Replace the hardcoded strings with `I18n.string`: | ||
|
||
``` | ||
Console.print(I18n.string("My localized string")); | ||
``` | ||
|
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,8 @@ | ||
اشمل "I18n"؛ | ||
|
||
عرف تـرجمة: لقب I18n؛ | ||
@دمج وحدة تـرجمة { | ||
عرف هيئ: لقب initialize؛ | ||
عرف أعرب_القاموس: لقب parsePo؛ | ||
عرف نص: لقب string؛ | ||
} |