How can I set localization culture when executing background job? I want culture to be set by the users culture settings. I loaded user entity and settings but there setting "Abp.Localization.DefaultLanguageName" value is null. I have changed language so it should be different than default.
14 Answer(s)
-
0
Hi,
Could you try getting setting value for user in a using block like this;
using (_session.Use(userId, tenantId)) { // Get setting value here... }
See, <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Abp-Session#user-identifier">https://aspnetboilerplate.com/Pages/Doc ... identifier</a>.
-
0
Hi,
Using "using(_session.Use)" worked. I was able to get localized texts with "L('str')".
Thank you!
-
0
Great :)
-
0
Yes it works, if user has setting which tells what users preferred language is. But when background task is running, and there is no such setting (language is determined by other way) Session provides default language. So session setting will not work allways.
Is there a way, that I can get users language when creating background task, and set that language to be active from task parameters when background task is running?
-
0
Hi MikaTmlVertti ,
You can add a new property <UserLanguage> to the user entity and use that field for user's language.
-
0
<cite>alper: </cite> You can add a new property <UserLanguage> to the user entity and use that field for user's language.
okay, but where should I take the active localization value?
and at the backgroud job, how I can set localization to use that provided setting?
-
0
Let's clarify things. You only need to use ISettingManager to get a user's language. If the use has not logged into system, then you get the website default language.
var hostAdminLanguage = _settingManager.GetSettingValueForUser(LocalizationSettingNames.DefaultLanguage, tenantAdmin.TenantId, tenantAdmin.Id);
Apart from that, you can see How the Current Language is Determined for ASP.NET Core <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Localization#asp-net-core">https://aspnetboilerplate.com/Pages/Doc ... p-net-core</a>
or see this <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/b5262128cc33e0ae12327d3e5ad48579a9734c18/doc/WebSite/Localization.md#how-the-current-language-is-determined">https://github.com/aspnetboilerplate/as ... determined</a>
the GitHub issue about language/culture determination <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/2103#thread-subscription-status">https://github.com/aspnetboilerplate/as ... ion-status</a>
-
0
ยจ@alper thank you for your patience, i still need more info :D maybe i should clafiry my situation more.
I have action where a background job is created to send email with data. Entity is being created by known user, so I am able to get tenant id and user id for parameters.
At background job, I need to translate email by the user who hit the action. At background job, there is active user, so I tried to use: using (_session.Use(userId, tenantId)) { // Get setting value here... } That seemed to work for me when locally debuggin but now I did some more investigations.
I created this code to the background job:
var usersLanguage = _settingManager.GetSettingValueForUser(LocalizationSettingNames.DefaultLanguage, tenantId, userId); string text; using (_session.Use(tenantId, userId)) { text = L("Error"); } string result; if (text.Equals("Error!")) result = "Localization is english"; else if (text.Equals("VIRHE!")) result = "Localization is finnish, default"; else result = "This was not expected: " + text;
and I tried to run it with two users: User 1 => language is set to "fi" User 2 => language is set to "en"
With both users, usersLanguage was correct ("fi" and "en"), but for some reason, result was on both "Localization is finnish, default". So it seems that it was using default all the time. My debugging server has default fi, so I thought that this was working because I have fi set to be my user language. In our staging server, there default language was "en" so emails were allways translated with english language.
So to summarise,
using (_session.Use(tenantId, userId)) { text = L("Error"); }
is not working as expected when used in background job? Can you verify this?
-
0
I understand what you mean.
using (CultureInfoHelper.Use("en")) { var localizedText = _localizationManager.GetSource(AbpZeroTemplateConsts.LocalizationSourceName).GetString("Error"); }
PS: You need to replace "AbpZeroTemplateConsts" with your project name like "YourProjectConsts" ...
-
0
@alper
Your suggestion and
var usersLanguage = _settingManager.GetSettingValueForUser(LocalizationSettingNames.DefaultLanguage, args.TenantId, userId);
worked well but sometimes setting manager returns null.
I tryed to add
if (usersLanguage == null) usersLanguage = _settingManager.GetSettingValueForTenant(LocalizationSettingNames.DefaultLanguage, args.TenantId); if (usersLanguage == null) usersLanguage = _settingManager.GetSettingValueForApplication(LocalizationSettingNames.DefaultLanguage);
but these also return null..
How I can get default language, if these fail?
-
0
var usersLanguage = _settingManager.GetSettingValueForUser(LocalizationSettingNames.DefaultLanguage, args.TenantId, userId);
did not return null, it returned string "null" :D
-
0
Might be related to this <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/issues/1254">https://github.com/aspnetzero/aspnet-ze ... ssues/1254</a>. Could you check this comment <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/issues/1254#issuecomment-404817559">https://github.com/aspnetzero/aspnet-ze ... -404817559</a> ?
-
0
-
0
as a workaround update Abp.Localization.DefaultLanguageName=null or "en" in DB