1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
[DesignTimeResourceProviderFactoryAttribute(typeof(RepairResourceProviderFactory))]
class RepairResourceProviderFactory : ResourceProviderFactory
{
public override IResourceProvider CreateGlobalResourceProvider(string classname)
{
return new RepairRessourceProvider( null, classname);
}
public override IResourceProvider CreateLocalResourceProvider(string virtualPath)
{
string classKey = virtualPath;
if (!string.IsNullOrEmpty(virtualPath))
{
virtualPath = virtualPath.Remove(0, 1);
classKey = virtualPath.Remove(0, virtualPath.IndexOf('/') + 1);
}
return new RepairRessourceProvider(classKey, null);
}
}
/// <summary>
/// Resource Provider marker interface. Also provides for clearing resources.
/// </summary>
public interface IwwResourceProvider
{
/// <summary>
/// Interface method used to force providers to register themselves
/// with wwDbResourceConfiguration.
/// </summary>
void ClearResourceCache();
}
/// <summary>
/// Provider de ressource
/// </summary>
public class RepairRessourceProvider : IResourceProvider, IwwResourceProvider
{
/// <summary>
///
/// </summary>
string _virtualPath;
/// <summary>
///
/// </summary>
string _className;
/// <summary>
/// Keep track of the 'className' passed by ASP.NET
/// which is the ResourceSetId in the database.
/// </summary>
private string _ResourceSetName;
/// <summary>
/// Cache for each culture of this ResourceSet. Once
/// loaded we just cache the resources.
/// </summary>
private IDictionary _resourceCache;
/// <summary>
/// Critical section for loading Resource Cache safely
/// </summary>
private static object _SyncLock = new object();
/// <summary>
///
/// </summary>
private RepairResourceReader _ResourceReader = null;
/// <summary>
/// Constructeur par défaut
/// </summary>
/// <param name="virtualPath"></param>
/// <param name="classname"></param>
public RepairRessourceProvider(string virtualPath, string classname)
{
_virtualPath = virtualPath;
_className = classname;
}
/// <summary>
/// Manages caching of the Resource Sets. Once loaded the values are loaded from the
/// cache only.
/// </summary>
/// <param name="cultureName"></param>
/// <returns></returns>
private IDictionary GetResourceCache(string cultureName)
{
if (cultureName == null)
cultureName = "";
if (this._resourceCache == null)
this._resourceCache = new ListDictionary();
IDictionary Resources = this._resourceCache[cultureName] as IDictionary;
if (Resources == null)
{
ExtranetRepairDataContext _data = new ExtranetRepairDataContext();
lock (_SyncLock)
{
if (this._resourceCache.Contains(cultureName))
Resources = this._resourceCache[cultureName] as IDictionary;
else
{
Resources = _data.Traductions
.Where(t => t.CodeLange == cultureName)
.ToDictionary(t => t.Variable, tt => tt.Libelle);
_resourceCache[cultureName] = Resources;
}
}
}
return Resources;
}
/// <summary>
/// Clears out the resource cache which forces all resources to be reloaded from
/// the database.
///
/// This is never actually called as far as I can tell
/// </summary>
public void ClearResourceCache()
{
this._resourceCache.Clear();
}
/// <summary>
/// The main worker method that retrieves a resource key for a given culture
/// from a ResourceSet.
/// </summary>
/// <param name="resourceKey"></param>
/// <param name="culture"></param>
/// <returns></returns>
object IResourceProvider.GetObject(string ResourceKey, CultureInfo Culture)
{
string CultureName = null;
if (Culture != null)
CultureName = Culture.Name;
else
CultureName = CultureInfo.CurrentUICulture.Name;
return this.GetObjectInternal(ResourceKey, CultureName);
}
/// <summary>
/// Internal lookup method that handles retrieving a resource
/// by its resource id and culture. Realistically this method
/// is always called with the culture being null or empty
/// but the routine handles resource fallback in case the
/// code is manually called.
/// </summary>
/// <param name="ResourceKey"></param>
/// <param name="CultureName"></param>
/// <returns></returns>
object GetObjectInternal(string ResourceKey, string CultureName)
{
IDictionary Resources = this.GetResourceCache(CultureName);
object value = null;
if (Resources == null)
value = null;
else
value = Resources[ResourceKey];
// *** If we're at a specific culture (en-Us) and there's no value fall back
// *** to the generic culture (en)
if (value == null && CultureName.Length > 3)
{
// *** try again with the 2 letter locale
return GetObjectInternal(ResourceKey, CultureName.Substring(0, 2));
}
// *** If the value is still null get the invariant value
if (value == null)
{
Resources = this.GetResourceCache("");
if (Resources == null)
value = null;
else
value = Resources[ResourceKey];
}
// Si la valeur de la clé est nulle et que nous sommes dans la cultur invariante
// Laisser ajouter un marqueyr que la valuer est maquante
// Cela permet un fonctionement a la precompilation et de ne jamais renvoyer null
if (value == null && string.IsNullOrEmpty(CultureName))
{
// *** No entry there
value = "";
ExtranetRepairDataContext _data = new ExtranetRepairDataContext();
return _data.Traductions
.Where(t => t.CodeLange == "en")
.ToDictionary(t => t.Variable, tt => tt.Libelle);
}
return value;
}
/// <summary>
/// The Resource Reader is used parse over the resource collection
/// that the ResourceSet contains. It's basically an IEnumarable interface
/// implementation and it's what's used to retrieve the actual keys
/// </summary>
public IResourceReader ResourceReader // IResourceProvider.ResourceReader
{
get
{
if (this._ResourceReader != null)
return this._ResourceReader as IResourceReader;
this._ResourceReader = new RepairResourceReader(GetResourceCache(null));
return this._ResourceReader as IResourceReader;
}
}
} |
Partager