Skip to content

Commit

Permalink
Add localized app name support on ios and android
Browse files Browse the repository at this point in the history
Fixes #7
  • Loading branch information
sakiwei committed Sep 8, 2024
1 parent dea0c4f commit 8d4b8c7
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 112 deletions.
118 changes: 118 additions & 0 deletions app.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import withAndroidLocalizedName from './vendors/expo-android-localized-app-name';

module.exports = ({ config }) => {
return {
...config,
name: '巴士到站預報 - hkbus.app',
slug: 'hkbusedemo-dev', // Replace with your app's slug
version: '2.8.1', // Your app's version
orientation: 'portrait',
icon: './assets/icon.png',
splash: {
image: './assets/splash.png',
resizeMode: "contain",
backgroundColor: "#000000"
},
updates: {
fallbackToCacheTimeout: 0,
url: 'https://u.expo.dev/d05aadae-7952-423d-bc30-31504dfbf8d2',
requestHeaders: {
'expo-channel-name': 'production'
},
enabled: false
},
plugins: [
[
'expo-tracking-transparency',
{
userTrackingPermission: 'This identifier will be used to count the number of active users.'
}
],
[
'expo-location',
{
locationAlwaysAndWhenInUsePermission: 'Geolocation permission will be used to alert your arrival for the stop',
locationWhenInUsePermission: 'Your location will be used to determine the nearby bus stops',
locationAlwaysPermission: 'Geolocation permission will be used to alert your arrival for the stop',
isAndroidBackgroundLocationEnabled: true
}
],
[
'expo-notifications',
{
icon: './assets/logo96.png',
iosDisplayInForegroud: true
}
],
[
'expo-build-properties',
{
android: {
targetSdkVersion: 34,
minSdkVersion: 29
}
}
],
withAndroidLocalizedName
],
ios: {
supportsTablet: true,
bundleIdentifier: 'app.hkbus',
infoPlist: {
CFBundleAllowMixedLocalizations: true,
NSLocationAlwaysAndWhenInUseUsageDescription: 'Your location will be used to determine the nearby bus stops',
NSLocationWhenInUseUsageDescription: 'Your location will be used to determine the nearby bus stops',
NSUserTrackingUsageDescription: 'This identifier will be used to count the number of active users.',
UIBackgroundModes: [
'location',
'remote-notification'
]
},
appStoreUrl: 'https://apps.apple.com/hk/app/%E5%B7%B4%E5%A3%AB%E5%88%B0%E7%AB%99%E9%A0%90%E5%A0%B1-hkbus-app/id1612184906',
associatedDomains: [
'applinks:hkbus.app'
],
buildNumber: '8'
},
android: {
adaptiveIcon: {
foregroundImage: './assets/adaptive-icon.png',
backgroundColor: '#000000'
},
package: 'app.hkbus',
versionCode: 21,
intentFilters: [
{
action: 'VIEW',
autoVerify: true,
data: [
{
scheme: 'https',
host: 'hkbus.app',
pathPrefix: '/'
}
],
category: [
'BROWSABLE',
'DEFAULT'
]
}
]
},
web: {
favicon: './assets/favicon.png'
},
extra: {
eas: {
projectId: 'd138ee5b-fb89-4552-ab28-d574884df2c6'
}
},
runtimeVersion: {
policy: 'appVersion'
},
locales: {
en: './locales/en.json',
'zh-Hant': './locales/zh-Hant.json'
}
};
};
112 changes: 0 additions & 112 deletions app.json

This file was deleted.

4 changes: 4 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"CFBundleDisplayName": "hkbus.app",
"app_name": "hkbus.app"
}
4 changes: 4 additions & 0 deletions locales/zh-Hant.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"CFBundleDisplayName": "巴士到站預報",
"app_name": "巴士到站預報"
}
45 changes: 45 additions & 0 deletions vendors/expo-android-localized-app-name/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Source: https://github.com/mmomtchev/expo-android-localized-app-name/pull/3
// `expo-android-localized-app-name` at the moment does not support `zh-Hant` locale and that repo is not maintained anymore
const fs = require('fs');
const path = require('path');
const { withStringsXml, AndroidConfig } = require('@expo/config-plugins');
const xml2js = require('xml2js');
const builder = new xml2js.Builder({ headless: true });

function withAndroidLocalizedName(config) {
return withStringsXml(config,
async config => {
const projectRoot = config.modRequest.projectRoot;
const resPath = await AndroidConfig.Paths.getResourceFolderAsync(projectRoot);
for (const locale of Object.keys(config.locales ?? {})) {
const json = await fs.promises.readFile(config.locales[locale]);
const strings = JSON.parse(json);
const resources = [];
for (const key of Object.keys(strings)) {
// Skip values that are not marked for translation or simply do not exist
// because gradle does not like them
const untranslated = config.modResults.resources.string?.find(item =>
item.$.name === key && item.$.translatable !== false);
if (untranslated)
resources.push({ string: { $: { name: key }, _: strings[key] } });
}
if (resources.length) {
let finalLocale = locale;
if (finalLocale.includes("-")) {
finalLocale = `b+${finalLocale.replaceAll("-", "+")}`;
}
await fs.promises.mkdir(path.resolve(resPath, `values-${finalLocale}`), {
recursive: true,
});
await fs.promises.writeFile(
path.resolve(resPath, `values-${finalLocale}`, 'strings.xml'),
builder.buildObject({ resources })
);
}
}
return config;
},
);
};

module.exports = withAndroidLocalizedName;

0 comments on commit 8d4b8c7

Please sign in to comment.