Jump to content

Recommended Posts

Posted

I've been trying to work with some pre-1.8 tutorials and examples, but I can't figure out how to get my single item's name localized.

 

Does anyone have a guide or something on localization in 1.8? I've got a manually-made en_US.lang under src/main/resources/assets/lang in my Eclipse Package Explorer, but when I grab the item in-game it reads item.bacon.name

Posted

It should be under src/main/resources/assets/your_mod_id/lang

 

Awesome, thanks! Though, if it requires a folder labelled with your_mod_id, then how's it work that it allows item and block textures to be in folders in assets? Wouldn't it be logical to put such things into the same folder as the lang files?

Posted

One more quick question; in the event that I'm creating a class to automagically add lines to my en_US.lang file, how would I navigate to said file? Currently, I've got a LangRegistry class that extends a custom RegistryFile class, with a constructor as such;

RegistryFile Constructor (LangRegistry uses super())

public RegistryFile(String filePath) {
this.filePath = filePath;
this.file     = new File(filePath);
}

 

I've got "resources/assets/cshex/lang/en_US.lang" passed to my LangRegistry class at the moment, but it doesn't seem to read it, let alone write to it.

 

NOTE I'm only learning; I'm reverse engineering a well-established mod, so my questions are pretty noobish for the stuff I'm trying to do. However, I've had previous programming experience, only Java itself is new for me :P

Posted

You shouldn't need any kind of special Registry at all - registering your Item to the normal GameRegistry and setting its unlocalized name will automatically check your language file for the string "item.unlocalized_name.name".

 

As for the directory structure, everyone sets it up differently, so it was probably fine where it was before, but the default workspace setup usually has the folder as src/main/resources/assets/mod_id/actual_folders.

Posted

You shouldn't need any kind of special Registry at all - registering your Item to the normal GameRegistry and setting its unlocalized name will automatically check your language file for the string "item.unlocalized_name.name".

The point was to create a class that added localized names via a simple regex process so that I could add items and tabs and the like and not have to manually enter new localizations. It partially works, except my bugged version had been putting en_US.lang in the root directory and made multiple copies of the same line (an issue I am trying to debug :P)

 

Though, I tested a couple things out while writing this reply, and it seems that manually adding entries is my best bet until I learn some more about Java and Forge.

 

As for the directory structure, everyone sets it up differently, so it was probably fine where it was before, but the default workspace setup usually has the folder as src/main/resources/assets/mod_id/actual_folders.

Changed the path that was being passed to LangRegistry to match up with that format; can't find the file that is - to me - clearly there. Not a problem for the time being, but something I want to fix.

 

RegistryFile.class (sorry for lack of brevity)

public abstract class RegistryFile {
protected String filePath;
protected BufferedWriter writer;
protected File file;
protected StringBuilder builder = new StringBuilder();

public RegistryFile(String filePath) {
	this.filePath = filePath;
	this.file     = new File(filePath);
}

public abstract void addNames();

public void localizeName(String prefixOfLine, String unlocalizedName) {
	String name = unlocalizedName.substring(5);
	char firstLetter = name.charAt(0);
	if(Character.isLowerCase(firstLetter)) {
		firstLetter = Character.toUpperCase(firstLetter);
	}
	String inGame = name.substring(1);
	String temp   = inGame;

	for(int p = 1; p < temp.length(); p++) {
		char c   = inGame.charAt(p);
		int code = c;

		if(inGame.charAt(p - 1) != ' ') {
			for(int n = 65; n < 90; n++) {
				if(code == n) {
					inGame = new StringBuffer(inGame).insert(p, "").toString();
				}
			}
		}

		inGame = inGame.replaceAll(" Of ", " of ").replaceAll(" The ", " the ");
		String finalName = firstLetter + inGame;
		addToFile(prefixOfLine + "." + name + ".name=" + finalName, name);
	}
}

public static String getLocalizedMobName(String str) {
	int l = str.length();
	if(str.length() > 1) {
		for(int i = 1; i < l; i++) {
			if((Character.isUpperCase(str.charAt(i))) && (str.charAt(i - 1) != ' ')) {
				str = new StringBuffer(str).insert(i, " ").toString();
			}
		}
	}

	return str.replace("HRPG", "");
}

public String readFile(String path) {
	StringBuilder source = new StringBuilder();
	BufferedReader reader = null;
	File file = new File(path);

	try {
		reader = new BufferedReader(new FileReader(file));
		String line;
		while((line = reader.readLine()) != null) {
			source.append(line);
		}
		reader.close();
	} catch(Exception e) {
		e.printStackTrace();
	}

	return source.toString();
}

public void addName(String prefix, String keyName, String inGameName) { addToFile(prefix + "." + keyName + "=" + inGameName, keyName); }

public void addToFile(String inGame, String oldName) {
	String temp = inGame;
	Log.dev("Registered new name, " + oldName + " became " + temp.substring(temp.indexOf('=') + 1));
	this.builder.append(inGame + "\n");
}

public void addToFile(String text) { this.builder.append(text + "\n"); }

public void write() {
	try {
		if(!this.file.exists()) { this.file.createNewFile(); }

		this.writer = new BufferedWriter(new FileWriter(this.file));
		this.writer.write(this.builder.toString());
		this.writer.close();
	} catch(IOException e) {
		e.printStackTrace();
	}
}
}

 

LangRegistry.class (extends RegistryFile)

public class LangRegistry extends RegistryFile {
private static ArrayList<HexTabs> tabs = new ArrayList();
private static ArrayList<Item> items   = new ArrayList();

public static final RegistryFile instance = new LangRegistry();

public LangRegistry() {
	super("src/main/resources/assets/cshex/lang/en_US.lang");
}

public static void registerNames() {
	instance.addNames();
}

public void addNames() {
	for(Item item : items) {
		localizeName("item", item.getUnlocalizedName());
	}

	instance.write();
}

public static void addTab(HexTabs tab) { tabs.add(tab); }
public static void addItem(Item item) { items.add(item); }
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.