-
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.
* Added `TextAssets` template class for easily loading text assets for multiple languages without having to manually define a class for them. * Added `isRightToLeft` function to detect whether a certain language is written from right to left.
- Loading branch information
Showing
7 changed files
with
358 additions
and
67 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 |
---|---|---|
@@ -1,63 +1,14 @@ | ||
import "Srl/String"; | ||
import "Srl/Array"; | ||
import "Srl/refs"; | ||
import "Spp"; | ||
import "Core/Basic"; | ||
import "Core/Data"; | ||
|
||
module I18n { | ||
use Srl; | ||
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; | ||
} | ||
} | ||
|
||
import "I18n/strings"; | ||
import "I18n/TextAssets"; | ||
import "I18n/isRightToLeft"; |
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,82 @@ | ||
@merge module I18n { | ||
class TextAssets [path: string, defaultLangCode: string] { | ||
@shared def data: Map[String, SrdRef[this_type]]; | ||
@shared def current: SrdRef[this_type]; | ||
@shared def currentLanguage: String; | ||
|
||
handler this~init() {} | ||
|
||
def AstTemplateMap: alias Map[String, ref[Core.Basic.TiObject]]; | ||
|
||
// Define member variables. | ||
preprocess { | ||
def filenames: ptr[Fs.FileNames] = Fs.readDir(String(path) + "/" + defaultLangCode); | ||
def i: Int; | ||
for i = 0, i < filenames~cnt.count, ++i { | ||
if filenames~cnt.names(i)(0) == '.' continue; // Skip . and .. and hidden files. | ||
def name: String = String(filenames~cnt.names(i)~ptr).split(".")(0); | ||
Spp.astMgr.insertAst( | ||
(ast def name: String), | ||
AstTemplateMap().set(String("name"), Core.Basic.TiStr(name)) | ||
); | ||
} | ||
} | ||
|
||
func initialize() { | ||
def d: SrdRef[this_type]; | ||
|
||
// Populate the map for all available locales. | ||
preprocess { | ||
def dirs: ptr[Fs.FileNames] = Fs.readDir(path); | ||
def i: Int; | ||
for i = 0, i < dirs~cnt.count, ++i { | ||
if dirs~cnt.names(i)(0) == '.' continue; // Skip . and .. and hidden files. | ||
Spp.astMgr.insertAst(ast d = SrdRef[this_type].construct()); | ||
|
||
def filenames: ptr[Fs.FileNames] = Fs.readDir(String(path) + "/" + dirs~cnt.names(i)~ptr); | ||
def j: Int; | ||
for j = 0, j < filenames~cnt.count, ++j { | ||
if filenames~cnt.names(j)(0) == '.' continue; // Skip . and .. and hidden files. | ||
def content: String = Fs.readFile( | ||
String(path) + "/" + dirs~cnt.names(i)~ptr + "/" + filenames~cnt.names(j)~ptr | ||
); | ||
def varName: String = String(filenames~cnt.names(j)~ptr).split(".")(0); | ||
Spp.astMgr.insertAst( | ||
(ast d.varName = varData), | ||
AstTemplateMap() | ||
.set(String("varName"), Core.Data.Ast.Identifier(varName)) | ||
.set(String("varData"), Core.Data.Ast.StringLiteral(content)) | ||
); | ||
} | ||
|
||
Spp.astMgr.insertAst( | ||
(ast data.set(String(lang), d)), | ||
AstTemplateMap().set(String("lang"), Core.Basic.TiStr(String(dirs~cnt.names(i)~ptr))) | ||
); | ||
} | ||
} | ||
|
||
setCurrentLanguage(String(defaultLangCode)); | ||
} | ||
|
||
func getAvailableLanguages(): Array[String] { | ||
def langs: Array[String] = data.keys; | ||
// Bring the default language to the front. | ||
def i: Int; | ||
for i = 0, i < langs.getLength(), ++i { | ||
if langs(i) == defaultLangCode { | ||
langs.remove(i); | ||
langs.insert(0, String(defaultLangCode)); | ||
break; | ||
} | ||
} | ||
return langs; | ||
} | ||
|
||
func setCurrentLanguage(langCode: String) { | ||
if data(langCode).isNull() langCode = defaultLangCode; | ||
current = data(langCode); | ||
currentLanguage = langCode; | ||
} | ||
} | ||
} |
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,22 @@ | ||
@merge module I18n { | ||
func isRightToLeft(languageCode: String): Bool { | ||
@shared def rtlLanguages: Array[String]({ | ||
String("ar"), | ||
String("arc"), | ||
String("dv"), | ||
String("fa"), | ||
String("ha"), | ||
String("he"), | ||
String("kh"), | ||
String("ks"), | ||
String("ku"), | ||
String("ps"), | ||
String("ur"), | ||
String("yi") | ||
}); | ||
languageCode = languageCode.split("_")(0); | ||
def i: Int; | ||
for i = 0, i < rtlLanguages.getLength(), ++i if rtlLanguages(i) == languageCode return true; | ||
return false; | ||
} | ||
} |
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 @@ | ||
@merge module I18n { | ||
def strings: Map[String, String]; | ||
|
||
func initializeStrings(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; | ||
} | ||
} |
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
Oops, something went wrong.