Replace NotNullAttribute with thrown exceptions

This commit is contained in:
Pranav K 2015-10-08 10:42:40 -07:00
Родитель 722fbc4ef5
Коммит 55a1fab053
21 изменённых файлов: 349 добавлений и 95 удалений

Просмотреть файл

@ -5,7 +5,8 @@ using System;
using System.Globalization;
using System.IO;
using System.Linq;
using Microsoft.Dnx.Runtime;using Microsoft.Win32;
using Microsoft.Dnx.Runtime;
using Microsoft.Win32;
namespace CultureInfoGenerator
{

Просмотреть файл

@ -1,11 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Globalization;
using Microsoft.Extensions.Internal;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNet.Localization
@ -23,8 +23,13 @@ namespace Microsoft.AspNet.Localization
public int MaximumAcceptLanguageHeaderValuesToTry { get; set; } = 3;
/// <inheritdoc />
public override Task<RequestCulture> DetermineRequestCulture([NotNull] HttpContext httpContext)
public override Task<RequestCulture> DetermineRequestCulture(HttpContext httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
var acceptLanguageHeader = httpContext.Request.GetTypedHeaders().AcceptLanguage;
if (acceptLanguageHeader == null || acceptLanguageHeader.Count == 0)
@ -40,7 +45,7 @@ namespace Microsoft.AspNet.Localization
// attempt to parse as a CultureInfo to mitigate potentially spinning CPU on lots of parse attempts.
languages = languages.Take(MaximumAcceptLanguageHeaderValuesToTry);
}
var orderedLanguages = languages.OrderByDescending(h => h, StringWithQualityHeaderValueComparer.QualityComparer)
.ToList();

Просмотреть файл

@ -1,8 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Localization;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNet.Builder
{
@ -17,8 +17,13 @@ namespace Microsoft.AspNet.Builder
/// </summary>
/// <param name="builder">The <see cref="IApplicationBuilder"/>.</param>
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseRequestLocalization([NotNull] this IApplicationBuilder builder)
public static IApplicationBuilder UseRequestLocalization(this IApplicationBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
var options = new RequestLocalizationOptions();
return UseRequestLocalization(builder, options);
@ -32,8 +37,20 @@ namespace Microsoft.AspNet.Builder
/// <param name="options">The options to configure the middleware with.</param>
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseRequestLocalization(
[NotNull] this IApplicationBuilder builder,
[NotNull] RequestLocalizationOptions options)
=> builder.UseMiddleware<RequestLocalizationMiddleware>(options);
this IApplicationBuilder builder,
RequestLocalizationOptions options)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
return builder.UseMiddleware<RequestLocalizationMiddleware>(options);
}
}
}

Просмотреть файл

@ -5,7 +5,6 @@ using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Globalization;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNet.Localization
{
@ -17,7 +16,7 @@ namespace Microsoft.AspNet.Localization
private static readonly char[] _cookieSeparator = new[] { '|' };
private static readonly string _culturePrefix = "c=";
private static readonly string _uiCulturePrefix = "uic=";
/// <summary>
/// Represent the default cookie name used to track the user's preferred culture information, which is "ASPNET_CULTURE".
/// </summary>
@ -30,8 +29,13 @@ namespace Microsoft.AspNet.Localization
public string CookieName { get; set; } = DefaultCookieName;
/// <inheritdoc />
public override Task<RequestCulture> DetermineRequestCulture([NotNull] HttpContext httpContext)
public override Task<RequestCulture> DetermineRequestCulture(HttpContext httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
var cookie = httpContext.Request.Cookies[CookieName];
if (cookie == null)
@ -51,8 +55,13 @@ namespace Microsoft.AspNet.Localization
/// </summary>
/// <param name="requestCulture">The <see cref="RequestCulture"/>.</param>
/// <returns>The cookie value.</returns>
public static string MakeCookieValue([NotNull] RequestCulture requestCulture)
public static string MakeCookieValue(RequestCulture requestCulture)
{
if (requestCulture == null)
{
throw new ArgumentNullException(nameof(requestCulture));
}
var seperator = _cookieSeparator[0].ToString();
return string.Join(seperator,
@ -66,7 +75,7 @@ namespace Microsoft.AspNet.Localization
/// </summary>
/// <param name="value">The cookie value to parse.</param>
/// <returns>The <see cref="RequestCulture"/> or <c>null</c> if parsing fails.</returns>
public static RequestCulture ParseCookieValue([NotNull] string value)
public static RequestCulture ParseCookieValue(string value)
{
if (string.IsNullOrWhiteSpace(value))
{

Просмотреть файл

@ -4,7 +4,6 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNet.Localization
{
@ -19,13 +18,25 @@ namespace Microsoft.AspNet.Localization
/// Creates a new <see cref="CustomRequestCultureProvider"/> using the specified delegate.
/// </summary>
/// <param name="provider">The provider delegate.</param>
public CustomRequestCultureProvider([NotNull] Func<HttpContext, Task<RequestCulture>> provider)
public CustomRequestCultureProvider(Func<HttpContext, Task<RequestCulture>> provider)
{
if (provider == null)
{
throw new ArgumentNullException(nameof(provider));
}
_provider = provider;
}
/// <inheritdoc />
public override Task<RequestCulture> DetermineRequestCulture([NotNull] HttpContext httpContext)
=> _provider(httpContext);
public override Task<RequestCulture> DetermineRequestCulture(HttpContext httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
return _provider(httpContext);
}
}
}

Просмотреть файл

@ -1,10 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Globalization;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNet.Localization
{
@ -27,8 +27,13 @@ namespace Microsoft.AspNet.Localization
public string UIQueryStringKey { get; set; } = "ui-culture";
/// <inheritdoc />
public override Task<RequestCulture> DetermineRequestCulture([NotNull] HttpContext httpContext)
public override Task<RequestCulture> DetermineRequestCulture(HttpContext httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
var request = httpContext.Request;
if (!request.QueryString.HasValue)
{
@ -65,7 +70,7 @@ namespace Microsoft.AspNet.Localization
// Value for UI culture but not for culture so default to UI culture value for both
queryCulture = queryUICulture;
}
var culture = CultureInfoCache.GetCultureInfo(queryCulture);
var uiCulture = CultureInfoCache.GetCultureInfo(queryUICulture);

Просмотреть файл

@ -1,8 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Globalization;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNet.Localization
{
@ -16,10 +16,9 @@ namespace Microsoft.AspNet.Localization
/// properties set to the same <see cref="CultureInfo"/> value.
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> for the request.</param>
public RequestCulture([NotNull] CultureInfo culture)
: this (culture, culture)
public RequestCulture(CultureInfo culture)
: this(culture, culture)
{
}
/// <summary>
@ -28,8 +27,18 @@ namespace Microsoft.AspNet.Localization
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> for the request to be used for formatting.</param>
/// <param name="uiCulture">The <see cref="CultureInfo"/> for the request to be used for text, i.e. language.</param>
public RequestCulture([NotNull] CultureInfo culture, [NotNull] CultureInfo uiCulture)
public RequestCulture(CultureInfo culture, CultureInfo uiCulture)
{
if (culture == null)
{
throw new ArgumentNullException(nameof(culture));
}
if (uiCulture == null)
{
throw new ArgumentNullException(nameof(uiCulture));
}
Culture = culture;
UICulture = uiCulture;
}

Просмотреть файл

@ -1,7 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Extensions.Internal;
using System;
namespace Microsoft.AspNet.Localization
{
@ -15,8 +15,13 @@ namespace Microsoft.AspNet.Localization
/// </summary>
/// <param name="requestCulture">The <see cref="Localization.RequestCulture"/>.</param>
/// <param name="provider">The <see cref="IRequestCultureProvider"/>.</param>
public RequestCultureFeature([NotNull] RequestCulture requestCulture, IRequestCultureProvider provider)
public RequestCultureFeature(RequestCulture requestCulture, IRequestCultureProvider provider)
{
if (requestCulture == null)
{
throw new ArgumentNullException(nameof(requestCulture));
}
RequestCulture = requestCulture;
Provider = provider;
}

Просмотреть файл

@ -1,13 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNet.Localization
{
@ -19,14 +19,24 @@ namespace Microsoft.AspNet.Localization
{
private readonly RequestDelegate _next;
private readonly RequestLocalizationOptions _options;
/// <summary>
/// Creates a new <see cref="RequestLocalizationMiddleware"/>.
/// </summary>
/// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param>
/// <param name="options">The <see cref="RequestLocalizationOptions"/> representing the options for the <see cref="RequestLocalizationMiddleware"/>.</param>
public RequestLocalizationMiddleware([NotNull] RequestDelegate next, [NotNull] RequestLocalizationOptions options)
public RequestLocalizationMiddleware(RequestDelegate next, RequestLocalizationOptions options)
{
if (next == null)
{
throw new ArgumentNullException(nameof(next));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
_next = next;
_options = options;
}
@ -36,8 +46,13 @@ namespace Microsoft.AspNet.Localization
/// </summary>
/// <param name="context">The <see cref="HttpContext"/>.</param>
/// <returns>A <see cref="Task"/> that completes when the middleware has completed processing.</returns>
public async Task Invoke([NotNull] HttpContext context)
public async Task Invoke(HttpContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var requestCulture = _options.DefaultRequestCulture ??
new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);

Просмотреть файл

@ -5,14 +5,13 @@
"type": "git",
"url": "https://github.com/aspnet/localization"
},
"compilationOptions": {
"warningsAsErrors": true
},
"dependencies": {
"Microsoft.AspNet.Http.Extensions": "1.0.0-*",
"Microsoft.Extensions.Globalization.CultureInfoCache": "1.0.0-*",
"Microsoft.Extensions.Localization.Abstractions": "1.0.0-*",
"Microsoft.Extensions.NotNullAttribute.Sources": {
"type": "build",
"version": "1.0.0-*"
}
"Microsoft.Extensions.Localization.Abstractions": "1.0.0-*"
},
"frameworks": {
"dnx451": { },

Просмотреть файл

@ -5,7 +5,8 @@
"type": "git",
"url": "https://github.com/aspnet/localization"
},
"dependencies": {
"compilationOptions": {
"warningsAsErrors": true
},
"frameworks": {
"dnx451": { },

Просмотреть файл

@ -1,7 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Extensions.Internal;
using System;
namespace Microsoft.Extensions.Localization
{
@ -15,10 +15,9 @@ namespace Microsoft.Extensions.Localization
/// </summary>
/// <param name="name">The name of the string in the resource it was loaded from.</param>
/// <param name="value">The actual string.</param>
public LocalizedString([NotNull] string name, [NotNull] string value)
public LocalizedString(string name, string value)
: this(name, value, resourceNotFound: false)
{
}
/// <summary>
@ -27,14 +26,24 @@ namespace Microsoft.Extensions.Localization
/// <param name="name">The name of the string in the resource it was loaded from.</param>
/// <param name="value">The actual string.</param>
/// <param name="resourceNotFound">Whether the string was found in a resource. Set this to <c>false</c> to indicate an alternate string value was used.</param>
public LocalizedString([NotNull] string name, [NotNull] string value, bool resourceNotFound)
public LocalizedString(string name, string value, bool resourceNotFound)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
Name = name;
Value = value;
ResourceNotFound = resourceNotFound;
}
public static implicit operator string (LocalizedString localizedString)
public static implicit operator string(LocalizedString localizedString)
{
return localizedString.Value;
}

Просмотреть файл

@ -1,8 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Internal;
namespace Microsoft.Extensions.Localization
{
@ -15,8 +15,21 @@ namespace Microsoft.Extensions.Localization
/// <param name="name">The name of the string resource.</param>
/// <returns>The string resource as a <see cref="LocalizedString"/>.</returns>
public static LocalizedString GetString(
[NotNull] this IStringLocalizer stringLocalizer,
[NotNull] string name) => stringLocalizer[name];
this IStringLocalizer stringLocalizer,
string name)
{
if (stringLocalizer == null)
{
throw new ArgumentNullException(nameof(stringLocalizer));
}
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return stringLocalizer[name];
}
/// <summary>
/// Gets the string resource with the given name and formatted with the supplied arguments.
@ -26,16 +39,36 @@ namespace Microsoft.Extensions.Localization
/// <param name="arguments">The values to format the string with.</param>
/// <returns>The formatted string resource as a <see cref="LocalizedString"/>.</returns>
public static LocalizedString GetString(
[NotNull] this IStringLocalizer stringLocalizer,
[NotNull] string name,
params object[] arguments) => stringLocalizer[name, arguments];
this IStringLocalizer stringLocalizer,
string name,
params object[] arguments)
{
if (stringLocalizer == null)
{
throw new ArgumentNullException(nameof(stringLocalizer));
}
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return stringLocalizer[name, arguments];
}
/// <summary>
/// Gets all string resources including those for ancestor cultures.
/// </summary>
/// <param name="stringLocalizer">The <see cref="IStringLocalizer"/>.</param>
/// <returns>The string resources.</returns>
public static IEnumerable<LocalizedString> GetAllStrings([NotNull] this IStringLocalizer stringLocalizer) =>
stringLocalizer.GetAllStrings(includeAncestorCultures: true);
public static IEnumerable<LocalizedString> GetAllStrings(this IStringLocalizer stringLocalizer)
{
if (stringLocalizer == null)
{
throw new ArgumentNullException(nameof(stringLocalizer));
}
return stringLocalizer.GetAllStrings(includeAncestorCultures: true);
}
}
}

Просмотреть файл

@ -1,9 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Extensions.Internal;
namespace Microsoft.Extensions.Localization
{
@ -19,8 +19,13 @@ namespace Microsoft.Extensions.Localization
/// Creates a new <see cref="StringLocalizer{TResourceSource}"/>.
/// </summary>
/// <param name="factory">The <see cref="IStringLocalizerFactory"/> to use.</param>
public StringLocalizer([NotNull] IStringLocalizerFactory factory)
public StringLocalizer(IStringLocalizerFactory factory)
{
if (factory == null)
{
throw new ArgumentNullException(nameof(factory));
}
_localizer = factory.Create(typeof(TResourceSource));
}
@ -28,11 +33,32 @@ namespace Microsoft.Extensions.Localization
public virtual IStringLocalizer WithCulture(CultureInfo culture) => _localizer.WithCulture(culture);
/// <inheritdoc />
public virtual LocalizedString this[[NotNull] string name] => _localizer[name];
public virtual LocalizedString this[string name]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return _localizer[name];
}
}
/// <inheritdoc />
public virtual LocalizedString this[[NotNull] string name, params object[] arguments] =>
_localizer[name, arguments];
public virtual LocalizedString this[string name, params object[] arguments]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return _localizer[name, arguments];
}
}
/// <inheritdoc />
public IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures) =>

Просмотреть файл

@ -5,11 +5,8 @@
"type": "git",
"url": "https://github.com/aspnet/localization"
},
"dependencies": {
"Microsoft.Extensions.NotNullAttribute.Sources": {
"type": "build",
"version": "1.0.0-*"
}
"compilationOptions": {
"warningsAsErrors": true
},
"frameworks": {
"dnx451": { },

Просмотреть файл

@ -1,16 +1,21 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.Internal;
namespace Microsoft.Extensions.Localization.Internal
{
public class AssemblyWrapper
{
public AssemblyWrapper([NotNull] Assembly assembly)
public AssemblyWrapper(Assembly assembly)
{
if (assembly == null)
{
throw new ArgumentNullException(nameof(assembly));
}
Assembly = assembly;
}

Просмотреть файл

@ -3,7 +3,6 @@
using System;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Localization;
namespace Microsoft.Extensions.DependencyInjection
@ -18,8 +17,13 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddLocalization([NotNull] this IServiceCollection services)
public static IServiceCollection AddLocalization(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
return AddLocalization(services, setupAction: null);
}
@ -30,9 +34,14 @@ namespace Microsoft.Extensions.DependencyInjection
/// <param name="setupAction">An action to configure the <see cref="LocalizationOptions"/>.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddLocalization(
[NotNull] this IServiceCollection services,
this IServiceCollection services,
Action<LocalizationOptions> setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.TryAdd(new ServiceDescriptor(
typeof(IStringLocalizerFactory),
typeof(ResourceManagerStringLocalizerFactory),

Просмотреть файл

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
@ -34,24 +35,47 @@ namespace Microsoft.Extensions.Localization
/// <param name="baseName">The base name of the embedded resource in the <see cref="Assembly"/> that contains the strings.</param>
/// <param name="resourceNamesCache">Cache of the list of strings for a given resource assembly name.</param>
public ResourceManagerStringLocalizer(
[NotNull] ResourceManager resourceManager,
[NotNull] Assembly resourceAssembly,
[NotNull] string baseName,
[NotNull] IResourceNamesCache resourceNamesCache)
ResourceManager resourceManager,
Assembly resourceAssembly,
string baseName,
IResourceNamesCache resourceNamesCache)
: this(resourceManager, new AssemblyWrapper(resourceAssembly), baseName, resourceNamesCache)
{
if (resourceAssembly == null)
{
throw new ArgumentNullException(nameof(resourceAssembly));
}
}
/// <summary>
/// Intended for testing purposes only.
/// </summary>
public ResourceManagerStringLocalizer(
[NotNull] ResourceManager resourceManager,
[NotNull] AssemblyWrapper resourceAssemblyWrapper,
[NotNull] string baseName,
[NotNull] IResourceNamesCache resourceNamesCache)
ResourceManager resourceManager,
AssemblyWrapper resourceAssemblyWrapper,
string baseName,
IResourceNamesCache resourceNamesCache)
{
if (resourceManager == null)
{
throw new ArgumentNullException(nameof(resourceManager));
}
if (resourceAssemblyWrapper == null)
{
throw new ArgumentNullException(nameof(resourceAssemblyWrapper));
}
if (baseName == null)
{
throw new ArgumentNullException(nameof(baseName));
}
if (resourceNamesCache == null)
{
throw new ArgumentNullException(nameof(resourceNamesCache));
}
_resourceAssemblyWrapper = resourceAssemblyWrapper;
_resourceManager = resourceManager;
_resourceBaseName = baseName;
@ -59,20 +83,30 @@ namespace Microsoft.Extensions.Localization
}
/// <inheritdoc />
public virtual LocalizedString this[[NotNull] string name]
public virtual LocalizedString this[string name]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
var value = GetStringSafely(name, null);
return new LocalizedString(name, value ?? name, resourceNotFound: value == null);
}
}
/// <inheritdoc />
public virtual LocalizedString this[[NotNull] string name, params object[] arguments]
public virtual LocalizedString this[string name, params object[] arguments]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
var format = GetStringSafely(name, null);
var value = string.Format(format ?? name, arguments);
return new LocalizedString(name, value, resourceNotFound: format == null);
@ -110,8 +144,13 @@ namespace Microsoft.Extensions.Localization
/// <param name="includeAncestorCultures"></param>
/// <param name="culture">The <see cref="CultureInfo"/> to get strings for.</param>
/// <returns>The strings.</returns>
protected IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures, [NotNull] CultureInfo culture)
protected IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures, CultureInfo culture)
{
if (culture == null)
{
throw new ArgumentNullException(nameof(culture));
}
var resourceNames = includeAncestorCultures
? GetResourceNamesFromCultureHierarchy(culture)
: GetResourceNamesForCulture(culture);
@ -130,8 +169,13 @@ namespace Microsoft.Extensions.Localization
/// <param name="name">The name of the string resource.</param>
/// <param name="culture">The <see cref="CultureInfo"/> to get the string for.</param>
/// <returns>The resource string, or <c>null</c> if none was found.</returns>
protected string GetStringSafely([NotNull] string name, CultureInfo culture)
protected string GetStringSafely(string name, CultureInfo culture)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
var cacheKey = $"name={name}&culture={(culture ?? CultureInfo.CurrentUICulture).Name}";
if (_missingManifestCache.ContainsKey(cacheKey))

Просмотреть файл

@ -6,7 +6,6 @@ using System.IO;
using System.Reflection;
using System.Resources;
using Microsoft.Dnx.Runtime;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.OptionsModel;
namespace Microsoft.Extensions.Localization
@ -28,9 +27,19 @@ namespace Microsoft.Extensions.Localization
/// <param name="applicationEnvironment">The <see cref="IApplicationEnvironment"/>.</param>
/// <param name="localizationOptions">The <see cref="IOptions{LocalizationOptions}"/>.</param>
public ResourceManagerStringLocalizerFactory(
[NotNull] IApplicationEnvironment applicationEnvironment,
[NotNull] IOptions<LocalizationOptions> localizationOptions)
IApplicationEnvironment applicationEnvironment,
IOptions<LocalizationOptions> localizationOptions)
{
if (applicationEnvironment == null)
{
throw new ArgumentNullException(nameof(applicationEnvironment));
}
if (localizationOptions == null)
{
throw new ArgumentNullException(nameof(localizationOptions));
}
_applicationEnvironment = applicationEnvironment;
_resourcesRelativePath = localizationOptions.Value.ResourcesPath ?? string.Empty;
if (!string.IsNullOrEmpty(_resourcesRelativePath))
@ -46,8 +55,13 @@ namespace Microsoft.Extensions.Localization
/// </summary>
/// <param name="resourceSource">The <see cref="Type"/>.</param>
/// <returns>The <see cref="ResourceManagerStringLocalizer"/>.</returns>
public IStringLocalizer Create([NotNull] Type resourceSource)
public IStringLocalizer Create(Type resourceSource)
{
if (resourceSource == null)
{
throw new ArgumentNullException(nameof(resourceSource));
}
var typeInfo = resourceSource.GetTypeInfo();
var assembly = typeInfo.Assembly;
var baseName = _applicationEnvironment.ApplicationName + "." + _resourcesRelativePath + resourceSource.Name;
@ -65,8 +79,13 @@ namespace Microsoft.Extensions.Localization
/// <param name="baseName">The base name of the resource to load strings from.</param>
/// <param name="location">The location to load resources from.</param>
/// <returns>The <see cref="ResourceManagerStringLocalizer"/>.</returns>
public IStringLocalizer Create([NotNull] string baseName, string location)
public IStringLocalizer Create(string baseName, string location)
{
if (baseName == null)
{
throw new ArgumentNullException(nameof(baseName));
}
var rootPath = location ?? _applicationEnvironment.ApplicationName;
var assembly = Assembly.Load(new AssemblyName(rootPath));
baseName = rootPath + "." + _resourcesRelativePath + baseName;

Просмотреть файл

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
@ -26,31 +27,66 @@ namespace Microsoft.Extensions.Localization
/// <param name="resourceNamesCache">Cache of the list of strings for a given resource assembly name.</param>
/// <param name="culture">The specific <see cref="CultureInfo"/> to use.</param>
public ResourceManagerWithCultureStringLocalizer(
[NotNull] ResourceManager resourceManager,
[NotNull] Assembly resourceAssembly,
[NotNull] string baseName,
[NotNull] IResourceNamesCache resourceNamesCache,
[NotNull] CultureInfo culture)
ResourceManager resourceManager,
Assembly resourceAssembly,
string baseName,
IResourceNamesCache resourceNamesCache,
CultureInfo culture)
: base(resourceManager, resourceAssembly, baseName, resourceNamesCache)
{
if (resourceManager == null)
{
throw new ArgumentNullException(nameof(resourceManager));
}
if (resourceAssembly == null)
{
throw new ArgumentNullException(nameof(resourceAssembly));
}
if (baseName == null)
{
throw new ArgumentNullException(nameof(baseName));
}
if (resourceNamesCache == null)
{
throw new ArgumentNullException(nameof(resourceNamesCache));
}
if (culture == null)
{
throw new ArgumentNullException(nameof(culture));
}
_culture = culture;
}
/// <inheritdoc />
public override LocalizedString this[[NotNull] string name]
public override LocalizedString this[string name]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
var value = GetStringSafely(name, _culture);
return new LocalizedString(name, value ?? name);
}
}
/// <inheritdoc />
public override LocalizedString this[[NotNull] string name, params object[] arguments]
public override LocalizedString this[string name, params object[] arguments]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
var format = GetStringSafely(name, _culture);
var value = string.Format(_culture, format ?? name, arguments);
return new LocalizedString(name, value ?? name, resourceNotFound: format == null);

Просмотреть файл

@ -5,14 +5,13 @@
"type": "git",
"url": "https://github.com/aspnet/localization"
},
"compilationOptions": {
"warningsAsErrors": true
},
"dependencies": {
"Microsoft.Dnx.Runtime.Abstractions": "1.0.0-*",
"Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0-*",
"Microsoft.Extensions.Localization.Abstractions": "1.0.0-*",
"Microsoft.Extensions.NotNullAttribute.Sources": {
"type": "build",
"version": "1.0.0-*"
},
"Microsoft.Extensions.OptionsModel": "1.0.0-*"
},
"frameworks": {