How do I optimize the repetition of language strings

Just starting out? Need help? Post your questions and find answers here.
hdt888
User
User
Posts: 57
Joined: Sun Jul 07, 2024 8:42 am

How do I optimize the repetition of language strings

Post by hdt888 »

Here, I've only added 3 languages ​​and 2 message lines, and I'm already seeing PureBasic commands starting to get longer.
If I continue to add 7 more languages, I have to retype these repetitive commands, I will probably die.
And the characters are not displayed correctly ( like Chinese language appears in the message box. I'm still using UTF-8 encoding text ).

I have heard about using MAP, or LIST, but I have absolutely no experience for this.

How do I optimize the repetition of language strings?.

Thank you for support.

Code: Select all

#NN_EN = "English"
#NN_CN = "Chinese"
#NN_VN = "Tiếng Việt"

Structure struc_LANG
  lang_path.s
  hello.s
  goodbye.s
EndStructure
Global *Lng.struc_LANG = AllocateStructure(struc_LANG)

*Lng\lang_path = GetCurrentDirectory() + "langs.txt"

Procedure create_lang_file()
  If CreatePreferences(*Lng\lang_path)
    PreferenceGroup(#NN_VN)
    WritePreferenceString("hello", "Chào bạn") 
    WritePreferenceString("goodbye", "Tạm biệt bạn nhé")
    PreferenceGroup(#NN_EN)
    WritePreferenceString("hello", "Hello sir")
    WritePreferenceString("goodbye", "Bye bye sir")
    PreferenceGroup(#NN_CN)
    WritePreferenceString("hello", "嗨,伙计")
    WritePreferenceString("goodbye", "再见")
    ClosePreferences()
  EndIf
EndProcedure

create_lang_file()

Procedure load_VN_lang()
  OpenPreferences(*Lng\lang_path)
  PreferenceGroup(#NN_VN)
  *Lng\hello  = ReadPreferenceString("hello", "0")
  *Lng\goodbye    = ReadPreferenceString("goodbye", "0")
  ClosePreferences()
  FreeStructure(*Lng)
EndProcedure

Procedure load_EN_lang()
  OpenPreferences(*Lng\lang_path)
  PreferenceGroup(#NN_EN)
  *Lng\hello  = ReadPreferenceString("hello", "0")
  *Lng\goodbye    = ReadPreferenceString("goodbye", "0")
  ClosePreferences()
  FreeStructure(*Lng)
EndProcedure

Procedure load_CN_lang()
  OpenPreferences(*Lng\lang_path)
  PreferenceGroup(#NN_CN)
  *Lng\hello  = ReadPreferenceString("hello", "0")
  *Lng\goodbye    = ReadPreferenceString("goodbye", "0")
  ClosePreferences()
  FreeStructure(*Lng)
EndProcedure

load_CN_lang()
Debug *Lng\hello
Debug *Lng\goodbye
MessageRequester(#NN_CN, *Lng\goodbye)
PB 5.x + 6.x + Win10. Feel the ...Pure... Power.
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: How do I optimize the repetition of language strings

Post by jacdelad »

Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
hdt888
User
User
Posts: 57
Joined: Sun Jul 07, 2024 8:42 am

Re: How do I optimize the repetition of language strings

Post by hdt888 »

Thank you very much jacdelad.

I decided to keep calling language strings in the CreatePreferences method.
PB 5.x + 6.x + Win10. Feel the ...Pure... Power.
Post Reply