Skip to content

Commit

Permalink
Initial implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarmadka committed Apr 18, 2023
1 parent f3bd4da commit 9eacc1a
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 2 deletions.
62 changes: 62 additions & 0 deletions I18n.alusus
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;
}
}
2 changes: 0 additions & 2 deletions README.md

This file was deleted.

File renamed without changes.
65 changes: 65 additions & 0 deletions readme.ar.md
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"));
```

33 changes: 33 additions & 0 deletions readme.md
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"));
```

8 changes: 8 additions & 0 deletions تـرجمة.أسس
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
اشمل "I18n"؛

عرف تـرجمة: لقب I18n؛
@دمج وحدة تـرجمة {
عرف هيئ: لقب initialize؛
عرف أعرب_القاموس: لقب parsePo؛
عرف نص: لقب string؛
}

0 comments on commit 9eacc1a

Please sign in to comment.