International language in Linux
Posted: Mon Feb 01, 2021 8:36 am
How to enable international language in Linux using .mo files?
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
If ExamineEnvironmentVariables()
While NextEnvironmentVariable()
Debug EnvironmentVariableName() + " = " + EnvironmentVariableValue()
Wend
EndIf
Code: Select all
ImportC "???"
...
EndImport
It seems the functions are already available like gettext_() for example, but I don't know how to use them.AZJIO wrote: #NULLCode: Select all
ImportC "???" ... EndImport
Code: Select all
EnableExplicit
; Determination of the interface language and apply
Global UserIntLang, UserIntLang$, PathLang$, tmp$, i, *Lang
; Defines the OS language
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
If OpenLibrary(0, "kernel32.dll")
*Lang = GetFunction(0, "GetUserDefaultUILanguage")
If *Lang
UserIntLang = CallFunctionFast(*Lang)
EndIf
CloseLibrary(0)
EndIf
CompilerCase #PB_OS_Linux
If ExamineEnvironmentVariables()
While NextEnvironmentVariable()
If Left(EnvironmentVariableName(), 4) = "LANG"
; LANG=ru_RU.UTF-8
; LANGUAGE=ru
UserIntLang$ = Left(EnvironmentVariableValue(), 2)
Break
EndIf
Wend
EndIf
CompilerEndSelect
#CountStrLang = 3 ; number of translation strings and correspondingly array
Global Dim Lng.s(#CountStrLang)
; Interface strings even if the language file is not found
Lng(1) = "Signal"
Lng(2) = "Hours"
Lng(3) = "Minutes"
; ...
; defines the paths to the language file
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
PathLang$ = GetPathPart(ProgramFilename()) + GetFilePart(ProgramFilename(), #PB_FileSystem_NoExtension) + "_Lang.txt"
CompilerCase #PB_OS_Linux
PathLang$ ="/usr/share/locale/" + UserIntLang$ + "/LC_MESSAGES/" + GetFilePart(ProgramFilename(), #PB_FileSystem_NoExtension) + ".txt"
CompilerEndSelect
; If the language file exists, then it uses it
If FileSize(PathLang$) > 1
If ReadFile(0, PathLang$) ; If the file descriptor was opened, then ...
i=0
While Eof(0) = 0 ; Loop until end of file is reached.
tmp$ = ReadString(0) ; reading the line
; If Left(tmp$, 1) = ";"
; Continue
; EndIf
tmp$ = ReplaceString(tmp$ , #CR$ , "") ; correction if in windows
If tmp$ And Left(tmp$, 1) <> ";"
i+1
If i > #CountStrLang ; the Lng() array is already set, but if there are more rows than you need, then we do not allow unnecessary ones
Break
EndIf
Lng(i) = tmp$
Else
Continue
EndIf
Wend
CloseFile(0)
EndIf
; Else
; SaveFile_Buff(PathLang$, ?LangFile, ?LangFileend - ?LangFile)
EndIf
; End => Language Definition