javascript - 静态文件更新时, Angular 翻译重新加载静态文件?

标签 javascript angularjs angular-translate

我将 CKEditor 与 AngularJS 结合使用进行内联编辑(使用 contenteditable)。 enter image description here enter image description here enter image description here

在 CKeditor 模糊的情况下,$http 请求会发送到服务器并更新数据库,特别是刚刚修改的相关字符串的翻译键/值对。

在服务器上,数据库中的字符串更新后,会触发构建系统,并通过轮询数据库来重写 [lang].json(静态文件)。

如何告诉 Angular-translate 然后重新加载 [lang].json?

我看过很多页面讨论部分重新加载,但我没有找到任何有关静态文件重新加载的信息。

这是该指令的作用。

.directive('frontOfficeEdit', [

    // RESTIntranet IS AN ANGULAR FACTORY COMMUNICATING WITH THE REST API

    '$rootScope',
    '$parse',
    'RESTIntranet', 
    '$translate', 
    '$filter', 
    function (
        $rootScope, 
        $parse, 
        RESTIntranet, 
        $translate,
        $filter
    ) {

    var counter = 0,
    prefix = '__ckd_';


    return {
        restrict: 'A',
        link: function (scope, element, attrs, controller) {


            // editMode IS A BOOLEAN DECLARED INTO THE DIRECTIVE'S PARENT $scope.
            scope.$watch('editMode', function(newVal, oldVal) {


                if (!newVal) // IF editMode IS SET TO FALSE (OFF)
                {
                    // REMOVE contenteditable FROM ELEMENT
                    attrs.$set('contenteditable', false);

                    // DESTROY ALL CKEDITOR INSTANCES AS SOON AS USER TURNS OFF editMode IN THE GUI. 
                    // THERE CAN BE MULTIPLE CKEDITOR INSTANCES ON THE SAME PAGE, SO, TO MAKE SURE, DESTROY ALL.
                    for(name in CKEDITOR.instances)
                    {
                        CKEDITOR.instances[name].destroy();
                    }
                    return;
                }

                else // ELSE, editMode IS SET TO TRUE (ON)
                {
                    var getter = $parse(attrs.frontOfficeEdit), 
                        setter = getter.assign;

                    attrs.$set('contenteditable', true); // inline ckeditor needs this

                    if (!attrs.id) {
                        attrs.$set('id', prefix + (++counter));
                    }



                    var options = {};

                    // ON BLUR, SAVE CKEDITOR'S CONTENT TO REMOTE DB (REST API)
                    options.on = {
                        blur: function (e) {
                            if (e.editor.checkDirty()) {
                                var ckValue = e.editor.getData();

                                // TRANSLATE IT?
                                ckValue = $filter('translate')(ckValue);

                                scope.$apply(function () {
                                    console.log('--->@{369} ckValue is going to be inserted/updated in DB through service : ');
                                    console.log(ckValue);

                                    // CB FRAMEWORK INTRANET REST API CALL
                                    RESTIntranet.saveContent(ckValue, attrs.stringid, attrs.stringname);

                                    // UPDATE THE STRING IN STRINGS (JSON)
                                    current_language = $translate.use();
                                    curlang_caps = current_language.toUpperCase();
                                    stringname = attrs.stringname;


                                    // THIS ENSURES THAT SCOPE'S OBJECT IS SET TO THE PROPER VALUE, EVEN ONCE CKEDITOR ARE DESTROYED
                                    /*
                                    ————————— BUT THEN, IF I CHANGE FROM EN TO FR, AND THEN GO BACK TO FR, THE TEXT IS AS BEFORE
                                    */
                                    setter(scope, ckValue);


                                });
                                ckValue = null;
                                e.editor.resetDirty();
                            }
                        }
                    };

                    options.extraPlugins = 'sourcedialog,find';
                    options.removePlugins = 'sourcearea';                   

                    var editorangular = CKEDITOR.inline(element[0], options); //invoke

                    scope.$watch(attrs.frontOfficeEdit, function (value) {
                        // TRANSLATE IT?
                        value = $filter('translate')(value);

                        editorangular.setData(value);
                    });

                } // - EOF - else

            }, true); // - EOF - scope.$watch('editMode', function(newVal, oldVal) {

        }
        // - EOF - link
    }
    // - EOF - return
}]);

我没有足够的 JavaScript 经验来知道在哪里连接以及如何连接(对于异步内容还没有足够的经验)。谢谢。

最佳答案

我认为你只需要执行$translate.refresh()

https://github.com/angular-translate/angular-translate/blob/a2ac7000f94bcee6dd8e206cab5d7cec3527dd25/src/service/translate.js#L1584

   * @description
   * Refreshes a translation table pointed by the given langKey. If langKey is not specified,
   * the module will drop all existent translation tables and load new version of those which
   * are currently in use.
   *
   * Refresh means that the module will drop target translation table and try to load it again.
   *
   * In case there are no loaders registered the refresh() method will throw an Error.
   *
   * If the module is able to refresh translation tables refresh() method will broadcast
   * $translateRefreshStart and $translateRefreshEnd events.
   *
   * @example
   * // this will drop all currently existent translation tables and reload those which are
   * // currently in use
   * $translate.refresh();
   * // this will refresh a translation table for the en_US language
   * $translate.refresh('en_US');

关于javascript - 静态文件更新时, Angular 翻译重新加载静态文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29337300/

相关文章:

javascript - IE8 : element:first-child deosn't work (selects parent of what I actually want)

AngularJs 切换 ng-class ng-click

angularjs - Angular 插值、ng-bind 和 ng-translate 意外行为

angularjs - 使用 $translateProvider.useUrlLoader

angularjs - 使用angular-translate进行多元化的AngularJS翻译

javascript - 如何使用纯 javascript 为嵌套在带有 id 的 div 中的 a-tags 设置样式

javascript - jQuery 中的三元运算符未按预期工作

javascript - 数据表、点击(功能)和 on(提交)在第一页后不起作用

javascript - Angular UI 路由器无法在 Chrome 应用程序上运行

javascript - jquery的打印方法不在打印屏幕上显示颜色