Skip to content
29 changes: 0 additions & 29 deletions src/BootstrapBlazor.Server/Components/Pages/Localization.razor
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@

<p><b>2. @Localizer["N24"]</b></p>

<Tab>
<TabItem Text="NET6.0" Icon="fa-solid fa-code" Closable="false">
<Pre>builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();

Comment thread
ArgoZhang marked this conversation as resolved.
Expand All @@ -97,33 +95,6 @@ if (option != null)
app.UseRequestLocalization(option.Value);
}
</Pre>
</TabItem>
<TabItem Text="NET5.0" Icon="fa-solid fa-code" Closable="false">
<Pre class="mb-3">public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// @Localizer["N28"]
services.AddBootstrapBlazor();

// @Localizer["N29"]
services.AddRequestLocalization&lt;IOptions&lt;BootstrapBlazorOptions&gt;&gt;((localizerOption, blazorOption) =>
{
var supportedCultures = blazorOption.Value.GetSupportedCultures();

localizerOption.SupportedCultures = supportedCultures;
localizerOption.SupportedUICultures = supportedCultures;
});
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// @Localizer["N30"]
app.UseRequestLocalization(app.ApplicationServices.GetService&lt;IOptions&lt;RequestLocalizationOptions&gt;&gt;()!.Value);
}
}</Pre>
</TabItem>
</Tab>

<p><b>3. @Localizer["N31"]</b></p>
<Pre class="mb-3">[Route("[controller]/[action]")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ private async Task OnClick()
{
var provider = IpLocatorFactory.Create(ProviderName);
Location = await provider.Locate(Ip);

if (!string.IsNullOrEmpty(provider.LastError))
{
Location = provider.LastError;
Comment thread
ArgoZhang marked this conversation as resolved.
}
Comment thread
ArgoZhang marked this conversation as resolved.
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/BootstrapBlazor.Server/Locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,7 @@
"N25": "Add Bootstrap Blazor components",
"N26": "Add multi-language support configuration information",
"N27": "Enable localization",
"N28": "Add BootstrapBlazor component",
"N29": "Add localization service configuration information",
"N3": "Type key-value information as a resource file, which is parsed as",
"N30": "Enable localization middleware and set supported culture info",
"N31": "Implement UI localization information storage (for example, cookies)",
"N32": "Add UI that allows users to change localization",
"N33": "Please select language",
Expand Down
3 changes: 0 additions & 3 deletions src/BootstrapBlazor.Server/Locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,7 @@
"N25": "增加 Bootstrap Blazor 组件",
"N26": "增加多语言支持配置信息",
"N27": "启用本地化",
"N28": "增加 BootstrapBlazor 组件",
"N29": "增加本地化服务配置信息",
"N3": "类型的键值信息作为资源文件,将其解析为",
"N30": "启用本地化中间件并设置支持的文化信息",
"N31": "实现 UI 本地化信息存储(例如,cookie)",
"N32": "添加允许用户更改本地化的 UI",
"N33": "请选择语言",
Expand Down
38 changes: 20 additions & 18 deletions src/BootstrapBlazor/Services/IPLocator/BaiduIpLocatorProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,25 @@ public class BaiduIpLocatorProvider(IHttpClientFactory httpClientFactory, IOptio
/// <param name="token"></param>
protected virtual async Task<string?> Fetch(string url, HttpClient client, CancellationToken token)
{
var result = await client.GetFromJsonAsync<LocationResult>(url, token);
return result?.ToString();
string? ret = null;
try
{
var result = await client.GetFromJsonAsync<LocationResult>(url, token);
if (result is { Status: "0" })
{
var location = result.Data.FirstOrDefault();
if (location != null)
{
ret = location.Location;
}
Comment thread
ArgoZhang marked this conversation as resolved.
}
}
catch (Exception ex)
{
LastError = ex.Message;
}

return ret;
}

/// <summary>
Expand All @@ -81,22 +98,7 @@ class LocationResult
/// <para lang="zh">获得/设置 定位信息</para>
/// <para lang="en">Gets or sets Location Info</para>
/// </summary>
public List<LocationData>? Data { get; set; }

/// <summary>
/// <inheritdoc/>
/// <para lang="en"><inheritdoc/></para>
/// </summary>
/// <returns></returns>
public override string? ToString()
{
string? ret = null;
if (Status == "0")
{
ret = Data?.FirstOrDefault()?.Location;
}
return ret;
}
public List<LocationData> Data { get; set; } = [];
}

[ExcludeFromCodeCoverage]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,23 @@ protected DefaultIpLocatorProvider(IOptions<BootstrapBlazorOptions> options)
private readonly List<string> _localhostList = [.. new[] { "::1", "127.0.0.1" }];

/// <summary>
/// <inheritdoc/>
/// <inheritdoc cref="IIpLocatorProvider.Key"/>
/// </summary>
public string? Key { get; set; }

/// <summary>
/// <inheritdoc cref="IIpLocatorProvider.LastError"/>
/// </summary>
public string? LastError { get; protected set; }

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="ip"></param>
public async Task<string?> Locate(string? ip)
{
string? ret = null;
LastError = null;

Comment thread
ArgoZhang marked this conversation as resolved.
// 解析本机地址
if (string.IsNullOrEmpty(ip) || _localhostList.Any(p => p == ip))
Expand Down
6 changes: 6 additions & 0 deletions src/BootstrapBlazor/Services/IPLocator/IIpLocatorProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ public interface IIpLocatorProvider
/// </summary>
/// <param name="ip"></param>
Task<string?> Locate(string? ip);

/// <summary>
/// <para lang="zh">获得 上次错误描述信息</para>
/// <para lang="en">Get the previous error detail</para>
Comment thread
ArgoZhang marked this conversation as resolved.
/// </summary>
string? LastError { get; }
}