Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs:-How to generate arabic url in angularjs

I am implementing an arabic site in which I want the url to be generated in arabic Fro eg:-program/يه تشامبيونز ليغ - مباشر

I have implemented the same for english in angularjs and ruby.SO the url is generating for english language

On click event I am calling a filter and the filter is helping me to deal with special characters.

The following 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 am trying to implement for arabic url it is giving me a strange output:-

For eg for programs I am getting-

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

So what should I do to generate the output ie url in arabic.Is there anything I can do to implement it in the filter


1 Answers

Try this solution:

replace the method "encodePath" in angular.js to this:

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 the 2 answers from: this answer and the answer from "tonman-neverwalk-alone"

like image 73
user2903753 Avatar answered Aug 08 '26 10:08

user2903753



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!