Difference between revisions of "Languages - How to add or change phrases used by the forum software"

From Online Manual

Jump to: navigation, search
Line 2: Line 2:
  
 
===Defining Language Files and Strings===
 
===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.
+
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.
 
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.
Line 13: Line 13:
  
 
===How to Use Language Strings===
 
===How to Use Language Strings===
SMF's language strings allows translation into other language without modifying templates or source files. but if the string is not translated into another language it will default to showing the English version. If you want a string to be translated you need to use the $txt variable. This is similar to how you would use $context, with the only difference being that it stores your language string instead.
+
SMF's language strings allows translation into other languages without modifying templates or source files. but if the string is not translated into another language it will default to showing the English version. If you want a string to be translated you need to use the $txt variable. This is similar to how you would use $context, with the only difference being that it stores your language string instead.
  
 
Using the previous example, you might use something like $txt['sendtopic_to_friend']. In your template, it would look a bit like this:
 
Using the previous example, you might use something like $txt['sendtopic_to_friend']. In your template, it would look a bit like this:
Line 23: Line 23:
 
Themes which significantly alter SMF will often introduce new language strings. It is for this reason that language files are tied directly to themes rather than to the entire forum.
 
Themes which significantly alter SMF will often introduce new language strings. It is for this reason that language files are tied directly to themes rather than 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:
+
If a custom theme is being used, SMF will check that theme's language folder to see if the file exists.  If the file does not exist, SMF will fall back on the default theme.  Similarly, SMF will also check the user's language file.  If that file is not found, SMF will default to English.
  
# Is there a *language file*.*user's language*.php in this theme?
+
For example, if file x.spanish.php is not found in the custom themes folder, SMF will check for x.english.php. If both of these are not fount, SMF will search for x.spanish.php in the default themes language folder.  If this is not found, SMF will use x.english.php, which should always be found.
# 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.
+
When writing a theme, it is sometimes necessary to add new language files. 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 initialisation sub template. Before the closing curly brace (}), add the following: loadLanguage('Theme'); and everything is all set. The file, Theme.somelang.php might look like this:
 
 
It is for this reason that it is sometimes necessary to add new language files. This helps to 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:
 
 
{{code|1=<nowiki><?php
 
{{code|1=<nowiki><?php
 
// Version: 1.0 ; Theme
 
// Version: 1.0 ; Theme
 
$txt['mytheme_hello'] = 'How are you today?';
 
$txt['mytheme_hello'] = 'How are you today?';
 
?></nowiki>}}
 
?></nowiki>}}
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.  
+
It is recommended that you use Theme 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 themes to develop a better understanding of this.  
  
 
===Some Notes to Remember===
 
===Some Notes to Remember===

Revision as of 11:52, 28 March 2016

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

SMF's language strings allows translation into other languages without modifying templates or source files. but if the string is not translated into another language it will default to showing the English version. If you want a string to be translated you need to use the $txt variable. This is similar to how you would use $context, with the only difference being that it stores your language string instead.

Using the previous 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 is placed between single quotes and inside square brackets after $txt, just as you would with contextual data, but you also have to use the ', ... , ' format to separate the string from regular text.

How to use a Language String Not in the Default Set

Themes which significantly alter SMF will often introduce new language strings. It is for this reason that language files are tied directly to themes rather than to the entire forum.

If a custom theme is being used, SMF will check that theme's language folder to see if the file exists. If the file does not exist, SMF will fall back on the default theme. Similarly, SMF will also check the user's language file. If that file is not found, SMF will default to English.

For example, if file x.spanish.php is not found in the custom themes folder, SMF will check for x.english.php. If both of these are not fount, SMF will search for x.spanish.php in the default themes language folder. If this is not found, SMF will use x.english.php, which should always be found.

When writing a theme, it is sometimes necessary to add new language files. 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 initialisation sub template. Before the closing curly brace (}), add the following: loadLanguage('Theme'); and everything is all set. The file, Theme.somelang.php might look like this:

<?php
// Version: 1.0 ; Theme
$txt['mytheme_hello'] = 'How are you today?';
?>

It is recommended that you use Theme 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 themes to develop a better understanding of this.

Some Notes to Remember

  1. If you want to use a contraction like "don't" in a language string, you need to use \ to avoid the '. If you were to write 'don't', it would be confusing because it could mean 'don' + t', or 'don't'? To prevent this problem from occurring you use 'don\'t'.
  2. When translating, you may need to use a special character, such as Å. However, owing to html validity and issues with PHP, it is best to use entities instead of these special characters. Note that some strings should not have entities in them and they should be noted as such.
  3. Whenever possible, try not to force word order. This means avoiding putting two strings together. An example would be 'How are ' . 'you'. Having a string like this would make it harder to translate.
  4. Remember that some words have more than one meaning in English, but not in other languages.
  5. Try to avoid using a word as a verb and a noun in different places.
  6. Be aware that some languages may have very different rules for dates and pluralisation 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!