/// <summary>Looks up for a translation bundle in the global cache.</summary> /// <remarks> /// Looks up for a translation bundle in the global cache. If found returns /// the cached bundle. If not found creates a new instance puts it into the /// cache and returns it. /// </remarks> /// <?></?> /// <param name="locale">the preferred locale</param> /// <param name="type">required bundle type</param> /// <returns>an instance of the required bundle type</returns> /// <exception> /// TranslationBundleLoadingException /// see /// <see cref="TranslationBundle.Load(System.Globalization.CultureInfo)">TranslationBundle.Load(System.Globalization.CultureInfo) /// </see> /// </exception> /// <exception> /// TranslationStringMissingException /// see /// <see cref="TranslationBundle.Load(System.Globalization.CultureInfo)">TranslationBundle.Load(System.Globalization.CultureInfo) /// </see> /// </exception> internal static T LookupBundle <T>(CultureInfo locale) where T : TranslationBundle { System.Type type = typeof(T); lock (typeof(GlobalBundleCache)) { try { IDictionary <Type, TranslationBundle> bundles = cachedBundles.Get(locale); if (bundles == null) { bundles = new Dictionary <Type, TranslationBundle>(); cachedBundles.Put(locale, bundles); } TranslationBundle bundle = bundles.Get(type); if (bundle == null) { bundle = (TranslationBundle)System.Activator.CreateInstance(type); bundle.Load(locale); bundles.Put(type, bundle); } return((T)bundle); } catch (InstantiationException e) { throw new Error(e); } catch (MemberAccessException e) { throw new Error(e); } } }
private T Get <T>() where T : TranslationBundle { System.Type type = typeof(T); TranslationBundle bundle = map.Get(type); if (bundle == null) { bundle = GlobalBundleCache.LookupBundle <T>(locale); // There is a small opportunity for a race, which we may // lose. Accept defeat and return the winner's instance. TranslationBundle old = map.PutIfAbsent(type, bundle); if (old != null) { bundle = old; } } return((T)bundle); }