2

我本地化了我的应用程序,它适用于标签、标题和错误消息。我在我的应用程序中使用了 jtable。但是按钮没有本地化。在 jtable.js 里面我有

messages: {
            serverCommunicationError: 'An error occured while communicating to the server.',
            loadingMessage: 'Loading records...',
            noDataAvailable: 'No data available!',
            areYouSure: 'Are you sure?',
            save: 'Save',
            saving: 'Saving',
            cancel: 'Cancel',
            error: 'Error',
            close: 'Close',
            cannotLoadOptionsFor: 'Can not load options for field {0}'
        }

但是,如果我选择俄语,我应该以俄语获取这些消息。为此,我尝试添加如下资源文件:

<script type="text/javascript">
(function ($) {
    $.widget("hik.jtable", {
        messages: {

            serverCommunicationError: '@SEFTool.Resources.Models.ValidationStrings.serverCommunication',
            loadingMessage: '@SEFTool.Resources.Models.ValidationStrings.loading',
            noDataAvailable: '@SEFTool.Resources.Models.ValidationStrings.noData',
            areYouSure: '@SEFTool.Resources.Models.ValidationStrings.Sure',
            save: '@SEFTool.Resources.Models.ValidationStrings.Save',
            saving: '@SEFTool.Resources.Models.ValidationStrings.Saving',
            cancel: '@SEFTool.Resources.Models.ValidationStrings.Cancel',
            error: '@SEFTool.Resources.Models.ValidationStrings.Error',
            close: '@SEFTool.Resources.Models.ValidationStrings.Close',
            cannotLoadOptionsFor: '@SEFTool.Resources.Models.ValidationStrings.cannotLoad'

        }
    });
});

但它不起作用..我如何将当前文化传递给 js 文件并在其中调用资源文件。

请帮助本地化按钮..

编辑:-

我创建了一个类

public static class MyGlobals
   {
       public static string serverCommunicationError;
.....
   }

我给的内部视图是这样的。

@SEFTool.Models.Administration.MyGlobals.serverCommunicationError= '@SEFTool.Resources.Models.ValidationStrings.serverCommunication'

我在 '@SEFTool.Resources.Models.ValidationStrings.serverCommunication' 中使用俄语。但是这个值没有被传递给 @SEFTool.Models.Administration.MyGlobals.serverCommunicationError.serverCommunicationError 显示为 null

编辑:-

在索引视图中我已经这样做了.....但是没有得到输出

  <script type="text/javascript">

(function (global) {
    var r = global.Resources || {};
    r.serverCommunicationError = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.serverCommunication)';
    r.loadingMessage = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.loading)';
    r.noDataAvailable = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.noData)';
    r.areYouSure = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.Sure)';
    r.save = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.Save)';
    r.saving = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.Saving)';
    r.cancel = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.Cancel)';
    r.error = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.Error)';
    r.close = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.Close)';
    r.cannotLoadOptionsFor = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.cannotLoad)';

    global.Resources = r;
})(this);

    $('#content_1').jtable({
        title: '',
        paging: true,
        pageSize: 10,
        sorting: true,
        inlineEditor: false,
        permissions: 'AED',
4

1 回答 1

0

In your view (.cshtml), set up the following script tag:

<script type="text/javascript">
    (function(global) {
        var r = global.Resources || {};
        r.serverCommunicationError = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.serverCommunication)';
        r.loadingMessage = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.loading)';
        r.noDataAvailable = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.noData)';
        r.areYouSure = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.Sure)';
        r.save = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.Save)';
        r.saving = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.Saving)';
        r.cancel = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.Cancel)';
        r.error = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.Error)';
        r.close = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.Close)';
        r.cannotLoadOptionsFor = '@Html.Raw(SEFTool.Resources.Models.ValidationStrings.cannotLoad)';

        global.Resources = r;
    })(this);
</script>

Then, in your script file, reference it like this:

messages: {
            serverCommunicationError: Resources.serverCommunicationError,
            loadingMessage: Resources.loadingMessage,
            noDataAvailable: Resources.noDataAvailable,
            areYouSure: Resources.areYouSure,
            save: Resources.save,
            saving: Resources.saving,
            cancel: Resources.cancel,
            error: Resources.error,
            close: Resources.close,
            cannotLoadOptionsFor: Resources.cannotLoadOptionsFor
        }

Just remember that the first script tag has to be called before the code in you js-file, so that the Resources object will be available. If, in your js-file, you wait for the DOM to be ready, this will be no problem though. Also, this is just the first solution that came to my mind. Maybe its not the best, and someone else knows a better approach. I would be interested too in different ways to solve this.

于 2013-03-01T07:06:31.317 回答