Skip to content

Library for text localisation, which is built into the application at the development stage

License

Notifications You must be signed in to change notification settings

mnoltmeer/SimpleLocaliseLibrary

Repository files navigation

image

31_Banner_Proudly_Powered_By_CBuilder_450x150-1475104

Code For Good Contest

https://www.embarcadero.com/products/cbuilder

Supporting multiple languages in your application is a good idea. But what if during the development phase you do not yet know the complete list of languages that your application will support? Or perhaps the localisation of your application will be created by enthusiasts? The basis for the modification of the text portion of the user interface is better to lay on the stage of development. And this library will help with this.

How it works:

  1. Create XML-file with structure like:
<LocalisationFile>
	<Record id = 'id_1'>first record</Record>
	<Record id = 'id_2'>second record</Record>
</LocalisationFile>
  1. Mark all text elements which need to localise as $[id], where [id] is string (with no spaces).

  2. Link Localisation.dll to app and use these functions:

int __stdcall LoadLocaleFile(const wchar_t *xml_file) - to load localisation xml-file.

wchar_t* __stdcall Localise(const wchar_t *mark) - to get localised text.

Localise() function geting mark (like $1) and searching for it in record list. If it found, function returns record that assigned to mark. If an error occurs, the function will return NULL. The detailed error text can be obtained by calling the function const wchar_t* __stdcall GetError().

To further simplify the work with the library, the project contains a module with a static class TLocalisation. This class contains all the necessary methods for working with the localisation library. Just include this file in your project and add the localisation library to its directory. After that, you can use the following class methods in your code:

static HINSTANCE LoadLib(const String &dll_path) - loads library.
static int FreeLib() - frees library.
static int OpenLocaleFile(const String &path) - opens a localisation file and creates a set of localised text records.
static wchar_t *LocaliseText(const String &mark) - returns localised text.
static const wchar_t *GetLastError() - returns last error.

Let's see how it works. Add some lines of code.

void __fastcall TDemoForm::LocaleFileListClick(TObject *Sender)
{
  if (LocaleFileList->ItemIndex == 0)
	{
	  //restoring default values (just for example)
	  LbName->Caption = "$name";
	  LbSay->Caption = "$wiz_say";
	  LbChoice->Caption = "$choice";
	  BtAnswer1->Caption = "$answer1";
	  BtAnswer2->Caption = "$answer2";
	  BtAnswer3->Caption = "$answer3";
	}
  else
	{
	  TLocalisation::LoadLib("Localisation.dll"); //loading library

	  String file = LocaleFileList->Items->Strings[LocaleFileList->ItemIndex] + ".xml";

	  if (!TLocalisation::OpenLocaleFile(file)) //reading file
		ShowMessage("Can't load file " + file);
	  else
		{
		  //localising UI controls
		  LbName->Caption = TLocalisation::LocaliseText("$name");
		  LbSay->Caption = TLocalisation::LocaliseText("$wiz_say");
		  LbChoice->Caption = TLocalisation::LocaliseText("$choice");
		  BtAnswer1->Caption = TLocalisation::LocaliseText("$answer1");
		  BtAnswer2->Caption = TLocalisation::LocaliseText("$answer2");
		  BtAnswer3->Caption = TLocalisation::LocaliseText("$answer3");
		}

	  TLocalisation::FreeLib();
	}
}
//---------------------------------------------------------------------------

Place the library and localisation files in the application directory.

image

And this is how our application changes in real time:

By default:

image

And after choosing one of the localisation files:

image image image

About

Library for text localisation, which is built into the application at the development stage

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published