Angularjs: -How to create arabic url in angularjs

I am implementing an Arabic site where I want the url to be generated in Arabic Fro, for example: - program/يه تشامبيونز ليغ - مباشر

I have implemented the same for English in angularjs and ruby.SO url generates for English

On the click event, I call the filter and the filter helps me handle special characters.

Below is the code I have implemented in the filter: -

woi.filter('encodeUrl',['$routeParams','$rootScope',function($routeParams,$rootScope){
    return function(value){
        if(value != undefined)
        {
            return value.replace(/\$\#\*\!/g, 'CeNc').replace(/\"/g, 'DqO').replace(/\+/g, 'PLus').replace(/\[/g, 'ObR').replace(/\]/g, 'CbR').replace(/\@/g, 'AtR').replace(/\&/g, 'EmPe').replace(/\#/g, 'HaSh').replace(/\*/g, 'StAr').replace(/\$/g, "DoLr").replace(/\-/g, "~").replace(/\s/g, "-").replace(/\//g, "$").replace(/\?/g, '*').replace(/\%/g, 'PeRc');
        }
        else
        {
            return ""
        }
    }
}]);

      

But when I try to implement Arabic url it gives me strange output: -

For example, for the programs I receive -

www.example.com/#!/program/%D8%A8%D9%8A%D8%BA-%D8%A8%D9%88%D8%B3

      

So what should I do to generate the result i.e. url in arabic.Is what can i do to implement it in filter

+3


source to share


3 answers


Try this solution:

replace the "encodePath" method in angular.js with the following:



function toUTF(str) {       
    var b64 = window.btoa(unescape(encodeURIComponent(str)));
    var str2 = decodeURIComponent(escape(window.atob(b64)));
    return str2;
}
function encodePath(path) {
    var segments = path.split('/'), i = segments.length;
    while (i--) { segments[i] = toUTF(segments[i]); }
    return segments.join('/');
}

      

I mixed 2 answers from: this answer and the answer from "tonman-neverwalk-alone"

+1


source


I would like to say that we have the same problem with Thais, but in fact we should not change the angular source code to solve this problem, because AngularJS must always encode the Uri when starting routing if you want to change this thing. you have to change angular source code in line.

function encodePath(path) {
    var segments = path.split('/'),
        i = segments.length;

    while (i--) {
        segments[i] = encodeUriSegment(segments[i]);
    }

    return segments.join('/');
}

      

For this:

function encodePath(path) {   
    return path;
}

      



for Firefox / Safari support

function encodePath(path) {
    var isFirefox = typeof InstallTrigger !== 'undefined';   // Firefox 1.0+
    var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
    if (isFirefox || isSafari) {
        var segments = path.split('/'),
            i = segments.length;
        while (i--) {
            segments[i] = encodeUriSegment(segments[i]);
        }
    } else {
        return path;
    }    
}
      

Run codeHide result


0


source


You can use the following package, which is written for Persian, but it can also work in Arabic: Persian.js

0


source







All Articles