Skip to content
129 changes: 129 additions & 0 deletions src/os/locale.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const macos = @import("macos");
const objc = @import("objc");
const internal_os = @import("main.zig");
const i18n = internal_os.i18n;
const i18n_locales = @import("i18n_locales.zig");

const log = std.log.scoped(.os_locale);

Expand All @@ -28,6 +29,29 @@ pub fn ensureLocale(alloc: std.mem.Allocator) !void {
}
}

language: {
if ((try internal_os.getenv(alloc, "LANGUAGE"))) |language| {
Comment thread
EriksRemess marked this conversation as resolved.
Resolved
defer language.deinit(alloc);

if (language.value.len <= 0) break :language;

var out: [64:0]u8 = undefined;
var writer: std.Io.Writer = .fixed(out[0..out.len]);
Comment thread
EriksRemess marked this conversation as resolved.
Resolved

const ok = preferredLanguage(&writer, language.value) catch false;

if (!ok) break :language;

writer.writeByte(0) catch break :language;

const value = writer.buffered();
const preferred = out[0 .. value.len - 1 :0];

log.info("setlocale preferred locale from LANGUAGE {s}", .{preferred});
_ = internal_os.setenv("LANGUAGE", preferred);
}
}

// Set the locale to whatever is set in env vars.
if (setlocale(LC_ALL, "")) |v| {
log.info("setlocale from env result={s}", .{v});
Expand Down Expand Up @@ -210,9 +234,114 @@ fn preferredLanguageFromCocoa(
return slice[0 .. slice.len - 1 :0];
}

fn preferredLanguage(writer: *std.Io.Writer, language: []const u8) !bool {
var locale_buf: [64]u8 = undefined;
var first = true;

// LANGUAGE might come as lv:en, without .UTF-8. Ordered by preference,
// we check one by one to find first supported locale.
var it = std.mem.splitScalar(u8, language, ':');
while (it.next()) |entry| {
if (entry.len == 0) continue;
defer first = false;

// If entry is already a full UTF-8 locale, keep it when supported.
if (std.mem.endsWith(u8, entry, ".UTF-8")) {
for (i18n_locales.locales) |supported| {
if (!std.mem.eql(u8, entry, supported)) continue;
try writer.writeAll(supported);
return true;
}
}

// If the first preferred language is English (Ghostty's default), stop.
if (first and std.mem.eql(u8, entry, "en")) {
return false;
}

// If entry has no region (no "_*"), pick the first matching full locale.
if (std.mem.indexOfScalar(u8, entry, '_') == null) {
for (i18n_locales.locales) |locale| {
if (!std.mem.startsWith(u8, locale, entry)) continue;
if (locale.len <= entry.len) continue;
if (locale[entry.len] != '_') continue;
try writer.writeAll(locale);
return true;
}
}

// Normalize to "{entry}.UTF-8" when supported.
const normalized = std.fmt.bufPrint(&locale_buf, "{s}.UTF-8", .{entry}) catch continue;
for (i18n_locales.locales) |supported| {
if (!std.mem.eql(u8, normalized, supported)) continue;
try writer.writeAll(supported);
return true;
}
}

return false;
}

const LC_ALL: c_int = 6; // from locale.h
const LC_ALL_MASK: c_int = 0x7fffffff; // from locale.h
const locale_t = ?*anyopaque;
extern "c" fn setlocale(category: c_int, locale: ?[*]const u8) ?[*:0]u8;
extern "c" fn newlocale(category: c_int, locale: ?[*]const u8, base: locale_t) locale_t;
extern "c" fn freelocale(v: locale_t) void;

test "preferredLanguage normalizes short language code" {
const testing = std.testing;
var buf: std.Io.Writer.Allocating = .init(testing.allocator);
defer buf.deinit();

const ok = try preferredLanguage(&buf.writer, "lv");

try testing.expect(ok);
try testing.expectEqualStrings("lv_LV.UTF-8", buf.written());
}

test "preferredLanguage keeps english when first and doesn't overwrite env" {
const testing = std.testing;
var buf: std.Io.Writer.Allocating = .init(testing.allocator);
defer buf.deinit();

const ok = try preferredLanguage(&buf.writer, "en:lv");

try testing.expect(!ok); // ignores LANGUAGE and just continues
try testing.expectEqual(0, buf.written().len);
try testing.expectEqualStrings("", buf.written());
}

test "preferredLanguage normalizes locale without encoding" {
const testing = std.testing;
var buf: std.Io.Writer.Allocating = .init(testing.allocator);
defer buf.deinit();

const ok = try preferredLanguage(&buf.writer, "it_IT");

try testing.expect(ok);
try testing.expectEqualStrings("it_IT.UTF-8", buf.written());
}

test "preferredLanguage keeps locale with encoding" {
const testing = std.testing;
var buf: std.Io.Writer.Allocating = .init(testing.allocator);
defer buf.deinit();

const ok = try preferredLanguage(&buf.writer, "pt_BR.UTF-8");

try testing.expect(ok);
try testing.expectEqualStrings("pt_BR.UTF-8", buf.written());
}

test "preferredLanguage returns false and writes nothing for completely invalid LANGUAGE" {
const testing = std.testing;
var buf: std.Io.Writer.Allocating = .init(testing.allocator);
defer buf.deinit();

const ok = try preferredLanguage(&buf.writer, "zz:qq_QQ:xx_YY.ISO-8859-1:__:@@@");

try testing.expect(!ok);
try testing.expectEqual(0, buf.written().len);
try testing.expectEqualStrings("", buf.written());
}
Loading