Keep strings outside of source files

Notice: This thread is very old.
hacafrakus
Member | 14
+
+1
-

Hello,

this is just a discussion about some practices, I would like to know how you do this and why. So, here is what is this topic about!

While programming single language application, we can just write strings directly into the source files. There will be something like:

$this->flashMessage("You have been signed out.");

…and the same for templates. Okay, sometimes we need multi language application. Now, I saw two solutions:

// We will have some default language, like english.
$this->flashMessage($this->translator->translate("You have been signed out."));

// We will have something like constants (named strings)
$this->flashMessage($this->translator->translate("SIGN_OUT_MESSAGE"));

// Or we can overwrite the flashMessage() implementation and do the localization there, this doesn't matter now.
$this->flashMessage("SIGN_OUT_MESSAGE");

So, when single application is made, we have no localization and messages for users are in source files. I guess, this is wrong, since every change or typo must be done by programmer who knows the source files. Of course, this doesn't matter in our own application.

This leads to using some kind of translator, which makes changes possible. However – we should not use default language in sources. Why? Imagine there is a typo in the default language message in sources. How administrator solves this? Does he translate the string?

Better would be using a constant or name for every used message. This is quite good for saving into database or a file and the only thing I don't like about this is the way it shows in editor – would be great if IDE displayed the assigned string.

Those things are already solved in Android Development Tools. I like the way it works – you have some file with strings (in XML, JSON, …) and the generator creates a real Java class with constants, for PHP, this what it would look like:

class R {
	const SIGN_IN = 'Login';
	const SIGN_OUT = 'Logout';
}

And, the IDE displays the string instead of identifier.

How do you solve this? I didn't find some kind of externalization of strings for PHP anywhere. One would say this is useless, but I don't think so – many messages or labels are used multiple times and what if we want, for instance, rename a feature of our application?

Filip Procházka
Moderator | 4668
+
0
-

Symfony Translator, and therefore Kdyby/Translation actually supports this. You can have identifiers in code and the translations in neon, xml, po, … or whatever format the translator supports

The only thing that is missing, and you have a valid point, is the pairing of strings from the files to the code, so you can see the translation and ideally, it would be referenced. I know a few people who develop extensions for PhpStorm and NetBeans, maybe we can convice them to write such extension :)

Last edited by Filip Procházka (2014-06-30 02:23)

hacafrakus
Member | 14
+
0
-

I like the Kdyby/Translation. However, the IDEs implementing the mentioned feature would have to be quite smart. I'm talking about pairing constants like R::MESSAGE or R::s("message") (static properties can't be accessed trough magic methods) versus $this->translator->translate("message"). But I'm not even sure whether I like the example static R class :[

Last edited by hacafrakus (2014-06-30 03:02)

Filip Procházka
Moderator | 4668
+
0
-

I actually don't like the constants solution at all. The IDE plugin doesn't have to be that smart, it just have to reference the strings to the dictionaries, and then maybe show them inlined.

Last edited by Filip Procházka (2014-06-30 14:36)