-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support expo config plugin (#25)
To make it easier for people using this library with Expo, I added support for the Expo config plugin to enable automatic generation of native configurations. This feature is currently in the experimental phase, so any feedback is greatly appreciated! Simply add the config plugin to your `app.json` file and specify the list of supported languages: ```json { "expo": { // ... "plugins": [ [ "react-native-localization-settings", { "languages": ["en", "pl"] } ] ] } } ``` and run `npx expo prebuild`.
- Loading branch information
Showing
9 changed files
with
3,663 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ android/keystores/debug.keystore | |
|
||
# Expo | ||
.expo/ | ||
tsconfig.tsbuildinfo | ||
|
||
# Turborepo | ||
.turbo/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./plugin/build'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
AndroidConfig, | ||
ConfigPlugin, | ||
withAndroidManifest, | ||
} from '@expo/config-plugins'; | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
|
||
export const withAndroidLanguages: ConfigPlugin<{ | ||
languages?: string[]; | ||
}> = (config, { languages = [] } = {}) => { | ||
config = withAndroidManifest(config, (config) => { | ||
const mainApplication = AndroidConfig.Manifest.getMainApplicationOrThrow( | ||
config.modResults | ||
); | ||
|
||
// Add the `android:localeConfig` attribute to the `<application>` tag | ||
mainApplication.$['android:localeConfig'] = '@xml/locales_config'; | ||
|
||
const localesConfigPath = path.join( | ||
config.modRequest.platformProjectRoot, | ||
'app/src/main/res/xml/locales_config.xml' | ||
); | ||
// Ensure the `res/xml` directory exists | ||
fs.mkdirSync(path.dirname(localesConfigPath), { recursive: true }); | ||
|
||
// Prepare locales_config.xml content | ||
const xmlContent = `<?xml version="1.0" encoding="utf-8"?> | ||
<locale-config xmlns:android="http://schemas.android.com/apk/res/android"> | ||
${languages.map((lang) => `<locale android:name="${lang}" />`).join('\n ')} | ||
</locale-config>`; | ||
|
||
// Write the `res/xml/locales_config.xml` file | ||
fs.writeFileSync(localesConfigPath, xmlContent); | ||
|
||
return config; | ||
}); | ||
|
||
return config; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ConfigPlugin } from '@expo/config-plugins'; | ||
import { withAndroidLanguages } from './android'; | ||
import { withIosLanguages } from './ios'; | ||
|
||
const withReactNativeLocalizationSettings: ConfigPlugin<{ | ||
languages?: string[]; | ||
}> = (config, { languages = [] } = {}) => { | ||
config = withAndroidLanguages(config, { languages }); | ||
config = withIosLanguages(config, { languages }); | ||
|
||
return config; | ||
}; | ||
|
||
export default withReactNativeLocalizationSettings; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { | ||
ConfigPlugin, | ||
IOSConfig, | ||
withXcodeProject, | ||
} from '@expo/config-plugins'; | ||
|
||
const generateLocalizableContent = (languages: string[]) => `{ | ||
"sourceLanguage" : "Base", | ||
"strings" : { | ||
"react-native-localization-settings" : { | ||
"extractionState" : "manual", | ||
"localizations" : { | ||
"base" : {"stringUnit" : {"state" : "translated","value" : ""}}, | ||
${languages | ||
.map( | ||
(lang) => | ||
`"${lang}" : {"stringUnit" : {"state" : "translated","value" : ""}},` | ||
) | ||
.join('\n ')} | ||
} | ||
} | ||
}, | ||
"version" : "1.0" | ||
}`; | ||
|
||
export const withIosLanguages: ConfigPlugin<{ | ||
languages?: string[]; | ||
}> = (config, { languages = [] } = {}) => { | ||
config = withXcodeProject(config, (config) => { | ||
const project = config.modResults; | ||
const projectObject = | ||
project.pbxProjectSection()[project.getFirstProject().uuid]; | ||
if (projectObject) { | ||
// Add known regions to the project | ||
projectObject.knownRegions = ['Base', ...languages]; | ||
|
||
// Write the Localizable.xcstrings file | ||
IOSConfig.XcodeProjectFile.createBuildSourceFile({ | ||
project, | ||
nativeProjectRoot: config.modRequest.platformProjectRoot, | ||
filePath: 'Localizable.xcstrings', | ||
fileContents: generateLocalizableContent(languages), | ||
overwrite: true, | ||
}); | ||
} | ||
return config; | ||
}); | ||
return config; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
{ | ||
"extends": "./tsconfig", | ||
"exclude": ["example"] | ||
|
Oops, something went wrong.