/*
* Box Social™
* http://boxsocial.net/
* Copyright © 2007, David Lachlan Smith
*
* $Id:$
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Threading;
using System.Web;
using BoxSocial;
using BoxSocial.Internals;
using BoxSocial.IO;
namespace BoxSocial.Internals
{
///
/// Handles string manipulation and translations.
///
/// An application shall have separate language files from the assembly
/// which can be translated, uploaded, and updated without updating the
/// application assembly itself. This facilitates community translation
/// of the application in an efficient manner.
///
/// The structure shall be
/// /language/applicationKey/applicationKey.ISO_languageCode.resources
///
/// A resource file is generated by resgen.exe (distributed with Visual
/// Studio, and mono).
///
public class Prose : IProse
{
private Core core;
private string language;
private CultureInfo culture;
private Dictionary languageResources;
public string Language
{
set
{
language = value;
culture = new CultureInfo(language);
if (language == "en")
{
// We cannot set the thread to a neutral culture,
// but never mind, en-au is a pretty good compromise
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-au");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-au");
}
else
{
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
}
}
internal void Initialise(Core core, string language)
{
this.core = core;
Language = language;
languageResources = new Dictionary();
AddApplication("Internals");
}
internal void AddApplication(string key)
{
try
{
ResourceManager rm = ResourceManager.CreateFileBasedResourceManager(key, Path.Combine(core.Http.LanguagePath, key), null);
languageResources.Add(key, rm);
}
catch
{
// TODO: throw error loading language
}
}
///
/// From Internals
///
///
///
public string GetString(string key)
{
try
{
return languageResources["Internals"].GetString(key, culture);
}
catch
{
foreach (string akey in languageResources.Keys)
{
try
{
return languageResources[akey].GetString(key, culture);
}
catch
{
}
}
}
return "";
}
///
/// From Internals
///
///
///
///
public string GetString(string key, params object[] param)
{
return string.Format(GetString(key), param);
}
///
/// Gets a language string for a specific language
///
///
///
///
public string GetString(string applicationKey, string languageKey)
{
try
{
return languageResources[applicationKey].GetString(languageKey, culture);
}
catch
{
return "";
}
}
///
/// Gets a language string for a specific language
///
///
///
///
///
public string GetString(string applicationKey, string languageKey, params object[] param)
{
return string.Format(GetString(applicationKey, languageKey), param);
}
///
/// Close the resource files
///
public void Close()
{
if (languageResources != null)
{
foreach (string key in languageResources.Keys)
{
ResourceManager rm = languageResources[key];
rm.ReleaseAllResources();
}
}
}
///
/// Queries Internals
///
///
///
public bool ContainsKey(string key)
{
try
{
if (string.IsNullOrEmpty(languageResources["Internals"].GetString(key)))
{
return false;
}
else
{
return true;
}
}
catch
{
return false;
}
}
///
/// Queries the specified application
///
///
///
///
public bool ContainsKey(string applicationKey, string key)
{
try
{
if (string.IsNullOrEmpty(languageResources[applicationKey].GetString(key)))
{
return false;
}
else
{
return true;
}
}
catch
{
return false;
}
}
}
}