/ shared / localization / src / getPageDir.ts
getPageDir.ts
 1  /**
 2   * TODO: rdar://73010072 (Make localization utils its own package)
 3   * Copied from:
 4   * https://github.pie.apple.com/amp-ui/desktop-music-app/blob/main/app/utils/page-dir.js
 5   */
 6  
 7  // these overrides were determined to always show page in RTL, even if the global elements dont contain
 8  // an he_il entry
 9  // <rdar://problem/49297213> LOC: IW-IL: RTL: Web Preview Pages: The Preview Pages are not RTL.
10  const RTL_LANG_CODES_OVERRIDE = [
11      'he', // hebrew
12  ];
13  
14  const RTL_LANG_CODES = [
15      'ar', // arabic
16      'he', // hebrew
17      'ku', // kurdish
18      'ur', // urdu
19      'ps', // pashto
20      'yi', // yiddish
21  ];
22  
23  /**
24   * Determine the page-direction for a given locale
25   *
26   * @param {String} localeCode - A string containing a language code and region code separated by a hyphen.
27   * @param {String|undefined|null} langParam - A language code passed from the `l=` query param.
28   */
29  export function getPageDir(
30      localeCode: string,
31      langParam: string | undefined | null = null,
32  ) {
33      const twoLettersLangCode = localeCode.split('-')[0];
34      const isRTLLang = RTL_LANG_CODES.includes(twoLettersLangCode);
35      const isRTLLangOverride =
36          typeof langParam === 'string' &&
37          RTL_LANG_CODES_OVERRIDE.includes(langParam);
38  
39      return isRTLLang || isRTLLangOverride ? 'rtl' : 'ltr';
40  }