Git Product home page Git Product logo

simplelocaliselibrary's Introduction

SimpleLocaliseLibrary

31_Banner_Proudly_Powered_By_CBuilder_450x150-1475104

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 $, where 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() 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

simplelocaliselibrary's People

Contributors

mnoltmeer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.