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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
/*
* Avec ces deux resx (Strings.en.resx et Strings.resx) :
*
<data name="Blue" xml:space="preserve">
<value>blue</value>
</data>
<data name="Red" xml:space="preserve">
<value>red</value>
</data>
<data name="Sedan" xml:space="preserve">
<value>sedan</value>
</data>
<data name="SportsCar" xml:space="preserve">
<value>sports car</value>
</data>
<data name="This_is_a" xml:space="preserve">
<value>This is a</value>
</data>
<data name="UtilityCar" xml:space="preserve">
<value>utility car</value>
</data>
*
<data name="Blue" xml:space="preserve">
<value>bleue</value>
</data>
<data name="Red" xml:space="preserve">
<value>rouge</value>
</data>
<data name="Sedan" xml:space="preserve">
<value>berline</value>
</data>
<data name="SportsCar" xml:space="preserve">
<value>sportive</value>
</data>
<data name="This_is_a" xml:space="preserve">
<value>Ceci est une</value>
</data>
<data name="UtilityCar" xml:space="preserve">
<value>voiture utilitaire</value>
</data>
*
* ... ce programme affiche sur la sortie de la console :
*
This_is_a UtilityCar Blue.
Ceci est une voiture utilitaire bleue.
Ceci est une voiture utilitaire bleue.
Ceci est une voiture utilitaire bleue.
This_is_a Blue UtilityCar.
This is a blue utility car.
This is a blue utility car.
This is a blue utility car.
This_is_a Red SportsCar.
This is a red sports car.
This is a red sports car.
This is a red sports car.
*
*/
namespace CSharp.LocalizeStrategies
{
public abstract class LocalizeStrategy
{
private static Dictionary<Type, LocalizeStrategy> _strategies = new Dictionary<Type, LocalizeStrategy>();
public static LocalizeStrategy GetInstance<S>() where S : LocalizeStrategy
{
if (!_strategies.ContainsKey(typeof(S)))
_strategies.Add(typeof(S), Activator.CreateInstance<S>());
return _strategies[typeof(S)];
}
public abstract ILocalizer GetLocalizer(params object[] args);
}
public interface ILocalizer
{
LocalizeStrategy Strategy { get; }
string Localize(string token);
string Localize(ILocalizable subject, string token);
}
public abstract class BaseLocalizer : ILocalizer
{
public abstract LocalizeStrategy Strategy { get; }
public abstract string Localize(string token);
public abstract string Localize(ILocalizable subject, string token);
}
public class NullLocalizer : BaseLocalizer
{
private LocalizeStrategy _strategy = null;
public NullLocalizer(LocalizeStrategy strategy)
{
_strategy = strategy;
}
public override LocalizeStrategy Strategy { get { return _strategy; } }
public override string Localize(string token)
{
return token;
}
public override string Localize(ILocalizable subject, string token)
{
return token;
}
}
public class ResourceBasedLocalizer : NullLocalizer
{
private static ResourceManager _resMan = new ResourceManager(typeof(Strings));
public ResourceBasedLocalizer(LocalizeStrategy strategy)
: base(strategy)
{
}
public override string Localize(string token)
{
return _resMan.GetString(token, Thread.CurrentThread.CurrentCulture);
}
}
public class VehicleLocalizer : ResourceBasedLocalizer
{
public VehicleLocalizer(LocalizeStrategy strategy) : base(strategy)
{
}
public override string Localize(ILocalizable subject, string token)
{
return base.Localize(token);
}
}
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class LocalizedAttribute : Attribute
{
private string _token = null;
public LocalizedAttribute() : this(String.Empty)
{
}
public LocalizedAttribute(string token)
{
_token = token;
}
public string Token { get { return _token; } }
}
public class AttributeBasedLocalizer : ResourceBasedLocalizer
{
public AttributeBasedLocalizer(LocalizeStrategy strategy)
: base(strategy)
{
}
public override string Localize(ILocalizable subject, string token)
{
string actualToken = token;
PropertyInfo[] props = subject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);
foreach (PropertyInfo prop in props)
{
LocalizedAttribute attr = Attribute.GetCustomAttribute(prop, typeof(LocalizedAttribute), true) as LocalizedAttribute;
if (attr.Token == token)
{
actualToken = prop.GetValue(subject, null) as string;
break;
}
}
return base.Localize(actualToken);
}
}
public class ReflectionBasedLocalizer : ResourceBasedLocalizer
{
public ReflectionBasedLocalizer(LocalizeStrategy strategy)
: base(strategy)
{
}
public override string Localize(ILocalizable subject, string token)
{
string actualToken = token;
PropertyInfo prop = subject.GetType().GetProperty(token);
if (prop != null)
actualToken = prop.GetValue(subject, null) as string;
return base.Localize(actualToken);
}
}
public interface ILocalizable
{
}
public abstract class Vehicle : ILocalizable
{
[Localized("Vehicle.Kind")]
public abstract string Kind { get; }
}
public class Car : Vehicle
{
private string _kind = String.Empty;
private string _colour = String.Empty;
public Car(string kind)
{
_kind = kind;
}
public override string Kind { get { return _kind; } }
[Localized("Car.Colour")]
public virtual string Colour { get { return _colour; } set { _colour = value; } }
}
public sealed class ContextFree : LocalizeStrategy
{
public static readonly ILocalizer NullLocalizer = new NullLocalizer(null);
public static readonly ILocalizer ResourceBasedLocalizer = new ResourceBasedLocalizer(null);
public override ILocalizer GetLocalizer(params object[] args)
{
if (args.Length < 1)
return NullLocalizer;
string key = args[0] as string;
switch (key)
{
case "resx":
return ResourceBasedLocalizer;
default:
return NullLocalizer;
}
}
}
public sealed class ByType : LocalizeStrategy
{
private Dictionary<Type, ILocalizer> _localizers = new Dictionary<Type, ILocalizer>();
public ByType()
{
_localizers.Add(typeof(Vehicle), new VehicleLocalizer(this));
}
public override ILocalizer GetLocalizer(params object[] args)
{
ILocalizer localizer = null;
foreach(Type type in _localizers.Keys)
if (type.IsInstanceOfType(args[0]))
{
localizer = _localizers[type];
break;
}
return localizer;
}
}
public sealed class ByAttribute : LocalizeStrategy
{
private static AttributeBasedLocalizer _localizer = null;
public override ILocalizer GetLocalizer(params object[] args)
{
if (_localizer == null)
_localizer = new AttributeBasedLocalizer(this);
return _localizer;
}
}
public sealed class ByReflection : LocalizeStrategy
{
private static ReflectionBasedLocalizer _localizer = null;
public override ILocalizer GetLocalizer(params object[] args)
{
if (_localizer == null)
_localizer = new ReflectionBasedLocalizer(this);
return _localizer;
}
}
class Program
{
private static string Localized(string token, bool doLocalize)
{
ILocalizer localizer = LocalizeStrategy.GetInstance<ContextFree>().GetLocalizer(doLocalize ? "resx" : "null");
return localizer.Localize(token);
}
private static string Localized(string token)
{
return Localized(token, true);
}
private static string NonLocalized(string token)
{
return Localized(token, false);
}
private static string Localized<S>(ILocalizable context, string token) where S : LocalizeStrategy
{
ILocalizer localizer = LocalizeStrategy.GetInstance<S>().GetLocalizer(context);
return localizer.Localize(context, token);
}
private static string Adjectivate(string adjective, string noun, bool prepend)
{
if (prepend)
return String.Concat(adjective, " ", noun);
else
return String.Concat(noun, " ", adjective);
}
private static void ApplyStrategiesOnCar(Car car)
{
bool englishMode = (String.Compare(Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName, "en", true) == 0);
Console.WriteLine("{0} {1}.", NonLocalized("This_is_a"), Adjectivate(NonLocalized(car.Colour), NonLocalized(car.Kind), englishMode));
Console.WriteLine("{0} {1}.", Localized("This_is_a"), Adjectivate(Localized<ByType>(car, car.Colour), Localized<ByType>(car, car.Kind), englishMode));
Console.WriteLine("{0} {1}.", Localized("This_is_a"), Adjectivate(Localized<ByAttribute>(car, "Car.Colour"), Localized<ByAttribute>(car, "Vehicle.Kind"), englishMode));
Console.WriteLine("{0} {1}.", Localized("This_is_a"), Adjectivate(Localized<ByReflection>(car, "Colour"), Localized<ByReflection>(car, "Kind"), englishMode));
}
static void Main(string[] args)
{
Car car = new Car("UtilityCar");
car.Colour = "Blue";
ApplyStrategiesOnCar(car);
Console.WriteLine();
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
ApplyStrategiesOnCar(car);
Console.WriteLine();
car = new Car("SportsCar");
car.Colour = "Red";
ApplyStrategiesOnCar(car);
Console.ReadLine();
}
}
} |
Partager