Da fiel mir dann folgendes aus den Fingern...
Code: Alles auswählen
;:- Module : Simple MultiLanguage (SML)
;:- Author : George Bisonte
;:- Date : September 22, 2013
;:- Compiler : PureBasic 5.20 LTS
;:- TargetOS : All
;:
;:- Use simply a MAP to hold words and their translation.
;:- Only one language at the same time possible with this
;:- I hope i do the "threadsafe" thing right...
;:
DeclareModule SimpleMultiLanguage
;: Module : Simple MultiLanguage (SML)
;: Author : George Bisonte (original idea : Hroudtwolf)
;: Date : September 22, 2013
Declare.s SML(Key$, Standard$ = "") ; Get the word from Key$ or Set the word in Key$ to Standard$ if not exist.
Declare SML_Set(Key$, Expression$) ; Set the word of Key$ to Expression$, already defined or not.
Declare SML_Save(FileName$) ; Save a languagefile... #PB_UTF8
Declare SML_Load(FileName$) ; Load a languagefile... #PB_UTF8
Declare SML_Clear() ; Erase the whole wordlist
EndDeclareModule
Module SimpleMultiLanguage
;: Module : Simple MultiLanguage (SML)
;: Author : George Bisonte (original idea : Hroudtwolf)
;: Date : September 22, 2013
EnableExplicit
Global NewMap SML_MAP.s()
Global SML_Mutex = CreateMutex()
Procedure.s SML(Key$, Standard$ = "")
Protected Result.s
If FindMapElement(SML_MAP(), Key$)
Result = SML_MAP(Key$)
Else
LockMutex(SML_Mutex)
SML_MAP(Key$) = Standard$
UnlockMutex(SML_Mutex)
Result = Standard$
EndIf
ProcedureReturn Result
EndProcedure
Procedure SML_Set(Key$, Expression$)
LockMutex(SML_Mutex)
SML_MAP(Key$) = Expression$
UnlockMutex(SML_Mutex)
ProcedureReturn #True
EndProcedure
Procedure SML_Save(FileName$)
Protected File, Result = #False
File = CreateFile(#PB_Any, FileName$)
If File
LockMutex(SML_Mutex)
ForEach SML_MAP()
WriteStringN(File, MapKey(SML_MAP()) + " = " + SML_MAP(), #PB_UTF8)
Next
UnlockMutex(SML_Mutex)
CloseFile(File)
Result = #True
EndIf
ProcedureReturn Result
EndProcedure
Procedure SML_Load(FileName$)
Protected File, String.s, Result = #False
File = ReadFile(#PB_Any, FileName$)
If File
LockMutex(SML_Mutex)
ClearMap(SML_MAP())
While Not Eof(File)
String.s = ReadString(File, #PB_UTF8)
If String <> ""
SML_MAP( Trim(StringField(String, 1, "="))) = Trim(StringField(String, 2, "="))
EndIf
Wend
UnlockMutex(SML_Mutex)
CloseFile(File)
If MapSize(SML_MAP()) > 0
Result = #True
EndIf
EndIf
ProcedureReturn Result
EndProcedure
Procedure SML_Clear()
LockMutex(SML_Mutex)
ClearMap(SML_MAP())
UnlockMutex(SML_Mutex)
EndProcedure
EndModule
;:
CompilerIf #PB_Compiler_IsMainFile
UseModule SimpleMultiLanguage
Debug "SimpleMultiLanguage - Demo"
Debug "--------------------------"
; --- Erst einmal ein Wort deklarieren
SML_Set("Hallo", "Huhu")
; --- Dann das Wort ausgeben
Debug SML("Hallo", "Hallo")
; --- Jetzt ein unbekanntes Wort
Debug SML("Hallo Neu", "Hallo Neu")
; --- Hier einfach mal (weil es ist schon definiert) den Standard weglassen
Debug SML("Hallo Neu")
Debug "Speichern ----------------"
; --- Am Ende eines Programmes (während der Entwicklung) speichert alle Wörter
SML_Save("E:\01Test.catalog") ; evt. Pfad anpassen
; --- Einmal die Wörter "clearen"
SML_Clear()
Debug "Laden --------------------"
; --- Nun laden wir das ganze um zu sehen ob das speichern geklappt hat
SML_Load("E:\01Test.catalog") ; evt. Pfad anpassen
Debug "Abschlusstest ------------"
; --- Und nun zeig mal obs so ist...
Debug SML("Hallo")
Debug SML("Hallo Neu")
CompilerEndIf