@digitalartlab/expo-plugin-localization
Version:
Native language switching in your Expo app
38 lines (37 loc) • 1.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withAndroidLocalizableGradle = void 0;
const config_plugins_1 = require("expo/config-plugins");
const setAndroidGradleLocalization = (buildGradle, locales) => {
const localesString = locales.map((locale) => `"${locale}"`).join(", ");
const resourceConfigurationsString = `resourceConfigurations += [${localesString}]`;
// There's already an exact match for the resourceConfigurations, so no need to add it again
if (buildGradle.includes(resourceConfigurationsString)) {
return buildGradle;
}
// There's already a resourceConfigurations, but it's not an exact
// One day, there might be a cleaner way to do this, but for now, we'll just throw an error and force the user to run expo prebuild --clean
if (buildGradle.includes("resourceConfigurations")) {
throw new Error(`build.gradle already contains a conflicting resourceConfigurations. Please run expo prebuild with the --clean flag to resolve.`);
}
// Add the resourceConfigurations to the defaultConfig
// Mind the indentation
return buildGradle.replace(/defaultConfig\s*{/, `defaultConfig {
resourceConfigurations += [${localesString}]`);
};
/**
* Adds the resourceConfigurations with selected locales to the defaultConfig in the build.gradle file
* See https://developer.android.com/guide/topics/resources/app-languages#gradle-config
*/
const withAndroidLocalizableGradle = (config, { locales }) => {
return (0, config_plugins_1.withAppBuildGradle)(config, (config) => {
if (config.modResults.language === "groovy") {
config.modResults.contents = setAndroidGradleLocalization(config.modResults.contents, locales);
}
else {
throw new Error(`Cannot configure localization because the build.gradle is not groovy`);
}
return config;
});
};
exports.withAndroidLocalizableGradle = withAndroidLocalizableGradle;