summaryrefslogtreecommitdiff
path: root/shared/localization/src/getPageDir.ts
blob: 47b855d6aab70cd6715662b7748d98b20e3184cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
 * TODO: rdar://73010072 (Make localization utils its own package)
 * Copied from:
 * https://github.pie.apple.com/amp-ui/desktop-music-app/blob/main/app/utils/page-dir.js
 */

// these overrides were determined to always show page in RTL, even if the global elements dont contain
// an he_il entry
// <rdar://problem/49297213> LOC: IW-IL: RTL: Web Preview Pages: The Preview Pages are not RTL.
const RTL_LANG_CODES_OVERRIDE = [
    'he', // hebrew
];

const RTL_LANG_CODES = [
    'ar', // arabic
    'he', // hebrew
    'ku', // kurdish
    'ur', // urdu
    'ps', // pashto
    'yi', // yiddish
];

/**
 * Determine the page-direction for a given locale
 *
 * @param {String} localeCode - A string containing a language code and region code separated by a hyphen.
 * @param {String|undefined|null} langParam - A language code passed from the `l=` query param.
 */
export function getPageDir(
    localeCode: string,
    langParam: string | undefined | null = null,
) {
    const twoLettersLangCode = localeCode.split('-')[0];
    const isRTLLang = RTL_LANG_CODES.includes(twoLettersLangCode);
    const isRTLLangOverride =
        typeof langParam === 'string' &&
        RTL_LANG_CODES_OVERRIDE.includes(langParam);

    return isRTLLang || isRTLLangOverride ? 'rtl' : 'ltr';
}