Comments (23)
You need to load the js file with the correct translate.
You check the current language and you get the specific language file.
Example on the top of you datatable page.
{
var requestCulture = Context.Features.Get();
var langFileUrl = string.Empty;
if (requestCulture.RequestCulture.UICulture.Name == "fr")
{
langFileUrl = "/lang/DataTable/fr.json";
}
else
{
langFileUrl = "/lang/DataTable/en.json";
}
}
And in the Datatable init you specify it.
var table = $('#@Model.Name').DataTable({
language: {
url: "@langFileUrl"
},
processing: true,
serverSide: true,
It not the most elegant way, but it work.
from jquerydatatablesserverside.
@jptoros many thanks for helping fellow developer. I'm happy that we can get working an example out here. Or may be we can make these as PR and add it to demo project. It would be a great addition. What say? Can you please give a PR for a demo project?
from jquerydatatablesserverside.
Hey @fingers10,
so here is what we need to do. Pretty easy. We import the the localizer and we add _localizer[column.Name].Value to replace everywhere you call column.Name
using Microsoft.AspNetCore.Mvc.Localization;
[HtmlTargetElement("jquery-datatablesTranslated", Attributes = "id,class,model")]
public class JqueryDataTablesTagHelper : TagHelper
{
**private readonly IHtmlLocalizer<JqueryDataTablesTagHelper> _localizer;
public JqueryDataTablesTagHelper(IHtmlLocalizer<JqueryDataTablesTagHelper> localizer)
{
_localizer = localizer;
}**
public string Id { get; set; }
public string Class { get; set; }
public object Model { get; set; }
[HtmlAttributeName("enable-searching")]
public bool EnableSearching { get; set; }
[HtmlAttributeName("thead-class")]
public string TheadClass { get; set; }
[HtmlAttributeName("search-row-th-class")]
public string SearchRowThClass { get; set; }
[HtmlAttributeName("search-input-class")]
public string SearchInputClass { get; set; }
[HtmlAttributeName("search-input-style")]
public string SearchInputStyle { get; set; }
[HtmlAttributeName("search-input-placeholder-prefix")]
public string SearchInputPlaceholderPrefix { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "table";
output.Attributes.Add("id", Id);
output.Attributes.Add("class", Class);
output.PreContent.SetHtmlContent($@"<thead class=""{TheadClass}"">");
var headerRow = new StringBuilder();
var searchRow = new StringBuilder();
headerRow.AppendLine("<tr>");
if (EnableSearching)
{
searchRow.AppendLine("<tr>");
}
var columns = GetColumnsFromModel(Model.GetType()).Where(c => !c.Exclude).OrderBy(c => c.Order);
foreach (var column in columns)
{
**headerRow.AppendLine($@"<th>{_localizer[column.Name].Value}</th>");**
if (!EnableSearching)
{
continue;
}
**searchRow.AppendLine($@"<th class=""{SearchRowThClass}""><span class=""sr-only"">{_localizer[column.Name].Value}</span>");**
if (column.HasSearch)
{
**searchRow.AppendLine($@"<input type=""search"" style=""{SearchInputStyle}"" class=""{SearchInputClass}"" placeholder=""{SearchInputPlaceholderPrefix} {_localizer[column.Name].Value}"" aria-label=""{_localizer[column.Name].Value}"" />");**
}
searchRow.AppendLine("</th>");
}
headerRow.AppendLine("</tr>");
if (EnableSearching)
{
searchRow.AppendLine("</tr>");
}
output.Content.SetHtmlContent($"{headerRow.ToString()}{searchRow.ToString()}");
output.PostContent.SetHtmlContent("</thead>");
}
from jquerydatatablesserverside.
Thanks @jptoros. I'll add this in the upcoming release.
from jquerydatatablesserverside.
@jptoros Added a push to master for this enhancement. This will be available from next release.
from jquerydatatablesserverside.
@jptoros please can you help with a HTML Localization sample?
from jquerydatatablesserverside.
Sure @jptoros . Will review and get back if I have any questions.
from jquerydatatablesserverside.
Hey jptoros,
Nice idea. I'll make sure to add this in the next release. If you have any ideas / PR. Your welcome to submit. Please add a star to my works if you think this deserves.
Thanks,
Abdul
from jquerydatatablesserverside.
@jptoros Right Now if you decorate the property with [DisplayName("Your Localized String")]
to the properties in your view model, it works. Can you check from your end to see of that is useful for you?
Anyways I'll be releasing a patch tonight to include [Display(Name = "Your Localized String")]
attribute as well.
Here is the order of precedence. The TagHelper will process the name with the below priority.
[Display(Name = "Your Localized String")]
[DisplayName("Your Localized String")]
- Your Property Name
from jquerydatatablesserverside.
No luck on my side.
Here is an example of the viewmodel.
public class CustomerViewModel
{
public int Id { get; set; }
[SearchableString]
[Sortable(Default = true)]
public string Company { get; set; }
[SearchableString]
[Sortable]
public string NickName { get; set; }
[SearchableString]
[Sortable]
[DisplayName("Phone")]
public string FullPhone { get; set; }
}
Note that we use the portable object localization
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/portable-object-localization?view=aspnetcore-3.0
Maybe it has a link to that ? Normally in our view we need to use @Html.DisplayFor or Localized["Phone"] for example and it is working.
from jquerydatatablesserverside.
Your using Asp.Net Core 2.x or 3.0? I tried with 2.x project and it worked for me. Try clearing Nuget Cache and check. I double checked form my end. Here is the link to demo project. Please check.
from jquerydatatablesserverside.
Sorry for the late reply, finally had time to check that error back and it still not working. The datable table display the 'DisplayName' attribute but if we use localization in the app to switch from english to french the translation it not applied.
In the demo project you don't have the option to switch language so we can't test it.
I will try to make you a demo project next week to display the problem
from jquerydatatablesserverside.
No luck on my side.
Here is an example of the viewmodel.
public class CustomerViewModel { public int Id { get; set; } [SearchableString] [Sortable(Default = true)] public string Company { get; set; } [SearchableString] [Sortable] public string NickName { get; set; } [SearchableString] [Sortable] [DisplayName("Phone")] public string FullPhone { get; set; } }Note that we use the portable object localization
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/portable-object-localization?view=aspnetcore-3.0Maybe it has a link to that ? Normally in our view we need to use @Html.DisplayFor or Localized["Phone"] for example and it is working.
@jptoros , I was looking at your implementation. I have a question here, in your above view model, did you try to pass the localized name in the [Display]
attribute? Something like below,
public class CustomerViewModel
{
public int Id { get; set; }
[SearchableString]
[Sortable(Default = true)]
public string Company { get; set; }
[SearchableString]
[Sortable]
public string NickName { get; set; }
[SearchableString]
[Sortable]
[Display(ResourceType = typeof(MyResources), Name = "Phone")]
public string FullPhone { get; set; }
}
I think the above one should resolve your issue as I'm reading the value coming from the [Display]
attribute. Did you give a try?
Instead of [DisplayName("Phone")]
try with [Display(ResourceType = typeof(MyResources), Name = "Phone")]
. I think this should be sufficient than changing the tag helper source code.
from jquerydatatablesserverside.
Hey @fingers10 ,
Two option to put ressource for localization in .net Core
-
Use RessourceFile from Microsoft.
Work with [Display(ResourceType = typeof(MyResources), Name = "Phone")] -
Use Portable Object Localization
Don't work with [Display(ResourceType = typeof(MyResources), Name = "Phone")] since the MyRessourcec won't exist since it use .po file instead of the normal Ressource File
More info on Portable Object Localization
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/portable-object-localization?view=aspnetcore-3.1
from jquerydatatablesserverside.
In this case adding IHtmlLocalizer<JqueryDataTablesTagHelper>
as a dependency inside the TagHelper will force the user of this package to register the localization service in their app even though they are not using it. If they fail do add then the app will throw runtime exception. Hence I'm thinking of alternative as this is a specific use case.
from jquerydatatablesserverside.
Hello @jptoros @fingers10
Can I get a demo of localization of new tag helper that you have added.
I have tried but getting error-
Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.Localization.IHtmlLocalizer`1[JqueryDataTables.ServerSide.AspNetCoreWeb.TagHelpers.JqueryDataTablesTagHelper]' while attempting to activate 'JqueryDataTables.ServerSide.AspNetCoreWeb.TagHelpers.JqueryDataTablesHtmlLocalizedTagHelper'.
from jquerydatatablesserverside.
@shrutigoyal123 if you're using portable object localization then please refer the following link - https://docs.microsoft.com/en-us/aspnet/core/fundamentals/portable-object-localization?view=aspnetcore-3.1#registering-the-service
Looks like you failed to add required services in the startup
from jquerydatatablesserverside.
@fingers10
I am using JSON file as Resource file and my startup.cs ss as below-
In case of JSON resource file. what I need to do?
from jquerydatatablesserverside.
Thank you for your support. It is working now. I had some implementation mistake at my end.
from jquerydatatablesserverside.
@shrutigoyal123 sorry for the delayed response and glad that you made it. If possible Please share the solution here so that it might help other developers in future.
from jquerydatatablesserverside.
@shrutigoyal123 let me know if you need help with anything.
from jquerydatatablesserverside.
@fingers10 Yes here I am sharing implementation with JSON Resource file-
1 . Add AddJsonLocalization and AddViewLocalization in StartUp.cs
-
Add [Display(Name = "")] in Model
Above is the implementation of column value changes in DataTable
@jptoros but how to change the language of Pagination and PageLength language like-
- Showing 1 to 4 of 4 entries
from jquerydatatablesserverside.
Sure ! With all the help that you provided to me during my dev here you go. Don't hesitate if you have any questions.
fingers10/JqueryDataTablesServerSideDemo#9
If you don't mind add me into the Contributions section of your GitHub project. Gonna be a first for me :)
from jquerydatatablesserverside.
Related Issues (20)
- Fetching Data on Clicking Row In Table
- How to remove the placeholder in column searching
- Buttons at footer using jquery datatables HOT 5
- Searching date is not working perfectly HOT 5
- Give an option to apply grouping
- Seems to be very slow HOT 1
- [FEATURE] Add SearchPanes support HOT 1
- Is there a way to use this in a Blazor App HOT 1
- Localization HOT 6
- SelectList
- SearchOptionsProcessor.Apply not correctly handling more than one "multiple term" search term HOT 2
- [FEATURE] Support for complex table headers with the tag helper
- Add support for "Like" operations
- Localization .resx
- How to add multi-select checkboxes to table? HOT 1
- [BUG] Invalid Operator HOT 2
- [QUESTION] DateTime Searching problem HOT 2
- [QUESTION] Searching Enum value as string instead of short/int HOT 2
- [QUESTION] How To localizate the name of Coloumns
- [QUESTION] How use IncludeInReport on Dictionary HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jquerydatatablesserverside.