UNPKG

@digitalartlab/expo-plugin-localization

Version:
60 lines (59 loc) 2.59 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.withAndroidLocalizableResources = void 0; const config_plugins_1 = require("expo/config-plugins"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); /** * Create res/xml/locales_config.xml file with selected locales * * See https://developer.android.com/guide/topics/resources/app-languages#use-localeconfig */ const withAndroidLocalizableResources = (config, { locales }) => { return (0, config_plugins_1.withDangerousMod)(config, [ "android", async (config) => { const projectRootPath = path.join(config.modRequest.platformProjectRoot); const RESOURCES = "app/src/main/res/xml"; const destAlreadyExists = fs.existsSync(path.join(projectRootPath, RESOURCES)); if (!destAlreadyExists) { fs.mkdirSync(path.join(projectRootPath, RESOURCES), { recursive: true, }); } const localeStrings = locales .map((locale) => `<locale android:name="${locale}" />`) .join("\n"); const xml = `<?xml version="1.0" encoding="utf-8"?> <locale-config xmlns:android="http://schemas.android.com/apk/res/android"> ${localeStrings} </locale-config>`; fs.writeFileSync(path.join(projectRootPath, RESOURCES, "locales_config.xml"), xml); return config; }, ]); }; exports.withAndroidLocalizableResources = withAndroidLocalizableResources;