Search code examples
javascriptangularjsfile-uploadlocaleuploadcare

How to dynamically change locale in uploadcare widget in angularjs based project


I am using uploadcare widget in my angularjs-based application. I integrated this widget as angular directive.

Documentation said, that I should set global variable to change locale:

<script>
    UPLOADCARE_LOCALE = 'ru';
</script>

But I am not very familiar with angular. So, I have no idea how do it dynamically.

I'll be glad of any help.

Update


I tryed to add $rootScope to my uploadcare directive and set UPLOADCARE_LOCALE variable there:

angular.module("project").directive 'projectUploadcare', ($uploadcare, $rootScope) ->
   restrict: 'E'
   replace: true
   template: '''
                <span>
                   <span class="uploadcare-preview"></span>
                   <input type="hidden">
                </span>
             '''
   link: (scope, element, attrs) ->
      $rootScope.UPLOADCARE_LOCALE = 'ru'
      ... other code ...

but it does not help.


Solution

  • All global settings are read one time at page loading. Locale is global setting and can't be overwritten for particular widget. But according to source there is a hack which allows one to rebuild translation at runtime.

    First, you need to acquire internal Uploadcare API.

    var internalUploadcare;
    uploadcare.plugin(function(internal) {
      internalUploadcare = internal;
    });
    

    Then you can use internal internalUploadcare.locale.rebuild method to rebuild locale settings. Third step is reinitialize your existing widgets on the page. For example:

    $('#uploader').html(
      $('#uploader input:eq(0)')
    );
    uploadcare.initialize($('#uploader'));
    

    Add all together: http://jsbin.com/comone/1/watch