When you install additional languages into Telligent Community users have the option to choose one of those installed languages as their preferred language. This will mean all content not generated dynamically or by the community will be translated into that language. If someone is browsing the site and not logged in, the site will evaluate the Accept-Language HTTP Header sent by browser. By default Telligent Community ships with U.S English as the language(en-US).
When Would You Would Use ILanguageDetector?
As mentioned, a site uses the following logic to determine what language should be used:
There may however be scenarios where you would like alter the logic that determines what language to display. You can implement a custom ILanguageDetector plugin to completely change the logic that is used to make this decision. ILanguageDetector is an ISingletonPlugin type, therefore you can only have 1 enabled at a time.
Creating a Custom Language Detector
In the rare instance you want to create a new way of defining the site language you can do so by creating a new plugin implementing ILanguageDetector. It's also important know that ILanguageDetector is a singleton plugin, meaning a site can only have a single language detector enabled at a time. This means in order to use a custom ILanguageDetector you must first disable any existing ones which includes the default one that ships in Telligent Community. It is called "Anonymous User Language Detector" and can be found in Administration under the Extensions menu option.
Required References:
- Telligent.Evolution.Platform.dll
- System.Web (.NET Assembly Reference)
Sample: Cookie Based Language Detector
We are going to create a sample that reads a cookie for logged in users to get the language and for unauthenticated users browsing the site it will be in French(fr-FR).
We'll start by creating a new plugin called SampleLanguageDetection that implements from ILanguageDetector. You will get the standard IPlugin members(omitted for brevity) and a DetectLanguage(ILanguageDetectionController) method that returns a language code as a string. If you wish to defer to the user or anonymous user's chosen language, return null.
public string DetectLanguage(ILanguageDetectionController controller) { }
ILanguageDetectorController gives access to several items we can use in our implementation:
- UserId - The user Id of the currently logged in user. If its null then its an anonymous(unauthenticated) request.
- HttpContext - The current HttpContext as HttpContextBase. If its null you are not in the scope of a web request and more than in likely in a job execution.
- SupportedLanguages - The current set of language packs installed in the community as a readonly list of language code strings.
- IsLanguageSupported - A method that will tell you if a given language key is supported on this site. You cannot return a language key if that language is not currently installed.