zotero/scripts/locale/generate_sr_latn
Dan Stillman 4949f7459e
Some checks are pending
CI / Detect changes (push) Waiting to run
CI / Build, Upload (push) Waiting to run
CI / Test () (push) Blocked by required conditions
CI / Test (macOS NFS) (push) Blocked by required conditions
CI / Test (Windows arm64) (push) Blocked by required conditions
CI / Test (Windows x64) (push) Blocked by required conditions
CI / Utilities Tests (push) Waiting to run
Add Serbian (Latin) locale generated from Serbian (Cyrillic)
sr-Latn-RS is transliterated from sr-RS by
scripts/locale/generate_sr_latn, which runs as part of the locale
update scripts, with exceptions for foreign names that the Cyrillic
spells phonetically.

https://forums.zotero.org/discussion/113596/localization-serbian
2026-11-13 21:21:49 -05:00

114 lines
3.5 KiB
Python
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
#
# Generate the Serbian Latin locale (sr-Latn-RS) from the Cyrillic one (sr-RS)
#
# Serbian Cyrillic maps one-to-one to Serbian Latin, so every file in
# chrome/locale/sr-RS is copied with Cyrillic letters transliterated. Anything
# not in Cyrillic (keys, placeholders, markup, names already written in Latin)
# is left as is. Foreign names that the Cyrillic spells phonetically are
# restored to their original spelling via LATIN_NAMES.
import os
import re
import shutil
import sys
CYRILLIC_TO_LATIN = {
'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd', 'ђ': 'đ', 'е': 'e',
'ж': 'ž', 'з': 'z', 'и': 'i', 'ј': 'j', 'к': 'k', 'л': 'l', 'љ': 'lj',
'м': 'm', 'н': 'n', 'њ': 'nj', 'о': 'o', 'п': 'p', 'р': 'r', 'с': 's',
'т': 't', 'ћ': 'ć', 'у': 'u', 'ф': 'f', 'х': 'h', 'ц': 'c', 'ч': 'č',
'џ': 'dž', 'ш': 'š',
}
# Transliterated spelling -> original spelling. Serbian case endings after the
# name are kept (e.g., "Vordu" -> "Wordu").
LATIN_NAMES = {
'BibTeH': 'BibTeX',
'Dropboks': 'Dropbox',
'Eskejp': 'Escape',
'Gugl': 'Google',
'Javaskript': 'JavaScript',
'Kalibre': 'Calibre',
'Kičejn': 'Keychain',
'KORider': 'KOReader',
'Libreofis': 'LibreOffice',
'Mendeli': 'Mendeley',
'Šift': 'Shift',
'VebDAV': 'WebDAV',
'Vord': 'Word',
}
# Applied after LATIN_NAMES
LATIN_PHRASES = {
'Arhiv ID': 'arXiv ID',
'Krijejtiv komons': 'Creative Commons',
'Mendeley desktop': 'Mendeley Desktop',
'Van drajv': 'OneDrive',
}
LETTER = r'[^\W\d_]'
ACCEPT_LANGUAGES = 'intl.accept_languages=sr-Latn-RS, sr-Latn, sr, en-US, en'
def transliterate_char(text, i):
c = text[i]
lower = c.lower()
latin = CYRILLIC_TO_LATIN.get(lower)
if latin is None:
return c
if c == lower:
return latin
# Uppercase digraph letters (Љ, Њ, Џ) are all caps within an all-caps word
# (e.g., "ЉУБАВ" -> "LJUBAV") and title case otherwise ("Љубав" -> "Ljubav")
if len(latin) > 1:
neighbors = text[max(i - 1, 0):i] + text[i + 1:i + 2]
if any(n.isupper() for n in neighbors):
return latin.upper()
return latin[0].upper() + latin[1:]
return latin.upper()
def transliterate(text):
latin = ''.join(transliterate_char(text, i) for i in range(len(text)))
for transliterated, original in LATIN_NAMES.items():
latin = re.sub(
rf'(?<!{LETTER}){re.escape(transliterated)}(?={LETTER}{{0,3}}(?!{LETTER}))',
original,
latin
)
for transliterated, original in LATIN_PHRASES.items():
latin = latin.replace(transliterated, original)
return latin
def main():
script_dir = os.path.dirname(os.path.realpath(__file__))
root_dir = os.path.dirname(os.path.dirname(script_dir))
source_dir = os.path.join(root_dir, 'chrome', 'locale', 'sr-RS')
target_dir = os.path.join(root_dir, 'chrome', 'locale', 'sr-Latn-RS')
if not os.path.isdir(source_dir):
sys.stderr.write(f'{source_dir} not found\n')
return 1
shutil.rmtree(target_dir, ignore_errors=True)
for dirpath, dirnames, filenames in os.walk(source_dir):
out_dir = os.path.join(target_dir, os.path.relpath(dirpath, source_dir))
os.makedirs(out_dir, exist_ok=True)
for filename in filenames:
with open(os.path.join(dirpath, filename), encoding='utf-8') as f:
text = f.read()
text = transliterate(text)
if filename == 'intl.properties':
text = re.sub(r'^intl\.accept_languages=.*$', ACCEPT_LANGUAGES, text, flags=re.M)
with open(os.path.join(out_dir, filename), 'w', encoding='utf-8') as f:
f.write(text)
print(f'Generated {os.path.relpath(target_dir, root_dir)} from sr-RS')
return 0
if __name__ == '__main__':
sys.exit(main())