Base solution for your next web application
Open Closed

Enums or consts? Localization? #914


User avatar
0
easyest created

Following the example in documentation, lets say my Personentity has Statusproperty, that can be one of Active, _Expired_or Blocked. First thing that comes into my head is to put that to enum and use Displayattribute. But then I realize, that in Angular I do not use html helpers and even if I would, I have no idea, how to translate those display names. On the other hand, constant string usage in Asp.Net Zero for me seems better than enums, as this way makes my database values look much better. But then there is a question - should I translate those constants in application service? In angular view? Any other place? So, the question - what way would You suggest and how this status should be translated after all?

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

8 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Exposing enums to client is in the roadmap of ABP but it doesn't have a determined release date yet.

    For now, you can define your enums on the client like this. (This code is taken from <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/pull/378">https://github.com/aspnetboilerplate/as ... e/pull/378</a>, thakns to jefftindall :))

    abp.constant.status = {
        notStarted: 0,
        inProgress: 10,
        draft: 20,
        cancelled: 90,
        completed: 100,
        values: [
            { name: 'NotStarted', value: 0, localizedName: 'Enum_Status_NotStarted' },
            { name: 'InProgress', value: 10, localizedName: 'Enum_Status_InProgress' },
            { name: 'Draft', value: 20, localizedName: 'Enum_Status_Draft' },
            { name: 'Cancelled', value: 90, localizedName: 'Enum_Status_Cancelled' },
            { name: 'Completed', value: 100, localizedName: 'Enum_Status_Completed' }]
    };
    

    Then in the angular grid, you can find localized name using underscore like this,

    var localizedStatusName = _.where(abp.constant.status.values, { value:0 }).localizedName;
    

    I hope this helps.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    easyest created

    And if I am using constants? Put code into constant and use javascript translation? Translate in .Select from IQueryable in application service? Create some kind of provider, as in feature provider?

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    For the constants, i would do the localization on the client as well. Lets assume you have consts for status like 'NotStarted', 'InProgress' etc.

    Add localization with key 'Const_Status_InProgress', and add a function to localize in helper.js like

    app.localizeStatus = function (status) {
           return app.localize('Const_Status_' + status);
    };
    

    use app.localizeStatus in the client.

    I hope this works for you.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    thor created

    Hi,

    For my project I have lots of requirements for users to capture data based on predetermined drop downs in forms.

    Not sure what the most elegant approach would be (including localization). I would appreciate any tips or tricks from people with experience in implementing this in Abp.

    Any update on this old post?

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @thor,

    AspNet Zero and ABP Framework doesn't offer a build in way. Have you tried my previous suggestion in this topic ?

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    thor created

    No, I have not tried it yet. But, I did read your suggested way forward as posted in early 2016. I was just hoping there was now a more "elegant" way to do it.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    ismcagdas created
    Support Team

    @thor got it :). Unfortunately we haven't worked on this, sorry.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    thor created

    No worries ;)

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)