Page 1 of 1

How do I optimize the repetition of language strings

Posted: Wed Jul 10, 2024 4:38 am
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)

Re: How do I optimize the repetition of language strings

Posted: Wed Jul 10, 2024 5:31 am
by jacdelad

Re: How do I optimize the repetition of language strings

Posted: Fri Jul 19, 2024 12:36 am
by hdt888
Thank you very much jacdelad.

I decided to keep calling language strings in the CreatePreferences method.

Re: How do I optimize the repetition of language strings

Posted: Fri Jul 19, 2024 3:04 am
by AZJIO