Skip to content
PN Scripts

Development

Latin letters hidden in Cyrillic words, and Bulgarian quotation marks: finding and fixing them

  • PN Scripts
  • 8 min read

The search on a shop finds no "Поръчка", although the word is on the page. A translation key sorts in the wrong place. The spell checker underlines a word that looks perfect. In each case the word is not what it looks like: one of its letters is Latin. Bulgarian has six lower-case letters that are drawn exactly like Latin ones (а, е, о, р, с, у, plus х and к in most fonts), and anyone who switches between a Bulgarian and an English keyboard layout types a Latin "o" into a Cyrillic word sooner or later. Copy and paste spreads it from there. The same texts usually also carry "straight" quotes, a hyphen where a dash belongs and "3км" without a space. This guide shows how to find mixed words with one command, how to fix them in code, and which quotes, dashes and hyphens Bulgarian text takes. The JavaScript was run on Node 22 and the command with GNU grep, as printed.

#Why one hidden letter matters

To a computer, "Пoръчка" with a Latin o (U+006F) and "Поръчка" with a Cyrillic о (U+043E) are different words. Search, sorting, spell checking, hyphenation and screen readers all work on the letters, not on their shapes, so the mixed word is not found, sorts in odd places, gets underlined and may be read out letter by letter. Unicode has a name for such letters, confusables, and its security reports describe mixed-script words as a warning sign, because the same trick is used to make a fake domain look like a real one. In ordinary copy it is usually just a keyboard slip, but it costs as much: a product nobody finds through the site search is a product nobody buys.

The reverse happens too. "Mаil" with a Cyrillic а in an English button looks right and fails just the same.

#Finding mixed words with one command

A word that contains both scripts is almost never right, which makes it easy to search for. GNU grep's Perl mode knows the Unicode scripts:

grep -rnoP '[\p{L}]*(\p{Cyrillic}\p{L}*\p{Latin}|\p{Latin}\p{L}*\p{Cyrillic})[\p{L}]*' lang

On a Laravel lang file with two planted mistakes it printed lang/bg/shop.php:4:Пoръчка and lang/bg/shop.php:5:Mаil, and left the correct line alone. Run it on your translations, Markdown articles and templates; on a large site the first run rarely finds nothing. If grep complains about the locale, set a UTF-8 one first, for example with LC_ALL=C.UTF-8 in front of the command.

#Fixing them in code

The fix is to put the word into one script. Which script is a simple vote: the script with more letters in the word wins, and only letters that have a lookalike in the other script can be swapped. A letter without a twin, such as a Latin i in a Bulgarian word, needs a person.

// Latin letters that look like Cyrillic ones, and back.
const TO_CYR = { a: "а", c: "с", e: "е", o: "о", p: "р", x: "х", y: "у", k: "к",
  A: "А", B: "В", C: "С", E: "Е", H: "Н", K: "К", M: "М", O: "О", P: "Р", T: "Т", X: "Х", Y: "У" };
const TO_LAT = Object.fromEntries(Object.entries(TO_CYR).map(([l, c]) => [c, l]));
const CYR = /\p{Script=Cyrillic}/u;
const LAT = /\p{Script=Latin}/u;

export function fixWord(word) {
  const chars = [...word];
  const cyr = chars.filter((c) => CYR.test(c)).length;
  const lat = chars.filter((c) => LAT.test(c)).length;
  if (!cyr || !lat) return word;                      // one script: nothing to do
  const map = cyr >= lat ? TO_CYR : TO_LAT;           // the majority decides
  const fixed = chars.map((c) => map[c] ?? c).join("");
  return [...fixed].some((c) => CYR.test(c)) && [...fixed].some((c) => LAT.test(c)) ? null : fixed;
}

export function fixText(text) {
  return text
    .replace(/[\p{L}\p{M}]+/gu, (w) => fixWord(w) ?? w)
    .replace(/"([^"\n]*\p{Script=Cyrillic}[^"\n]*)"/gu, (_, quoted) => `„${quoted}“`);
}

for (const s of ["Пoръчка № 1024", "Mаil адрес", "Пиши ни на \"Контакти\"", "Пoртфолиo"]) {
  const word = s.split(" ")[0];
  console.log(JSON.stringify(s), "=>", JSON.stringify(fixText(s)),
    [...word].map((c) => c.codePointAt(0).toString(16).padStart(4, "0")).join(" "));
}

It turns "Пoръчка № 1024" into "Поръчка № 1024" and shows the culprit in the code points of the first word: 041f 006f 0440, a Latin 006f between two Cyrillic letters. "Mаil адрес" becomes "Mail адрес", because the word has three Latin letters and one Cyrillic. "Пoртфолиo", with two Latin letters, is fixed in one pass, and the straight quotes around "Контакти" become „Контакти“. When fixWord() returns null, the word cannot be fixed by swapping and should be listed for a person to look at.

Run a fix like this on text, not on code: a variable or a CSS class with a Cyrillic letter in it is a separate bug, and the replacement belongs in the place where the text is written, not only in the output.

#Quotes, dashes and hyphens

  • Quotation marks. Bulgarian text takes „…“: a low opening mark (U+201E) and a high closing one (U+201C), which is also what the Unicode CLDR data gives for Bulgarian. "Straight" quotes and English “curly” ones both look foreign in Bulgarian copy. « » appear in some printed books; pick one style and keep it.
  • The dash between words. A dash that separates parts of a sentence is written with a space on each side: "Цената е важна – от нея зависи всичко." A hyphen with spaces around it ("важна - от нея") is a typing shortcut, not a dash.
  • The hyphen inside words. The comparative and superlative of adjectives and adverbs are written with a hyphen and no spaces: по-добър, най-важното, по-бързо. The Institute for Bulgarian Language's advice adds the other half of the rule: with nouns, verbs and some phrases по and най are written apart, and по takes an accent, as in "пò ми се иска". So "по - добър" with spaces is a broken hyphen, and the right fix is "по-добър", not a dash.
  • Numbers and units. A space goes between a number and its unit or abbreviation: 2026 г., 3 км, 15 ч. A no-break space keeps them on one line.
  • Punctuation. No space before a comma, a full stop, a colon or a question mark, one space after them, and an ellipsis (…) instead of three dots if your style uses it.

#What to leave alone

Automatic fixes need limits. Number ranges such as 10-20, dates, phone numbers and product codes keep their hyphens. Quotes inside program code are syntax, and an escaped \" in a string has to stay as it is. Keys in a lang file, URLs, CSS classes and HTML attributes that nobody reads are code, while alt, title, placeholder and aria-label are text that people do read and hear. A line full of ы and э is probably Russian on purpose; one ы in a Bulgarian line is a keyboard slip.

#An editor extension that does this as you type

We made Kavichka, a Visual Studio Code extension for Bulgarian text in Markdown, lang files, Blade, Vue, JSON and plain text. It underlines Latin letters hidden in Cyrillic words and the reverse, stray Russian or Ukrainian letters, straight quotes, a hyphen used as a dash, missing spaces before units, a space before a comma and more, offers a quick fix for each, and fixes a whole file in one undo step. In code it looks only inside strings and comments, in templates only at the text and the attributes people read, and number ranges are left alone. It also transliterates Bulgarian to Latin and makes URL slugs. It was tested with an automated suite and inside VS Code 1.141 on Linux. It also follows the rule above for по and най: "по - добър" and "най - важното" become "по-добър" and "най-важното", with a hyphen and no spaces, not a dash. It is listed as Coming soon.

#Frequently asked questions

Why does the site search not find a word that is on the page?

Often because one letter of the word on the page is Latin. It looks the same but it is a different character, so the word does not match what people type. Search the source for words that mix scripts.

Which quotation marks does Bulgarian use?

„…“, with the opening mark low and the closing mark high. They are the marks the Unicode CLDR data lists for Bulgarian, and the ones most Bulgarian sites and publishers use.

Is it по-добър or по добър?

По-добър, with a hyphen and no spaces, as for every comparative and superlative of an adjective or adverb. По and най are written apart, with an accent on по, only before nouns, verbs and in some phrases.

Why does VS Code draw boxes around Cyrillic letters?

VS Code marks characters that can be confused with others, such as Cyrillic е and о inside code strings. That is a different, broader check; the setting editor.unicodeHighlight.ambiguousCharacters switches it off if a tool that reports only real mixed words does the job.

#Sources

Keep reading

Keep reading

Comments

Comments

No comments yet. Ask a question or share what worked for you.

Leave a comment

shown with your comment; a nickname is fine
optional; only for reply notifications, never shown
Plain text with **bold**, *italic*, `code`, > quotes and links.

Next step

pnscripts.com/contact

Talk to the team

Ask about an article, or tell us about a project you want built. We reply within one business day.

Write to us

The PN Scripts family

Other PN Scripts sites

Hosting, games and the blog each have their own site, run by the same company.

  • pnscripts.com

    PN Scripts

    Software engineering

    Custom web, mobile and API development, plus our open-source products and plugins.

  • games.pnscripts.com

    Games

    Games and game servers

    The home for PN Scripts games: free browser games you can play right now, with game servers coming soon.

  • hosting.pnscripts.com

    Hosting

    Hosting and infrastructure

    Shared hosting, KVM VPS, dedicated servers and domains, from the same company that builds your project.

  • blog.pnscripts.com

    Blog

    Articles and field notes

    Practical articles on software development, hosting, open source and games, written with AI assistance and checked by the people who build them.

    You are here