我在我的系统中实施了文化。选择组合框的更改工作正常。
[
[
现在我想根据用户语言来改变语言。
我阅读了其他描述,但我无法让它工作。
=======================我的开关类型号===================
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
namespace WorkCollaboration.Models
{
public class SwitchCultureModel
{
public CultureInfo CurrentUICulture { get; set; }
public List<CultureInfo> SupportedCultures { get; set; }
}
}
我有一个通用登录服务,我在其中检查用户权限语言.... 在系统中,我创建了一个页面,我想更改用户语言会话等等。
这就是我的页面的样子
[
=========这是我的剃须刀页面==========
@page
@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@model WorkCollaboration.Pages.LogonService.LanguageModel
@{
ViewData["Title"] = "UserLanguage";
}
@inject IViewLocalizer Localizer
<h1>@Localizer["Edit"]</h1>
<h4>@Localizer["LogonService"]</h4>
<p>
<a asp-page="/LogonService/IndexLanguage">@Localizer["Back to Index"]</a>
</p>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="LogonService.WorkColUserMail" />
<input type="hidden" asp-for="LogonService.WorkColUserId" class="form-control" />
<input type="hidden" asp-for="LogonService.WorkColUserPassword" class="form-control" />
<input type="hidden" asp-for="LogonService.WorkColUserPasswordConfirm" class="form-control" />
<input type="hidden" asp-for="LogonService.WorkColUserName" class="form-control" />
<input type="hidden" asp-for="LogonService.WorkColUserVerificationMode" class="form-control" />
<input type="hidden" asp-for="LogonService.WorkColEnteredPassword" class="form-control" />
<input type="hidden" asp-for="LogonService.WorkColLoggedIn" class="form-control" />
<div class="form-group">
@Html.LabelFor(model => model.LogonService.WorkColUserLanguage, htmlAttributes: new { @class = "form-group" })
<div class="form-group">
@Html.DropDownListFor(model => model.LogonService.WorkColUserLanguage, new List<SelectListItem>
{
new SelectListItem {Text = "DE", Value = "DE", Selected = true },
new SelectListItem {Text = "EN", Value = "EN" },
}, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.LogonService.WorkColUserLanguage, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-page="/LogonService/IndexLanguage">@Localizer["Back to List"]</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
在 c# 中,我调用以下线程
if (LogonService.WorkColUserLanguage == "DE")
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("de");
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("de");
}
if (LogonService.WorkColUserLanguage == "EN")
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("en");
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("en");
}
但是页面语言继续使用相同的语言
==========这是我的c#代码======================
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualBasic;
using WorkCollaboration.Data;
using WorkCollaboration.Models;
using System.Collections;
using System.Web;
namespace WorkCollaboration.Pages.LogonService
{
public class LanguageModel : PageModel
{
private readonly WorkCollaboration.Data.WorkCollaborationContext _context;
public LanguageModel(WorkCollaboration.Data.WorkCollaborationContext context)
{
_context = context;
}
[BindProperty]
public Models.LogonService LogonService { get; set; }
public async Task<IActionResult> OnGetAsync(string id)
{
if (id == null)
{
return NotFound();
}
LogonService = await _context.LogonService.FirstOrDefaultAsync(m => m.WorkColUserMail == id);
if (LogonService == null)
{
return NotFound();
}
return Page();
}
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://aka.ms/RazorPagesCRUD.
public async Task<IActionResult> OnPostAsync()
{
string id = LogonService.WorkColUserMail;
byte[] ep = LogonService.WorkColEnteredPassword;
string ln = LogonService.WorkColUserLanguage;
string SessionKeyName = "_Name";
string SessionKeyId = "_Id";
string SessionKeyDate = "_Date";
string SessionKeyLang = "_Lang";
if (id == null)
{
return NotFound();
}
//====================================
// Load WorColUser Data with Mail Key
//====================================
LogonService = await _context.LogonService.FirstOrDefaultAsync(m => m.WorkColUserMail == id);
//====================================
// Overwrite Data with GUI Data
//====================================
LogonService.WorkColEnteredPassword = ep;
LogonService.WorkColUserLanguage = ln;
LogonService.WorkColLoggedIn = true;
//====================================
// Create SessionID
//====================================
DateTime now = DateTime.Now;
string valueSystemKeyDates = now.ToString("yyyymmddhh:mm:ss");
HttpContext.Session.SetString(SessionKeyDate, valueSystemKeyDates);
HttpContext.Session.SetString(SessionKeyId, Convert.ToString(LogonService.WorkColUserId));
HttpContext.Session.SetString(SessionKeyName, LogonService.WorkColUserMail);
HttpContext.Session.SetString(SessionKeyLang, LogonService.WorkColUserLanguage);
var SessionIdDate = HttpContext.Session.GetString(SessionKeyDate);
var SessionIdId = HttpContext.Session.GetString(SessionKeyId);
var SessionIdName = HttpContext.Session.GetString(SessionKeyName);
var SessionIdLang = HttpContext.Session.GetString(SessionKeyLang);
LogonService.WorkColUserSessionId = Strings.RTrim(SessionIdDate) + " " + Strings.RTrim(SessionIdName) + " " + Strings.RTrim(SessionIdId) + " " + Strings.RTrim(SessionIdLang);
//=========================================
// Change Culture on specific user language
//=========================================
if (LogonService.WorkColUserLanguage == "DE")
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("de");
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("de");
}
if (LogonService.WorkColUserLanguage == "EN")
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("en");
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("en");
}
if (!ModelState.IsValid)
{
return Page();
}
_context.Attach(LogonService).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!LogonServiceExists(LogonService.WorkColUserMail))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToPage("/LogonService/IndexLanguage");
}
private bool LogonServiceExists(string id)
{
return _context.LogonService.Any(e => e.WorkColUserMail == id);
}
}
}
感谢帮助
==================================================== ============================ 更新:25.1.2021:我进一步调查了为什么我的文化定义的价值观不起作用。我找到了原因。在我的 SwithCulturViewComponent.cs 中,文化在每次页面加载或退出时都会刷新到组合中语言中定义的文化(见图 1 或 2)
现在我试图通过设置会话中用户语言的文化信息来影响开关类。
=================== SwitchCultureViewComponet ===============
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using WorkCollaboration.Models;
namespace WorkCollaboration.ViewComponents
{
public class SwitchCultureViewComponent : ViewComponent
{
private readonly IOptions<RequestLocalizationOptions> localizationOptions;
public SwitchCultureViewComponent(IOptions<RequestLocalizationOptions> localizationOptions)
{
this.localizationOptions = localizationOptions;
}
public IViewComponentResult Invoke()
{
//=============================================
// Get Session Language
//=============================================
string SessionKeyName = "_Name";
string SessionKeyId = "_Id";
string SessionKeyDate = "_Date";
string SessionKeyLang = "_Lang";
var SessionIdDate = HttpContext.Session.GetString(SessionKeyDate);
var SessionIdId = HttpContext.Session.GetString(SessionKeyId);
var SessionIdName = HttpContext.Session.GetString(SessionKeyName);
var SessionIdLang = HttpContext.Session.GetString(SessionKeyLang);
if (SessionIdId == null)
{
SessionIdId = "0";
}
if (SessionIdId == "")
{
SessionIdId = "0";
}
if (SessionIdLang == null)
{
SessionIdLang = "DE";
}
if (SessionIdLang == "")
{
SessionIdLang = "DE";
}
//=========================================
// Change Culture on specific user language
//=========================================
if (SessionIdLang == "DE")
{
CultureInfo myCIintl = new CultureInfo("de-CH", false);
CultureInfo current = CultureInfo.CurrentCulture;
Console.WriteLine("The current culture is {0}", current.Name);
CultureInfo newCulture;
newCulture = new CultureInfo("de-CH");
CultureInfo.CurrentCulture = newCulture;
Console.WriteLine("The current culture is now {0}",
CultureInfo.CurrentCulture.Name);
CultureInfo.CurrentUICulture = new CultureInfo("de-CH", false);
// CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("de-CH");
Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name);
}
if (SessionIdLang == "DE")
{
CultureInfo myCIintl = new CultureInfo("en-US", false);
CultureInfo current = CultureInfo.CurrentCulture;
Console.WriteLine("The current culture is {0}", current.Name);
CultureInfo newCulture;
newCulture = new CultureInfo("en-US");
CultureInfo.CurrentCulture = newCulture;
Console.WriteLine("The current culture is now {0}",
CultureInfo.CurrentCulture.Name);
CultureInfo.CurrentUICulture = new CultureInfo("en-US", false);
// CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("de-CH");
Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name);
}
// How can I influence this code below. I only want this to be executed when I change the UI language code in the dropdown
// Otherwise it should use the custure Info from the above user languages
var cultureFeature = HttpContext.Features.Get<IRequestCultureFeature>();
var model = new SwitchCultureModel
{
SupportedCultures = localizationOptions.Value.SupportedUICultures.ToList(),
CurrentUICulture = cultureFeature.RequestCulture.UICulture
};
return View(model);
}
}
}
我如何影响下面的代码?我只希望在更改下拉菜单中的 UI 语言代码时执行此操作,否则它应该使用上述用户语言中的文化信息
每当我尝试在“var”中使用 CurrentUICuture ...
============ SwitchViewComponent.cs 中的部分 ==============
var model = new SwitchCultureModel
{
SupportedCultures =
localizationOptions.Value.SupportedUICultures.ToList(),
CurrentUICulture = cultureFeature.RequestCulture.UICulture
};
return View(model);
我得到错误。谢谢您的支持