Coverage for portality/lib/isolang.py: 78%

9 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-19 18:38 +0100

1import pycountry 

2 

3 

4def find(lang): 

5 """" Look up a language using any of its attributes """ 

6 try: 

7 return as_dict(pycountry.languages.lookup(lang)) 

8 except LookupError: 

9 return {} 

10 

11 

12def as_dict(language_object): 

13 """ Get a dict representing a language """ 

14 # Previously we had a list of ISO_639_2b (bibliographic) languages in datasets.py represented e.g. 

15 # [u"wel", u"cym", u"cy", u"Welsh", u"gallois"] - now we have pycountry, so we favour the bibliographic name when 

16 # available. French is no longer present. 

17 

18 # Convert to a dict in pycountry's representation 

19 language_dict = vars(language_object)['_fields'] 

20 

21 return { 

22 "alpha3": language_dict.get('bibliographic', language_dict.get('alpha_3', '')), 

23 "alt3": language_dict.get('alpha_3', ''), 

24 "alpha2": language_dict.get('alpha_2', ''), 

25 "name": language_dict.get('name', ''), 

26 "fr": '' 

27 }