Languages - How to add or change phrases used by the forum software
From Online Manual
This page explains how to work with language files and strings in SMF
Defining Language Files and Strings
Language files are simple groupings of language strings, the words phrases and sentences, which are meant to make it easier to link templates, sub templates, and language strings together. For performance and scope reasons not every string is available to every template, instead they are only available through the "loaded" language files.
Generally speaking, language files are loaded with the same names as the templates in which they are used. There are exceptions to this, such as Login being used by Register, but the majority follows the general rule.
There is one special language file which is called "Modifications". This language file is meant mainly for packages and modifications, but it can also be used to specify more strings in many cases.
The language strings themselves are the "cream filling"; for example, a phrase such as "Send this Topic to a friend" could be a language string. Every language string has an identifier, and for ease of use these are, for the most part, short English strings explaining the purpose of the string.
The format is, for the most part, prefix_string_name. The above example might have the identifier 'sendtopic_to_friend'. This serves the purpose of grouping it (it is part of the sendtopic functionality) and labelling it.
How to Use Language Strings
The fact that SMF uses language strings is all well and good, but what does that mean to you? If you use english in your theme, will that get automatically translated for you? Heh, we wish. No, you have to specifically use it in your theme - this means no english phrases :/. To have strings also available in languages like German or Thai, you will need the $txt variable. This is a lot like $context, except that it stores only a list of language strings available. To use our above example, you might use something like $txt['sendtopic_to_friend']. In your template, it would look a bit like this:
<b>', $txt['sendtopic_to_friend'], '</b>
As you can see, the identifier goes inside the brackets after $txt, surrounded by single quotes - just like contextual data. You have to use the ', ... , ' format to separate the string from regular text, though, as well.
But, how do I use a language string not in the default set...
With themes that really change the look of SMF, this is inevitable. It is for this reason specifically that language files can be tied directly to themes, not to the entire forum.
Just like templates, if you include a language file in your theme, it will be loaded from your theme, but if it isn't there it won&'t be. Here's the rule with language files:
- Is there a *language file*.*user's language*.php in this theme?
- Is there a *language file*.*forum language*.php in this theme?
- Is there a *language file*.*user's language*.php in the default theme?
- Is there a *language file*.*forum language*.php in the default theme?
However, if the forum's default language is Spanish, and you only have English available, it will end up falling back on the default language files.
It is for this reason that it is sometimes necessary to add new language files. This helps make it clear that a new language file is needed for this theme, and that the default ones - even translated - will not do. To load a new language file, you will need to open your index template. Look for the "init" sub template, which can be found by searching for "template_init". This is a special initialization sub template. Right before the closing curly brace (}), add the following: loadLanguage('Theme'); And you're all set! The file, Theme.somelang.php might look like this:
<?php // Version: 1.0 ; Theme $txt['mytheme_hello'] = 'How are you today?'; ?>
It's recommended you use Theme, just because this makes it easier to tell which language file is yours for translators. You might want to look at some other language files, and other themes, to understand this better.
Some notes to remember...
A very important note is that if you want something like "don't", you need to use a \ to "escape" the '. See, if I said 'don't', it would be confusing... do I mean 'don' + t', or 'don't'? To "escape" this problem (pun intended) you just use 'don\'t'.
When translating, especially, you may need to use a special character, such as Å. Because of html validity, issues with PHP, and other things, it's best for you to instead use entities - these are like Å. Note that some strings should not have entities in them, and they should be noted as such.
Some other guidelines to follow for better internationalization (a fancy word for translatability):
- Whenever possible, try not to force word order. This means, don't take two strings and put them together - like 'How are ' . 'you'... this will make it harder to translate.
- Remember that some words have more than one meaning in English, but don't in other languages. Try to avoid using a word as a verb and a noun in different places.
- Be aware that some other languages may have very different rules for dates and pluralization than your native language.
Hopefully this will help you on your way to making themes that are different, wonderfully unique, and above all translatable - our non-English users thank you!