Hussein Farg
Hussein Farg

Reputation: 21

Why does this script not accept any Arabic input?

The code below is a code for the Blogger platform

but the problem with it is that it does not accept any Arabic input

but when converting text via this tool https://www.url-encode-decode.com/ works well please help

var section = {
  category: null,
  "max-results": 6,
  template: {
    default:
      '<div class="card card-masonry col-3-lg col-4-md col-6-sm"><div class="card__content"><div class="card__header"><a class="card__image flex-none" href="URL"><img src="IMG" /></a></div><div class="card__data"><h2 class="card__title card__space"><a href="URL">TITLE</a></h2><div class="card__meta display-flex justify-content-between"><time class="card__space meta__time flx-yc"><svg class="feather feather-clock" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><circle cx="12" cy="12" r="10"></circle><polyline points="12 6 12 12 16 14"></polyline></svg>TIME</time><a class="card__space meta__more flx-yc" href="URL" title="قراءة المزيد"><svg class="feather feather-arrow-left" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><line x1="19" x2="5" y1="12" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg></a></div></div></div></div>',
    empty: '<p class="alert">لا توجد مقالات</p>',
  },
};
function forEach(items, callback) {
  var i, item;
  for (i = 0; (item = items[i++]); ) if (false === callback.call(item, i, item)) break;
  return items;
}
function getCurrentData(element) {
  var name, value;
  var obj = {};
  for (name in section) {
    value = element.getAttribute("data-" + name);
    if (value) obj[name] = value;
  }
  return obj;
}
function temp(content, data) {
  var name;
  for (name in data) content = content.replace(new RegExp(name, "g"), data[name]);
  return content;
}
function getLabel(element, data) {
  var name;
  var category = data.category;
  var script = document.createElement("script");
  var src =
    defaults.homeUrl +
    "/feeds/posts/default?alt=json-in-script&callback=callbacks." +
    category.replace(/[^A-Z0-9]/gi, "");
  callbacks[category.replace(/[^A-Z0-9]/gi, "")] = function (json) {
    var i, post;
    var html = "";
    var entry = json.feed.entry;
    if (entry)
      for (i = 0; (post = entry[i++]); ) {
        var content = post.content;
        var summary = post.summary;
        var body = content ? content.$t : summary.$t;
        var img = post.media$thumbnail;
        var tempHtml = document.createElement("div");
        tempHtml.innerHTML = body;
        var imgHtml = tempHtml.querySelector("img");
        html += temp(data.template, {
          IMG: (img ? img.url : imgHtml ? imgHtml.src : defaults.image).replace(/s\B\d{2,4}-c/, defaults.imgSize),
          TITLE: post.title.$t,
          TIME: new Date(post.published.$t).toLocaleDateString("ar-EG", { month: "long", day: "2-digit" }),
          SNIPPET: body.replace(/<[^>]*>?/g, "").substring(0, defaults.snippet) + "...",
          URL: function () {
            var i, link;
            for (i = 0; (link = post.link[i++]); ) if (link.rel === "alternate") return link.href;
          },
        });
      }
    else html += section.template.empty;
    element.innerHTML = html;
  };
  for (name in data) if (name !== "template") src += "&" + name + "=" + data[name];
  script.src = src;
  document.body.appendChild(script);
}
forEach(document.querySelectorAll(defaults.className), function (index, element) {
  var currentData = getCurrentData(element);
  if (currentData.category) {
    if (!currentData["max-results"]) currentData["max-results"] = section["max-results"];
    currentData.template = section.template["default"];
    getLabel(element, currentData);
  }
});

When adding the section name in this way it does not work

tag: "قسم 5", results: 10

But when added this way it works fine

tag: "%D9%82%D8%B3%D9%85+5", results: 10

I want to add the department name in Arabic without the need to convert it

How can I solve this problem?

Or will one of you solve it?

Upvotes: 2

Views: 120

Answers (1)

MR Mark II
MR Mark II

Reputation: 453

One of the function of the url encode web you use:

  • Convert the character string into a sequence of bytes using the UTF-8 encoding

UTF-8 can store the full Unicode range, so it's fine to use for Arabic. Maybe you dont use utf-8 in your project.

Edit:

Instead of:

<p class="alert">لا توجد مقالات</p>

try:

'<p class="alert"><?php echo 'لا توجد مقالات'; ?></p>'

Upvotes: 2

Related Questions