Skip to content

Commit baf2a7d

Browse files
authored
Refactor determining language list for command lookup (#123)
1 parent d273fcd commit baf2a7d

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ You can configure the behavior and output of the `tldr` client by setting enviro
6060
export TLDR_CACHE_MAX_AGE=720
6161
export TLDR_PAGES_SOURCE_LOCATION="https://raw.githubusercontent.com/tldr-pages/tldr/master/pages"
6262
export TLDR_DOWNLOAD_CACHE_LOCATION="https://tldr-pages.github.io/assets/tldr.zip"
63-
export TLDR_LANGUAGE="en"
6463

6564
### Cache
6665

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

112-
### Language
111+
### Language
113112

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

123122
### Remote source
124123

tldr.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from glob import glob
1919

2020
__version__ = "1.0.0"
21-
__client_specification__ = "1.2"
21+
__client_specification__ = "1.3"
2222

2323
REQUEST_HEADERS = {'User-Agent': 'tldr-python-client'}
2424
PAGES_SOURCE_LOCATION = os.environ.get(
@@ -31,11 +31,11 @@
3131
)
3232

3333
DEFAULT_LANG = os.environ.get(
34-
'TLDR_LANGUAGE',
35-
os.environ.get('LANG', None)
34+
'LANG',
35+
'C'
3636
).split('_')[0]
3737

38-
if DEFAULT_LANG == 'C':
38+
if DEFAULT_LANG == 'C' or DEFAULT_LANG == 'POSIX':
3939
DEFAULT_LANG = None
4040

4141
USE_CACHE = int(os.environ.get('TLDR_CACHE_ENABLED', '1')) > 0
@@ -163,16 +163,15 @@ def get_language_list():
163163
languages = os.environ.get('LANGUAGE', '').split(':')
164164
languages = list(map(
165165
lambda x: x.split('_')[0],
166-
filter(lambda x: not (x == 'C' or x == ''), languages)
166+
filter(lambda x: not (x == 'C' or x == 'POSIX' or x == ''), languages)
167167
))
168168
if DEFAULT_LANG is not None:
169-
try:
170-
languages.remove(DEFAULT_LANG)
171-
except ValueError:
172-
pass
173-
languages.insert(0, DEFAULT_LANG)
169+
if DEFAULT_LANG not in languages:
170+
languages.append(DEFAULT_LANG)
174171
else:
175-
languages.append('en')
172+
languages = []
173+
if 'en' not in languages:
174+
languages.append(None)
176175
return languages
177176

178177

0 commit comments

Comments
 (0)