locale-simple 0.100__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,40 @@
1
+ Metadata-Version: 2.4
2
+ Name: locale-simple
3
+ Version: 0.100
4
+ Summary: Translation system based on gettext, same API in Perl, Python and JavaScript
5
+ Author-email: Torsten Raudssus <torsten@raudssus.de>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/Getty/locale-simple
8
+ Project-URL: Repository, https://github.com/Getty/locale-simple
9
+ Project-URL: Issues, https://github.com/Getty/locale-simple/issues
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.8
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Topic :: Software Development :: Localization
20
+ Requires-Python: >=3.8
21
+ Description-Content-Type: text/markdown
22
+
23
+ # Description
24
+ This is a small wrapper around Gettext functionality that integrates sprintf and makes it a bit more easy to setup the internationalization. It ONLY supports UTF8 data, and in or output, that is a fixed setup (and always will be).
25
+
26
+ # Usage
27
+
28
+ Usage of this module is very similar to that of [Locale::Simple](https://metacpan.org/module/Locale::Simple):
29
+
30
+ from locale_simple import *
31
+
32
+ # Set the locale dir
33
+ l_dir('data/locale')
34
+ # Set the default domain
35
+ ltd('test')
36
+ # Set the default language
37
+ l_lang('de_DE')
38
+
39
+ print l("Hello") # Hallo
40
+ print ln("You have %d message","You have %d messages",1) # Du hast 1 Nachricht
@@ -0,0 +1,5 @@
1
+ locale_simple.py,sha256=6IuoFuvFD67qqNDp6NE-tXDOHa7paQX58m9gni9QRFg,3785
2
+ locale_simple-0.100.dist-info/METADATA,sha256=9x3GJFRyyFmbAOQB83ETF0i7cwpeMkaqbcMQ91kpZnw,1632
3
+ locale_simple-0.100.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
4
+ locale_simple-0.100.dist-info/top_level.txt,sha256=mLZ7r5wzAM7fzmhH1TnIqfCv1TydGTjyks0niNx55n0,14
5
+ locale_simple-0.100.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ locale_simple
locale_simple.py ADDED
@@ -0,0 +1,125 @@
1
+ from gettext import textdomain, dngettext, ngettext, bindtextdomain, dgettext, gettext
2
+ try:
3
+ from gettext import bind_textdomain_codeset
4
+ except ImportError:
5
+ bind_textdomain_codeset = None
6
+ import os, sys, re
7
+
8
+ __version__ = "0.100"
9
+
10
+ __all__ = [
11
+ 'l_nolocales',
12
+ 'l_dry',
13
+ 'l_dir',
14
+ 'l_lang',
15
+ 'ltd',
16
+ 'l',
17
+ 'ln',
18
+ 'lp',
19
+ 'lnp',
20
+ 'ld',
21
+ 'ldn',
22
+ 'ldp',
23
+ 'ldnp',
24
+ ]
25
+
26
+ global dry, nowrite, nolocales, dir
27
+ dry, nowrite, nolocales, dir = None, None, None, None
28
+ tds = {}
29
+
30
+ def l_nolocales(x):
31
+ global nolocales
32
+ nolocales = x
33
+ def l_dry(d, n=False):
34
+ global dry, nowrite, nolocales
35
+ dry, nowrite, nolocales = d, n, bool(d)
36
+ def l_dir(d):
37
+ global dir
38
+ if not os.path.isdir(d): raise OSError("%s: No such file or directory" % d)
39
+ dir = d
40
+
41
+ def _die(msg):
42
+ sys.stderr.write(msg + '\n')
43
+ exit(1)
44
+
45
+ def gettext_escape(content):
46
+ content = content.replace('\n', '\\n')
47
+ content = content.replace('"', '\"')
48
+ return content
49
+
50
+ def l_lang(primary):
51
+ for var in 'LANG', 'LANGUAGE', 'LC_ALL':
52
+ os.environ.update({var: primary})
53
+
54
+ def wd(data):
55
+ if nowrite: return
56
+ f = open(dry, 'a')
57
+ f.write('\n'.join(data) + "\n\n")
58
+ f.close()
59
+
60
+ def sprintf(string, *args):
61
+ args = list(args)
62
+ for m in re.finditer(r'%(\d+)\$([scboxXuidfegEG])', string):
63
+ substr = m.string[m.start():m.end()]
64
+ groups = m.groups()
65
+ string = string.replace(substr, ('%'+groups[1]) % args[int(groups[0])-1])
66
+ count = len(re.findall(r'%%|%(?:\d+\$)?(?:[-+\'#0 ]*)(?:\*\d+\$|\*|\d+)?(\.(?:\*\d+\$|\*|\d+))?(?:[scboxXuidfegEG])', string))
67
+ args = tuple(args[:count])
68
+ return string % args
69
+
70
+ def l(id, *args):
71
+ return ldnp('', None, id, None, None, *args)
72
+ def ln(id, idp, n, *args):
73
+ return ldnp('', None, id, idp, n, *args)
74
+ def lp(ctxt, id, *args):
75
+ return ldnp('', ctxt, id, None, None, *args)
76
+ def lnp(ctxt, id, idp, n, *args):
77
+ return ldnp('', ctxt, id, idp, n, *args)
78
+ def ld(td, id, *args):
79
+ return ldnp(td, None, id, None, None, *args)
80
+ def ldn(td, id, idp, n, *args):
81
+ return ldnp(td, None, id, idp, n, *args)
82
+ def ldp(td, ctxt, id, *args):
83
+ return ldnp(td, ctxt, id, None, None, *args)
84
+
85
+ def ldnp(td, ctxt, id, idp, n, *args):
86
+ if not len(id): return id
87
+ if not dir and not nolocales:
88
+ _die("please set a locale directory with l_dir() before using other translate functions")
89
+
90
+ if idp is not None: args = (n,) + args
91
+ is_plural = idp is not None
92
+ out = None
93
+ if dry:
94
+ if not nowrite:
95
+ save = []
96
+ if td: save.append(' # domain: '+td)
97
+ if ctxt: save.append('msgctxt: "'+gettext_escape(ctxt)+'"')
98
+ save.append('msgid "'+gettext_escape(id)+'"')
99
+ if idp: save.append('msgid_plural "'+gettext_escape(idp)+'"')
100
+ wd(save)
101
+ out = (idp if is_plural and n != 1 else id) % args
102
+ else:
103
+ key = (ctxt + '\x04' + id) if ctxt else id
104
+ keyp = (ctxt + '\x04' + idp) if ctxt and idp else idp
105
+ if td and is_plural: raw = dngettext(td, key, keyp, n)
106
+ elif td and not is_plural: raw = dgettext(td, key)
107
+ elif is_plural: raw = ngettext(key, keyp, n)
108
+ else: raw = gettext(key)
109
+
110
+ # gettext returns the input string untouched when no translation
111
+ # exists, so for a pgettext-style lookup we have to strip the
112
+ # "ctxt\x04" prefix ourselves to fall back to the plain msgid.
113
+ if ctxt and raw.startswith(ctxt + '\x04'):
114
+ raw = raw[len(ctxt) + 1:]
115
+
116
+ out = sprintf(raw, *args)
117
+ return out
118
+
119
+ def ltd(td):
120
+ if td not in tds:
121
+ bindtextdomain(td, dir)
122
+ if bind_textdomain_codeset:
123
+ bind_textdomain_codeset(td, 'utf-8')
124
+ tds[td] = 1
125
+ textdomain(td)