Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(per-app-language): add Android config plugin #247

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/react-native-android-per-app-language/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @generated by expo-module-scripts
module.exports = require('expo-module-scripts/eslintrc.base.js');
8 changes: 8 additions & 0 deletions packages/react-native-android-per-app-language/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### @config-plugins/react-native-android-per-app-language 1.0.0

-
### Added
- Initial release of the `@config-plugins/react-native-android-per-app-language` plugin.
- Implemented support for Android's per-app language preferences feature.
- Added functionality to modify the Android Manifest to include the `localeConfig` attribute.
- Created a config plugin to generate the `locales_config.xml` file with user-specified supported languages.
41 changes: 41 additions & 0 deletions packages/react-native-android-per-app-language/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# @config-plugins/react-native-android-per-app-language

### How it works
This plugin automates the manual setup process described in the [Android documentation](https://developer.android.com/guide/topics/resources/app-languages#use-localeconfig) for implementing per-app language preferences:

1. Modifies the `AndroidManifest.xml` to add the `android:localeConfig` attribute to the `<application>` tag.
2. Creates a `res/xml/locales_config.xml` file with the specified supported languages.

## Expo installation
To use this plugin in your Expo project, follow these steps:

1. Install the plugin:
```
expo install @config-plugins/react-native-android-per-app-language
```

2. Add the plugin to your `app.json` or `app.config.js`:
```json
{
"expo": {
"plugins": [
[
"@config-plugins/react-native-android-per-app-language",
{
"supportedLanguages": ["en", "es", "fr"]
}
]
]
}
}
```

The `supportedLanguages` parameter is required and should be an array of language codes that your app supports.

3. Rebuild your app:
```
expo prebuild
```

This will configure your Android project to support per-app language preferences for the specified languages.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./build/withAndroidPerAppLanguage");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('expo-module-scripts/jest-preset-plugin');
41 changes: 41 additions & 0 deletions packages/react-native-android-per-app-language/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@config-plugins/react-native-android-per-app-language",
"version": "8.0.0",
"description": "Config plugin to auto configure react-native-android-per-app-language on prebuild",
"main": "build/withAndroidPerAppLanguage.js",
"types": "build/withAndroidPerAppLanguage.d.ts",
"sideEffects": false,
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/expo/config-plugins.git",
"directory": "packages/react-native-android-per-app-language"
},
"files": [
"build",
"app.plugin.js"
],
"scripts": {
"build": "expo-module build",
"clean": "expo-module clean",
"lint": "expo-module lint",
"test": "expo-module test",
"prepare": "expo-module prepare",
"prepublishOnly": "expo-module prepublishOnly",
"expo-module": "expo-module"
},
"keywords": [
"android",
"localization",
"react-native-android-per-app-language",
"react-native",
"expo"
],
"peerDependencies": {
"expo": "^51"
},
"devDependencies": {
"expo-module-scripts": "^3.5.1"
},
"upstreamPackage": "react-native-android-per-app-language"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getTemplateFile returns only base-config when subdomains is '*' 1`] = `
"<?xml version="1.0" encoding="utf-8"?>
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
<locale android:name="en"/>
</locale-config>"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getTemplateFile } from "../withAndroidPerAppLanguage";

describe("getTemplateFile", () => {
it("returns the expected XML content", () => {
const supportedLanguages = ["en", "fr"];
const expectedContent = `<?xml version="1.0" encoding="utf-8"?>
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
<locale android:name="en"/>
<locale android:name="fr"/>
</locale-config>`;
const content = getTemplateFile(supportedLanguages);
expect(content).toEqual(expectedContent);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
ConfigPlugin,
withAndroidManifest,
AndroidManifest,
withDangerousMod,
AndroidConfig,
} from "expo/config-plugins";
import * as fs from "fs";
import * as path from "path";

type SupportedLanguages = string[];

export const withAndroidPerAppLanguage: ConfigPlugin<{
supportedLanguages: SupportedLanguages;
}> = (config, { supportedLanguages }) => {
if (!supportedLanguages || !supportedLanguages.length) {
return config;
}

/**
* Modify the Android Manifest 'application' to include the localeConfig attribute
*/
config = withAndroidManifest(config, (config) => {
config.modResults = addLocaleConfigToManifest(config.modResults);
return config;
});

/**
* Create `network_security_config.xml` resource file.
*/
return withDangerousMod(config, [
"android",
async (config) => {
const folder = path.join(
config.modRequest.platformProjectRoot,
`app/src/main/res/xml`,
);
fs.mkdirSync(folder, { recursive: true });
fs.writeFileSync(
path.join(folder, "locales_config.xml"),
getTemplateFile(supportedLanguages),
{ encoding: "utf8" },
);
return config;
},
]);
};

export const addLocaleConfigToManifest = (androidManifest: AndroidManifest) => {
const application =
AndroidConfig.Manifest.getMainApplicationOrThrow(androidManifest);

application.$["android:localeConfig"] = "@xml/locales_config";
return androidManifest;
};

export const getTemplateFile = (supportedLanguages: SupportedLanguages) => {
const localesList = supportedLanguages
.map((lang) => ` <locale android:name="${lang}"/>`)
.join("\n");

const content = `<?xml version="1.0" encoding="utf-8"?>
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
${localesList}
</locale-config>`;
return content;
};
9 changes: 9 additions & 0 deletions packages/react-native-android-per-app-language/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "expo-module-scripts/tsconfig.plugin",
"compilerOptions": {
"outDir": "build",
"rootDir": "src"
},
"include": ["./src"],
"exclude": ["**/__mocks__/*", "**/__tests__/*"]
}