|
| 1 | +# -*- coding: UTF-8 -*- |
| 2 | +# A part of NonVisual Desktop Access (NVDA) |
| 3 | +# Copyright (C) 2006-2020 NV Access Limited, Łukasz Golonka |
| 4 | +# This file may be used under the terms of the GNU General Public License, version 2 or later. |
| 5 | +# For more details see: https://www.gnu.org/licenses/gpl-2.0.html |
| 6 | + |
| 7 | +import os |
| 8 | +import sys |
| 9 | + |
| 10 | +import globalVars |
| 11 | +import languageHandler |
| 12 | + |
| 13 | + |
| 14 | +def getDocFilePath(fileName, localized=True): |
| 15 | + if not getDocFilePath.rootPath: |
| 16 | + if hasattr(sys, "frozen"): |
| 17 | + getDocFilePath.rootPath = os.path.join(globalVars.appDir, "documentation") |
| 18 | + else: |
| 19 | + getDocFilePath.rootPath = os.path.join(globalVars.appDir, "..", "user_docs") |
| 20 | + |
| 21 | + if localized: |
| 22 | + lang = languageHandler.getLanguage() |
| 23 | + tryLangs = [lang] |
| 24 | + if "_" in lang: |
| 25 | + # This locale has a sub-locale, but documentation might not exist for the sub-locale, so try stripping it. |
| 26 | + tryLangs.append(lang.split("_")[0]) |
| 27 | + # If all else fails, use English. |
| 28 | + tryLangs.append("en") |
| 29 | + |
| 30 | + fileName, fileExt = os.path.splitext(fileName) |
| 31 | + for tryLang in tryLangs: |
| 32 | + tryDir = os.path.join(getDocFilePath.rootPath, tryLang) |
| 33 | + if not os.path.isdir(tryDir): |
| 34 | + continue |
| 35 | + |
| 36 | + # Some out of date translations might include .txt files which are now .html files in newer translations. |
| 37 | + # Therefore, ignore the extension and try both .html and .txt. |
| 38 | + for tryExt in ("html", "txt"): |
| 39 | + tryPath = os.path.join(tryDir, f"{fileName}.{tryExt}") |
| 40 | + if os.path.isfile(tryPath): |
| 41 | + return tryPath |
| 42 | + return None |
| 43 | + else: |
| 44 | + # Not localized. |
| 45 | + if not hasattr(sys, "frozen") and fileName in ("copying.txt", "contributors.txt"): |
| 46 | + # If running from source, these two files are in the root dir. |
| 47 | + return os.path.join(globalVars.appDir, "..", fileName) |
| 48 | + else: |
| 49 | + return os.path.join(getDocFilePath.rootPath, fileName) |
| 50 | + |
| 51 | + |
| 52 | +getDocFilePath.rootPath = None |
0 commit comments