Skip to content

Change language priority for environment variables #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ You can configure the behavior and output of the `tldr` client by setting enviro
export TLDR_CACHE_MAX_AGE=720
export TLDR_PAGES_SOURCE_LOCATION="https://raw.githubusercontent.com/tldr-pages/tldr/master/pages"
export TLDR_DOWNLOAD_CACHE_LOCATION="https://tldr-pages.github.io/assets/tldr.zip"
export TLDR_LANGUAGE="en"

### Cache

Expand Down Expand Up @@ -104,16 +103,16 @@ Any of the values of above may be omitted. For example, you can do similar thing
* `TLDR_COLOR_PARAMETER="red on_yellow underline"` for underlined red text on yellow background
* `TLDR_COLOR_NAME="bold underline"` for default system font and background colors with underline and bolded effects

### Language
### Language

The language that tldr will use is dependent on a number of factors. If you specify a language via the
`--language` flag, tldr will attempt to use that language and only that language. Otherwise, it will
default to language set using either `TLDR_LANGUAGE` before falling back to `LANG` (ignoring the value `C`).
If neither are set, then tldr will always attempt to get the `en` page. Finally, if `LANGUAGES` is set, it uses
this as the priority list to try languages in, with the exception that it will attempt `TLDR_LANGUAGE` and `LANG`
first, and if neither are set, will use `en` last (assuming it does not already appear somewhere in `LANGUAGES`).
default to language set using `LANGUAGE` and `LANG` (ignoring the value `C` and `POSIX`).
If neither are set, then tldr will always attempt to get the `en` page. Finally, if `LANG` is set, it uses `LANGUAGE`, if set,
first as the priority list to try languages in, followed by `LANG` if not included in `LANGUAGE`
and `en` as fallback (assuming it does not already appear somewhere in `LANGUAGE` or `LANG`).
All language values should be set to a value that follows [RFC 1766](https://tools.ietf.org/html/rfc1766.html),
with the special exception of `C` which is ignored.
with the special exception of `C` and `POSIX` which is ignored.

### Remote source

Expand Down
21 changes: 10 additions & 11 deletions tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import colorama # Required for Windows

__version__ = "1.0.0"
__client_specification__ = "1.2"
__client_specification__ = "1.3"

REQUEST_HEADERS = {'User-Agent': 'tldr-python-client'}
PAGES_SOURCE_LOCATION = os.environ.get(
Expand All @@ -28,11 +28,11 @@
)

DEFAULT_LANG = os.environ.get(
'TLDR_LANGUAGE',
os.environ.get('LANG', None)
'LANG',
'C'
).split('_')[0]

if DEFAULT_LANG == 'C':
if DEFAULT_LANG == 'C' or DEFAULT_LANG == 'POSIX':
DEFAULT_LANG = None

USE_CACHE = int(os.environ.get('TLDR_CACHE_ENABLED', '1')) > 0
Expand Down Expand Up @@ -160,16 +160,15 @@ def get_language_list():
languages = os.environ.get('LANGUAGE', '').split(':')
languages = list(map(
lambda x: x.split('_')[0],
filter(lambda x: not (x == 'C' or x == ''), languages)
filter(lambda x: not (x == 'C' or x == 'POSIX' or x == ''), languages)
))
if DEFAULT_LANG is not None:
try:
languages.remove(DEFAULT_LANG)
except ValueError:
pass
languages.insert(0, DEFAULT_LANG)
if DEFAULT_LANG not in languages:
languages.append(DEFAULT_LANG)
else:
languages.append('en')
languages = []
if 'en' not in languages:
languages.append(None)
return languages


Expand Down