UPDATE: If you've come here looking for information about integrating PayPal into Community Server or about paid subscriptions for Community Server, I and several other Community Server technical evangelists have put together a product named Four Roads Commerce. You can find information about it at the Four Roads website.
DISCLAIMER: This is definitely not a "I'm an expert on this topic" post. This is more of a "I'm ignorant and this is the best way I can come up with" post. So if you know of a better way, please fill me in via the comments.
It would be wonderful if communities outside the English-speaking world could use this add-on and have it display text in their native language. And now is the time to prepare for it.
Community Server has a ResourceManager class within the CommunityServer.Components namespace. The installation includes string resources for U.S. English only. But you can download other language packs from the Downloads section of the Community Server website.
To enable a particular language, you must enable it by uncommenting the appropriate entry in the language.xml file, located in directory {CSroot}\web\languages. The entry for your language has a key attribute identifying its culture name (e.g., "en-US" for English, "de-de" for German). The culture name is the name of the subfolder where you must install the language pack.
For example, if you installed the German language pack, you would place the files into {CS root}\web\languages\de-de. The English language pack provided with Community Server is in {CS root}\web\languages\en-US.
We know where to put things. How do we add resource strings specific to our add-on? Here's what I came up with.
Create a resource file
The resource file is an XML file. It should lead off with an xml header followed by a root element. Within the root element, each string used by the add-on must be in its own resource element. Each resource element must have its own name attribute. Pick descriptive names because you'll be using their values in your add-on code.
The inner text of the element is the actual string to be displayed in the user interface. Following is a portion of the SHSubscriber.xml for this project:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<!-- Admin Control Panel: Payment Settings -->
<resource name="Admin_PaymentSettings_Add_Global_Settings">Add settings</resource>
<resource name="Admin_PaymentSettings_Configure">Configure</resource>
<resource name="Admin_PaymentSettings_Global_Description">Configure the payment settings that apply to all sites. This will be overridden by Site-specific settings.</resource>
...
</root>
xml version="1.0" encoding="UTF-8" ?>
<root>
<!-- Admin Control Panel: Payment Settings -->
<resource name="Admin_PaymentSettings_Add_Global_Settings">Add settings</resource>
<resource name="Admin_PaymentSettings_Configure">Configure</resource>
<resource name="Admin_PaymentSettings_Global_Description">Configure the payment settings that apply to all sites. This will be overridden by Site-specific settings.</resource>
...
</root>
Copy it into the language directory
The ResourceManager class is set up to look in directory {CS root}\web\languages\{current language} for resource files.
Actually, it always looks in en-US first, just so that it has an English version of the resource on hand in the event that the current language pack doesn't contain that resource.
Once you have your xml file created, place the English version in en-US and your localized versions in their respective directories.
For this project, I am putting the file SHSubscriber.xml into {CS root}\web\languages\en-US.
Add calls to the ResourceManager
There are at least two ways to make use of a string resource. In your ASPX or ASCX markup, you can display the text of a resource via the ResourceControl. In the following example, we tell the control to load resource "Admin_PaymentSettings_Global_Description" from file "SHSubscriber.xml". You must tell it the name of your resource file in order for it to find the string.
<cp:resourcecontrol runat="Server"
resourcename="Admin_PaymentSettings_Global_Description"
resourcefile="SHSubscriber.xml" />
cp:resourcecontrol runat="Server"
resourcename="Admin_PaymentSettings_Global_Description"
resourcefile="SHSubscriber.xml" /> If you must use code behind to dynamically display strings (e.g., the string displayed changes upon whether the user is adding or editing), you can take advantage of the ResourceManager's GetString method.
It has several overloads. In the following example, we need to display the text "Gateway:" followed by the name of the gateway. We give it the name of the resource and the name of our resource file.
// Get the gateway name.
lblGateway.Text = String.Format(ResourceManager.GetString
("Admin_PaymentSettings_Gateway_Name", Constants.RESOURCE_FILENAME),
gateway.Name);
// Get the gateway name.
lblGateway.Text = String.Format(ResourceManager.GetString
("Admin_PaymentSettings_Gateway_Name", Constants.RESOURCE_FILENAME),
gateway.Name); Note that the name of the resource is stored as a constant so that if it changes, we change it in only one place. Elsewhere, the constant is defined as "SHSubscriber.xml".
Making use of Community Server's ResourceManager is pretty straightforward. The hard part is going to be at the end. We'll need some way to locate the CS installation and place our language file(s) into the right spot. We won't worry about that for now. It'll be quite an achievement if we get all our strings into the resource file to begin with.
--
Sean Winstead
Tags: CommunityServer