mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-22 10:42:34 +01:00
new module for simple language code to name mapping
This commit is contained in:
parent
6dcfed16fc
commit
b24bb1da7d
41
gallery_dl/iso639_1.py
Normal file
41
gallery_dl/iso639_1.py
Normal file
@ -0,0 +1,41 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2015 Mike Fährmann
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License version 2 as
|
||||
# published by the Free Software Foundation.
|
||||
|
||||
"""Conversion between language names and ISO 639-1 codes"""
|
||||
|
||||
def code_to_language(code, default="English"):
|
||||
"""Map an ISO 639-1 language code to its actual name"""
|
||||
return codes.get(code, default)
|
||||
|
||||
def language_to_code(lang, default="en"):
|
||||
"""Map a language name to its ISO 639-1 code"""
|
||||
for code, language in codes.items():
|
||||
if language == lang:
|
||||
return code
|
||||
return default
|
||||
|
||||
codes = {
|
||||
"cs": "Czech",
|
||||
"da": "Danish",
|
||||
"de": "German",
|
||||
"en": "English",
|
||||
"es": "Spanish",
|
||||
"fi": "Finnish",
|
||||
"fr": "French",
|
||||
"it": "Italian",
|
||||
"jp": "Japanese",
|
||||
"ko": "Korean",
|
||||
"nl": "Dutch",
|
||||
"no": "Norwegian",
|
||||
"pl": "Polish",
|
||||
"pt": "Portuguese",
|
||||
"ru": "Russian",
|
||||
"sv": "Swedish",
|
||||
"vi": "Vietnamese",
|
||||
"zh": "Chinese",
|
||||
}
|
25
test/test_iso639_1.py
Normal file
25
test/test_iso639_1.py
Normal file
@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2015 Mike Fährmann
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License version 2 as
|
||||
# published by the Free Software Foundation.
|
||||
|
||||
import unittest
|
||||
import gallery_dl.iso639_1 as iso639_1
|
||||
|
||||
class TestISO639_1(unittest.TestCase):
|
||||
|
||||
def test_code_to_language(self):
|
||||
self.assertEqual(iso639_1.code_to_language("en"), "English")
|
||||
self.assertEqual(iso639_1.code_to_language("xx"), "English")
|
||||
self.assertEqual(iso639_1.code_to_language("xx", default=None), None)
|
||||
def test_language_to_code(self):
|
||||
self.assertEqual(iso639_1.language_to_code("English"), "en")
|
||||
self.assertEqual(iso639_1.language_to_code("Nothing"), "en")
|
||||
self.assertEqual(iso639_1.language_to_code("Nothing", default=None), None)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in New Issue
Block a user