react-native-notificare-push-ui
Version:
Notificare Push UI React Native module.
66 lines (65 loc) • 2.89 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.withNotificarePushUIAndroidLocalizable = void 0;
const Resources_1 = require("@expo/config-plugins/build/android/Resources");
const Strings_1 = require("@expo/config-plugins/build/android/Strings");
const Locales_1 = require("@expo/config-plugins/build/ios/Locales");
const XML_1 = require("@expo/config-plugins/build/utils/XML");
const warnings_1 = require("@expo/config-plugins/build/utils/warnings");
const config_plugins_1 = require("expo/config-plugins");
const path_1 = require("path");
const plugin_1 = require("react-native-notificare/lib/plugin");
const STRINGS_RESOURCE_NAME = 'strings.xml';
const withSetupLocalizable = (config, props) => {
const locales = props?.android?.locales;
if (!locales) {
return config;
}
return (0, config_plugins_1.withDangerousMod)(config, [
'android',
async (newConfig) => {
await createStringsResource(locales, newConfig.modRequest.projectRoot);
return newConfig;
},
]);
};
async function createStringsResource(locales, projectRoot) {
const localesMap = await (0, Locales_1.getResolvedLocalesAsync)(projectRoot, locales);
const hasDefaultValues = Object.keys(localesMap).some(function (lang) {
return lang === 'default';
});
if (!hasDefaultValues) {
(0, warnings_1.addWarningAndroid)('NotificarePushUIPluginProps.android.locales', 'Default values for localization have not been provided.');
}
for (const [langCode, localizationObj] of Object.entries(localesMap)) {
const lang = transformLangAndRegionCode(langCode);
const resPath = (0, path_1.resolve)(projectRoot, lang === 'default'
? `${plugin_1.RESOURCE_ROOT_PATH}/values`
: `${plugin_1.RESOURCE_ROOT_PATH}/values-${lang}`, STRINGS_RESOURCE_NAME);
const resXML = await (0, Resources_1.readResourcesXMLAsync)({ path: resPath });
const stringsItemsXML = [];
for (const [key, value] of Object.entries(localizationObj)) {
stringsItemsXML.push({
_: value,
$: {
name: key,
},
});
}
// Sets string and updates if already exists
const resultingXML = (0, Strings_1.setStringItem)(stringsItemsXML, resXML);
await (0, XML_1.writeXMLAsync)({ path: resPath, xml: resultingXML });
}
}
function transformLangAndRegionCode(langCode) {
const match = langCode.match(/^([a-z]{2})-([A-Z]{2})$/);
if (match) {
return `${match[1]}-r${match[2]}`;
}
return langCode;
}
const withNotificarePushUIAndroidLocalizable = (config, props) => {
config = withSetupLocalizable(config, props);
return config;
};
exports.withNotificarePushUIAndroidLocalizable = withNotificarePushUIAndroidLocalizable;
;