* @function getLocalizedPath * @description Returns a localized file path accoring to the locale. * * Localized files are searched in subfolders of a given path, e.g. * * root/ * ├── base/ // base path to files * │ ├── example.html // default file
(defaultPath, locale)
| 41 | * there is no matching localized file. |
| 42 | */ |
| 43 | static async getLocalizedPath(defaultPath, locale) { |
| 44 | // Get file name and paths |
| 45 | const file = path.basename(defaultPath); |
| 46 | const basePath = path.dirname(defaultPath); |
| 47 | |
| 48 | // If locale is not set return default file |
| 49 | if (!locale) { |
| 50 | return { path: defaultPath }; |
| 51 | } |
| 52 | |
| 53 | // Check file for locale exists |
| 54 | const localePath = path.join(basePath, locale, file); |
| 55 | const localeFileExists = await Utils.fileExists(localePath); |
| 56 | |
| 57 | // If file for locale exists return file |
| 58 | if (localeFileExists) { |
| 59 | return { path: localePath, subdir: locale }; |
| 60 | } |
| 61 | |
| 62 | // Check file for language exists |
| 63 | const language = locale.split('-')[0]; |
| 64 | const languagePath = path.join(basePath, language, file); |
| 65 | const languageFileExists = await Utils.fileExists(languagePath); |
| 66 | |
| 67 | // If file for language exists return file |
| 68 | if (languageFileExists) { |
| 69 | return { path: languagePath, subdir: language }; |
| 70 | } |
| 71 | |
| 72 | // Return default file |
| 73 | return { path: defaultPath }; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @function fileExists |