| #!/usr/bin/env python |
| # Licensed to the Apache Software Foundation (ASF) under one or more |
| # contributor license agreements. See the NOTICE file distributed with |
| # this work for additional information regarding copyright ownership. |
| # The ASF licenses this file to You under the Apache License, Version 2.0 |
| # (the "License"); you may not use this file except in compliance with |
| # the License. You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| """ |
| Converts among language names and abbreviations using Python's ISO-639 module. |
| https://pypi.python.org/pypi/iso-639/0.4.3 |
| |
| You need to install the iso-639 module: |
| |
| pip install [--user] iso-639 |
| |
| Here is some example usage of that module: |
| |
| > from iso639 import languages as l |
| > greek = l.get(part1 = 'el') |
| > pprint(vars(greek)) |
| {'inverted': 'Greek, Modern (1453-)', |
| 'macro': '', |
| 'name': 'Modern Greek (1453-)', |
| 'names': [], |
| 'part1': 'el', |
| 'part2b': 'gre', |
| 'part2t': 'ell', |
| 'part3': 'ell', |
| 'part5': ''} |
| """ |
| |
| import sys |
| |
| if len(sys.argv) != 2: |
| print "Usage: iso639 LANG" |
| print "where LANG is a language name or abbreviation." |
| sys.exit(1) |
| |
| # Common manual mappings in order to avoid the potentially |
| # not present iso639 module |
| quickdict = { |
| 'English': 'en', |
| 'en': 'English', |
| 'Spanish': 'es', |
| 'es': 'Spanish', |
| 'Hebrew': 'he', |
| 'he': 'Hebrew', |
| 'German': 'de', |
| 'de': 'German', |
| 'French': 'fr', |
| 'fr': 'French', |
| 'Arabic': 'ar', |
| 'ar': 'Arabic', |
| 'Czech': 'cs', |
| 'cs': 'Czech', |
| 'Romanian': 'ro', |
| 'ro': 'Romanian', |
| } |
| |
| input = sys.argv[1] |
| |
| if quickdict.has_key(input): |
| print quickdict[input] |
| |
| else: |
| # Only load this if we have to |
| |
| try: |
| import iso639 |
| |
| if len(input) == 2: |
| lang = iso639.languages.get(part1 = input) |
| print lang.name |
| elif len(input) == 3: |
| lang = iso639.languages.get(part3 = input) |
| print lang.name |
| else: |
| lang = iso639.languages.get(name = input) |
| print lang.part1 |
| |
| except ImportError: |
| sys.stderr.write('Can\'t load module iso639 ("pip install --user iso-639")\n') |
| |
| except KeyError: |
| sys.stderr.write('Language not found: {}\n'.format(input)) |